diff --git a/src/cli.rs b/src/cli.rs index 5985c70..9e08840 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -60,10 +60,10 @@ pub fn handle_command() { Command::Add { files, all, update } => { add(files, all, update); } - Command::Rm { files, cached, recursive} => { + Command::Rm { files, cached, recursive } => { remove(files, cached, recursive).expect("删除失败"); } - Command::Commit { message, allow_empty, } => { + Command::Commit { message, allow_empty } => { commit(message, allow_empty); } } diff --git a/src/commands/add.rs b/src/commands/add.rs index f009c2c..fc032c1 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -1,9 +1,11 @@ +use std::env; +use std::path::{Path, PathBuf}; + +use colored::Colorize; + use crate::models::blob::Blob; use crate::models::index::{FileMetaData, Index}; use crate::utils::util::{check_repo_exist, get_relative_path, get_working_dir, list_files}; -use colored::Colorize; -use std::env; -use std::path::{Path, PathBuf}; // TODO: fatal: ../moj/app.py: '../moj/app.py' is outside repository at 'Git-Rust' pub fn add(files: Vec, all: bool, mut update: bool) { @@ -19,6 +21,7 @@ pub fn add(files: Vec, all: bool, mut update: bool) { dot = false; update = false; } else if update { + // update 优先级次之 dot = false; } @@ -29,9 +32,9 @@ pub fn add(files: Vec, all: bool, mut update: bool) { println!("{}", "'.'代表了当前目录".bright_green()); env::current_dir().unwrap() } else { - panic!("不应该运行到这里"); + panic!(); }; - + println!("Working on [{}]\n", path.to_str().unwrap().bright_blue()); files = list_files(&path).unwrap(); if update { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 852962e..6732aa1 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,5 +1,5 @@ -pub mod init; pub mod add; pub mod commit; +pub mod init; +pub mod remove; pub mod status; -pub mod remove; \ No newline at end of file diff --git a/src/commands/remove.rs b/src/commands/remove.rs index 765d9df..b2b16d0 100644 --- a/src/commands/remove.rs +++ b/src/commands/remove.rs @@ -1,9 +1,9 @@ -use std::{fs, io}; -use std::path::PathBuf; -use colored::Colorize; use crate::models::index::Index; use crate::utils::util; use crate::utils::util::check_repo_exist; +use colored::Colorize; +use std::path::PathBuf; +use std::{fs, io}; /// 从暂存区&|工作区删除文件 pub fn remove(files: Vec, cached: bool, recursive: bool) -> io::Result<()> { @@ -15,7 +15,8 @@ pub fn remove(files: Vec, cached: bool, recursive: bool) -> io::Result<( println!("Warning: {} not exist", file.red()); continue; } - if !index.contains(&path) { //不能删除未跟踪的文件 + if !index.contains(&path) { + //不能删除未跟踪的文件 println!("Warning: {} not tracked", file.red()); continue; } @@ -41,4 +42,4 @@ pub fn remove(files: Vec, cached: bool, recursive: bool) -> io::Result<( println!("removed [{}]", file.bright_green()); } Ok(()) -} \ No newline at end of file +} diff --git a/src/head.rs b/src/head.rs index c85ac8a..84fba17 100644 --- a/src/head.rs +++ b/src/head.rs @@ -25,10 +25,8 @@ fn update_branch_head(branch_name: &String, commit_hash: &String) { branch.push("refs"); branch.push("heads"); branch.push(branch_name); - std::fs::write(&branch, commit_hash).expect(&format!( - "无法写入branch in {:?} with {}", - branch, commit_hash - )); + std::fs::write(&branch, commit_hash) + .expect(&format!("无法写入branch in {:?} with {}", branch, commit_hash)); } fn get_branch_head(branch_name: &String) -> String { diff --git a/src/lib.rs b/src/lib.rs index 5f577c9..493a380 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ // 不使用lib.rs的话,就无法在tests里引用到src中的模块 -pub mod models; -pub mod utils; pub mod commands; +mod head; +pub mod models; mod store; -mod head; \ No newline at end of file +pub mod utils; diff --git a/src/models/blob.rs b/src/models/blob.rs index 98e57f0..e24f566 100644 --- a/src/models/blob.rs +++ b/src/models/blob.rs @@ -27,10 +27,7 @@ impl Blob { pub fn load(hash: &String) -> Blob { let s = Store::new(); let data = s.load(hash); - Blob { - hash: hash.clone(), - data, - } + Blob { hash: hash.clone(), data } } /// 写入文件;优化:文件已存在时不做操作 diff --git a/src/models/index.rs b/src/models/index.rs index 203375b..461a63a 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -2,12 +2,12 @@ use crate::models::blob::Blob; use crate::models::object::Hash; use crate::utils::util; use crate::utils::util::get_relative_path; +use colored::Colorize; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs; use std::path::{Path, PathBuf}; use std::time::SystemTime; -use colored::Colorize; // 文件元数据结构 #[derive(Serialize, Deserialize, Debug, Clone)] @@ -85,11 +85,6 @@ impl Index { &self.get_hash(file).unwrap_or_default() == hash } - // 获取所有文件元数据 - fn get_all(&self) -> &HashMap { - &self.entries - } - pub fn contains(&self, path: &Path) -> bool { let path = Index::preprocess_path(path); self.entries.contains_key(&path) diff --git a/src/models/mod.rs b/src/models/mod.rs index 3a27bc2..5691ebb 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,5 +1,5 @@ -pub mod commit; pub mod blob; -pub mod tree; +pub mod commit; +pub mod index; pub mod object; -pub mod index; \ No newline at end of file +pub mod tree; diff --git a/src/models/object.rs b/src/models/object.rs index ff70033..fe39df9 100644 --- a/src/models/object.rs +++ b/src/models/object.rs @@ -1 +1 @@ -pub type Hash = String; \ No newline at end of file +pub type Hash = String; diff --git a/src/models/tree.rs b/src/models/tree.rs index 310d336..6f8fe39 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -38,10 +38,7 @@ fn store_path_to_tree(path_entries: &Vec, current_root: PathBuf) -> Tre }; entry }; - let mut tree = Tree { - hash: "".to_string(), - entries: Vec::new(), - }; + let mut tree = Tree { hash: "".to_string(), entries: Vec::new() }; let mut processed_path: HashMap = HashMap::new(); for path in path_entries.iter() { // 判断是不是直接在根目录下 @@ -149,10 +146,7 @@ impl Tree { sub_blobs .iter() .map(|(path, blob_hash)| { - ( - PathBuf::from(entry.name.clone()).join(path), - blob_hash.clone(), - ) + (PathBuf::from(entry.name.clone()).join(path), blob_hash.clone()) }) .collect::>() .as_mut(), @@ -177,14 +171,8 @@ mod test { for test_file in vec!["b.txt", "mit_src/a.txt"] { let test_file = PathBuf::from(test_file); util::ensure_test_file(&test_file, None); - index.add( - test_file.clone(), - FileMetaData::new(&Blob::new(&test_file), &test_file), - ); - index.add( - test_file.clone(), - FileMetaData::new(&Blob::new(&test_file), &test_file), - ); + index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); + index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } let tree = super::Tree::new(&index); @@ -200,10 +188,7 @@ mod test { for test_file in test_files.clone() { let test_file = PathBuf::from(test_file); util::ensure_test_file(&test_file, None); - index.add( - test_file.clone(), - FileMetaData::new(&Blob::new(&test_file), &test_file), - ); + index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } let tree = super::Tree::new(&index); @@ -223,10 +208,7 @@ mod test { for test_file in test_files.clone() { let test_file = PathBuf::from(test_file); util::ensure_test_file(&test_file, None); - index.add( - test_file.clone(), - FileMetaData::new(&Blob::new(&test_file), &test_file), - ); + index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } let tree = super::Tree::new(&index); @@ -250,10 +232,7 @@ mod test { util::ensure_test_file(&test_file, None); let blob = Blob::new(&test_file); test_blobs.push(blob.clone()); - index.add( - test_file.clone(), - FileMetaData::new(&Blob::new(&test_file), &test_file), - ); + index.add(test_file.clone(), FileMetaData::new(&Blob::new(&test_file), &test_file)); } let tree = super::Tree::new(&index); diff --git a/src/rustfmt.toml b/src/rustfmt.toml new file mode 100644 index 0000000..608dcd4 --- /dev/null +++ b/src/rustfmt.toml @@ -0,0 +1,20 @@ +# Run rustfmt with this config (it should be picked up automatically). +version = "Two" +use_small_heuristics = "Max" +max_width = 100 +struct_lit_width = 60 +chain_width = 60 +single_line_if_else_max_width = 60 +single_line_let_else_max_width = 60 + +merge_derives = true +reorder_imports = true +# 格式化注释代码块:Unstable +format_code_in_doc_comments = true + + +# 彩色输出:Unstable +color = "Auto" +ignore = [ + "tests", +] \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 929d03a..812d1ed 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1 @@ -pub mod util; \ No newline at end of file +pub mod util; diff --git a/tests/test.rs b/tests/test.rs index e426bd2..36c2367 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,7 +1,7 @@ -use sha1::{Sha1, Digest}; -use std::fs::File; -use std::io::{Write, BufReader, BufRead, Error}; use mit::utils::util; +use sha1::{Digest, Sha1}; +use std::fs::File; +use std::io::{BufRead, BufReader, Error, Write}; #[test] fn test_hash() { @@ -42,4 +42,4 @@ fn test_string() { s += "2"; s.push('!'); println!("{}", s); -} \ No newline at end of file +}