From 6d697e3e7430737a6eb7668ba7eb1e10c0095ab7 Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 28 Dec 2023 14:31:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4lib.rs=EF=BC=8C=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E6=96=87=E4=BB=B6=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.rs | 3 +-- src/commands/branch.rs | 5 ++++- src/commands/commit.rs | 4 ++-- src/commands/log.rs | 2 +- src/commands/merge.rs | 6 ++---- src/commands/restore.rs | 14 ++++++------ src/commands/status.rs | 7 +++--- src/commands/switch.rs | 6 +++--- src/lib.rs | 6 ------ src/main.rs | 8 ++++--- src/models/blob.rs | 9 +++++--- src/models/commit.rs | 2 +- src/models/index.rs | 2 +- src/models/tree.rs | 2 +- src/{ => utils}/head.rs | 9 ++++---- src/utils/mod.rs | 2 ++ src/{ => utils}/store.rs | 2 +- tests/test.rs | 46 ---------------------------------------- 18 files changed, 46 insertions(+), 89 deletions(-) delete mode 100644 src/lib.rs rename src/{ => utils}/head.rs (95%) rename src/{ => utils}/store.rs (99%) diff --git a/src/cli.rs b/src/cli.rs index 97af558..99b622a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,5 @@ use clap::{ArgGroup, Parser, Subcommand}; -use mit::commands as cmd; - +use super::commands as cmd; /// Rust实现的简易版本的Git,用于学习Rust语言 #[derive(Parser)] #[command(author, version, about, long_about = None)] diff --git a/src/commands/branch.rs b/src/commands/branch.rs index 5643228..f49d7a5 100644 --- a/src/commands/branch.rs +++ b/src/commands/branch.rs @@ -1,6 +1,9 @@ use colored::Colorize; -use crate::{head, models::*, store, utils::util}; +use crate::{ + models::*, + utils::{head, store, util}, +}; // branch error enum BranchErr { diff --git a/src/commands/commit.rs b/src/commands/commit.rs index b81190a..8981526 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -1,4 +1,4 @@ -use crate::{head, models::*}; +use crate::{models::*, utils::head}; use super::status; @@ -38,7 +38,7 @@ pub fn commit(message: String, allow_empty: bool) { mod test { use std::path::Path; - use crate::{commands as cmd, head, models, utils::util}; + use crate::{commands as cmd, utils::head, models, utils::util}; #[test] #[should_panic] diff --git a/src/commands/log.rs b/src/commands/log.rs index d0063ec..2ca869b 100644 --- a/src/commands/log.rs +++ b/src/commands/log.rs @@ -1,4 +1,4 @@ -use crate::{head, models::Commit}; +use crate::{models::Commit, utils::head}; use colored::Colorize; const DEFAULT_LOG_NUMBER: usize = 10; diff --git a/src/commands/merge.rs b/src/commands/merge.rs index 3192222..95cab54 100644 --- a/src/commands/merge.rs +++ b/src/commands/merge.rs @@ -1,9 +1,7 @@ use crate::{ commands::{self, status::*}, - head, models::{Commit, Hash}, - store::Store, - utils::util, + utils::{head, store, util}, }; enum MergeErr { @@ -67,7 +65,7 @@ pub fn merge(branch: String) { head::get_branch_head(&branch) } else { // Commit Hash, e.g. a1b2c3d4 - let store = Store::new(); + let store = store::Store::new(); let commit = store.search(&branch); if commit.is_none() || !util::is_typeof_commit(commit.clone().unwrap()) { println!("fatal: 非法的 commit hash: '{}'", branch); diff --git a/src/commands/restore.rs b/src/commands/restore.rs index 3b669c4..b680819 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -4,7 +4,10 @@ use std::{ path::PathBuf, }; -use crate::{head, models::*, store::Store, utils::util}; +use crate::{ + models::*, + utils::{head, store, util}, +}; /// 统计[工作区]中相对于target_blobs已删除的文件(根据filters进行过滤) fn get_worktree_deleted_files_in_filters( @@ -64,8 +67,8 @@ pub fn restore_worktree(filter: Option<&Vec>, target_blobs: &Vec<(PathB let mut file_paths = util::integrate_paths(&input_paths); //根据用户输入整合存在的文件(绝对路径) file_paths.extend(deleted_files); //已删除的文件 - let index = Index::get_instance(); - let store = Store::new(); + let index = Index::new(); + let store = store::Store::new(); for path in &file_paths { assert!(path.is_absolute()); // 绝对路径 @@ -165,7 +168,7 @@ pub fn restore(paths: Vec, source: Option, worktree: bool, stage head::get_branch_head(&src) // "" if not exist } else { // [Commit Hash, e.g. a1b2c3d4] || [Wrong Branch Name] - let store = Store::new(); + let store = store::Store::new(); let commit = store.search(&src); if commit.is_none() || !util::is_typeof_commit(commit.clone().unwrap()) { println!("fatal: 非法的 commit hash: '{}'", src); @@ -214,8 +217,7 @@ pub fn restore(paths: Vec, source: Option, worktree: bool, stage mod test { use std::fs; //TODO 写测试! - use crate::commands::status; - use crate::{commands as cmd, models::Index, utils::util}; + use crate::{commands as cmd, commands::status, models::Index, utils::util}; use std::path::PathBuf; #[test] diff --git a/src/commands/status.rs b/src/commands/status.rs index cc78a69..6f15ed2 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -1,6 +1,5 @@ use crate::{ - head, - head::Head, + utils::head, models::{Commit, Index}, utils::util, }; @@ -139,10 +138,10 @@ pub fn changes_to_be_staged() -> Changes { pub fn status() { util::check_repo_exist(); match head::current_head() { - Head::Detached(commit) => { + head::Head::Detached(commit) => { println!("HEAD detached at {}", commit[0..7].to_string()); } - Head::Branch(branch) => { + head::Head::Branch(branch) => { println!("On branch {}", branch); } } diff --git a/src/commands/switch.rs b/src/commands/switch.rs index aa50bc8..b1c70c6 100644 --- a/src/commands/switch.rs +++ b/src/commands/switch.rs @@ -1,9 +1,9 @@ use colored::Colorize; use crate::{ - head::{self}, + utils::head, models::{Commit, Hash}, - store::Store, + utils::store, utils::util, }; @@ -41,7 +41,7 @@ fn switch_to(branch: String, detach: bool) -> Result<(), SwitchErr> { return Err(SwitchErr::NoClean); } - let store = Store::new(); + let store = store::Store::new(); if head::list_local_branches().contains(&branch) { // 切到分支 let branch_commit = head::get_branch_head(&branch); diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 28ecdc9..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,6 +0,0 @@ -// 不使用lib.rs的话,就无法在tests里引用到src中的模块 -pub mod commands; -pub mod head; -pub mod models; -pub mod store; -pub mod utils; diff --git a/src/main.rs b/src/main.rs index 0545096..202dae6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ -use mit::models::Index; - mod cli; +mod commands; +mod models; +mod utils; + fn main() { color_backtrace::install(); // colorize backtrace cli::handle_command(); - Index::get_instance().save(); //兜底save + models::Index::get_instance().save(); //兜底save } diff --git a/src/models/blob.rs b/src/models/blob.rs index aebcd45..e9a2acc 100644 --- a/src/models/blob.rs +++ b/src/models/blob.rs @@ -1,4 +1,7 @@ -use crate::{models::Hash, store::Store, utils::util}; +use crate::{ + models::Hash, + utils::{store, util}, +}; use std::{fs, path::Path}; /**Blob
@@ -21,14 +24,14 @@ impl Blob { } pub fn load(hash: &String) -> Blob { - let s = Store::new(); + let s = store::Store::new(); let data = s.load(hash); Blob { hash: hash.clone(), data } } /// 写入文件;优化:文件已存在时不做操作 pub fn save(&self) { - let s = Store::new(); + let s = store::Store::new(); if !s.contains(&self.hash) { let hash = s.save(&self.data); assert_eq!(hash, self.hash); diff --git a/src/models/commit.rs b/src/models/commit.rs index c42353d..1cb571c 100644 --- a/src/models/commit.rs +++ b/src/models/commit.rs @@ -2,7 +2,7 @@ use std::time::SystemTime; use serde::{Deserialize, Serialize}; -use crate::{store, utils::util}; +use crate::utils::{store, util}; use super::*; /*Commit diff --git a/src/models/index.rs b/src/models/index.rs index 428cf90..2fcdb68 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -55,7 +55,7 @@ pub struct Index { impl Index { /// 从index文件加载 - fn new() -> Index { + pub fn new() -> Index { let mut index = Index::default(); index.load(); return index; diff --git a/src/models/tree.rs b/src/models/tree.rs index b9b3c6e..90e03d7 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, path::PathBuf}; use serde::{Deserialize, Serialize}; -use crate::{store, utils::util}; +use crate::utils::{store, util}; use super::{Hash, Index}; /*Tree diff --git a/src/head.rs b/src/utils/head.rs similarity index 95% rename from src/head.rs rename to src/utils/head.rs index 8e7d1cd..342c658 100644 --- a/src/head.rs +++ b/src/utils/head.rs @@ -113,7 +113,8 @@ pub fn change_head_to_commit(commit_hash: &String) { #[cfg(test)] mod test { - use crate::{head::update_branch, utils::util}; + use crate::utils::util; + use crate::utils::head; #[test] fn test_edit_branch() { @@ -134,8 +135,8 @@ mod test { util::setup_test_with_clean_mit(); let branch_one = "test_branch".to_string() + &rand::random::().to_string(); let branch_two = "test_branch".to_string() + &rand::random::().to_string(); - update_branch(&branch_one, &"1234567890".to_string()); - update_branch(&branch_two, &"1234567890".to_string()); + head::update_branch(&branch_one, &"1234567890".to_string()); + head::update_branch(&branch_two, &"1234567890".to_string()); let branches = super::list_local_branches(); assert!(branches.contains(&branch_one)); @@ -146,7 +147,7 @@ mod test { fn test_change_head_to_branch() { util::setup_test_with_clean_mit(); let branch_name = "test_branch".to_string() + &rand::random::().to_string(); - update_branch(&branch_name, &"1234567890".to_string()); + head::update_branch(&branch_name, &"1234567890".to_string()); super::change_head_to_branch(&branch_name); assert!( match super::current_head() { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 812d1ed..e2a3235 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,3 @@ pub mod util; +pub mod head; +pub mod store; \ No newline at end of file diff --git a/src/store.rs b/src/utils/store.rs similarity index 99% rename from src/store.rs rename to src/utils/store.rs index 0fb52c5..96d3d6b 100644 --- a/src/store.rs +++ b/src/utils/store.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use crate::models::Hash; -use super::utils::util; +use super::util; /// 管理.mit仓库的读写 pub struct Store { diff --git a/tests/test.rs b/tests/test.rs index 71af2aa..e69de29 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,46 +0,0 @@ -use mit::utils::util; -use sha1::{Digest, Sha1}; -use std::fs::File; -use std::io::{BufRead, BufReader, Error, Write}; - -#[test] -fn test_hash() { - let mut hasher = Sha1::new(); - hasher.update(String::from("hello world")); - let result = format!("{:x}", hasher.finalize()); - println!("{}", result); - println!("{}", util::calc_hash(&String::from("hello world"))); -} - -#[test] -fn test_write() -> Result<(), Error> { - util::setup_test_with_clean_mit(); - let path = "lines.txt"; - //create会截断文件 - let mut output = File::create(path)?; // ? 用于传播错误 - write!(output, "Rust\nWrite\nRead4")?; - Ok(()) -} - -#[test] -fn test_read() -> Result<(), Error> { - util::setup_test_with_clean_mit(); - let path = "lines.txt"; - util::ensure_test_file(path.as_ref(), None); - let input = File::open(path)?; - let buffered = BufReader::new(input); - - for line in buffered.lines() { - println!("{}", line?); - } - Ok(()) -} - -#[test] -fn test_string() { - let mut s = String::from("Hello"); - s.push_str(", world!"); - s += "2"; - s.push('!'); - println!("{}", s); -}