diff --git a/src/commands/add.rs b/src/commands/add.rs index bf920ab..f9598ed 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -1,11 +1,11 @@ -use std::env; -use std::path::{Path, PathBuf}; -use colored::Colorize; -use sha1::Digest; 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) { check_repo_exist(); let mut index = Index::new(); @@ -13,8 +13,9 @@ pub fn add(files: Vec, all: bool, mut update: bool) { let mut files: Vec = files.into_iter().map(PathBuf::from).collect(); - if dot || all || update{ - if all { // all 优先级最高 + if dot || all || update { + if all { + // all 优先级最高 dot = false; update = false; } else if update { @@ -31,9 +32,7 @@ pub fn add(files: Vec, all: bool, mut update: bool) { println!("Working on [{}]\n", path.to_str().unwrap().bright_blue()); files = list_files(&path).unwrap(); if update { - files.retain(|file|{ - index.contains(file) - }); + files.retain(|file| index.contains(file)); } files.extend(index.get_deleted_files()); //包含已删除的文件 @@ -47,22 +46,28 @@ pub fn add(files: Vec, all: bool, mut update: bool) { fn add_a_file(file: &Path, index: &mut Index) { let relative_path = get_relative_path(file, &get_working_dir().unwrap()); - if !file.exists() { //文件被删除 + if !file.exists() { + //文件被删除 index.remove(file); println!("removed: {}", relative_path.display()); - } else { //文件存在 - if !index.contains(file) { //文件未被跟踪 + } else { + //文件存在 + if !index.contains(file) { + //文件未被跟踪 let blob = Blob::new(file); index.add(file.to_path_buf(), FileMetaData::new(&blob, file)); println!("add(stage): {}", relative_path.display()); - } else { //文件已被跟踪,可能被修改 - if index.is_modified(file) { //文件被修改,但不一定内容更改 + } else { + //文件已被跟踪,可能被修改 + if index.is_modified(file) { + //文件被修改,但不一定内容更改 let blob = Blob::new(file); //到这一步才创建blob是为了优化 - if !index.verify_hash(file, &blob.get_hash()) { //比较hash 确认内容更改 + if !index.verify_hash(file, &blob.get_hash()) { + //比较hash 确认内容更改 index.update(file.to_path_buf(), FileMetaData::new(&blob, file)); println!("add(modified): {}", relative_path.display()); } } } } -} \ No newline at end of file +} diff --git a/src/commands/status.rs b/src/commands/status.rs index a16e793..8f80af4 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -22,7 +22,7 @@ pub fn changes_to_be_committed() -> Changes { let tracked_files = index .get_tracked_files() .iter() - .map(|f| util::to_root_relative_path(f)) + .map(|f| util::to_workdir_relative_path(f)) .collect::>(); if head_hash == "" { // 初始提交 @@ -43,7 +43,7 @@ pub fn changes_to_be_committed() -> Changes { change.modified.push(tree_file.clone()); } } else { - change.deleted.push(tree_file.clone()); //todo: abs_path? + change.deleted.push(tree_file.clone()); } } for index_file in index_files.iter() { diff --git a/src/models/tree.rs b/src/models/tree.rs index 1671c68..310d336 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -91,7 +91,7 @@ impl Tree { let file_entries: Vec = index .get_tracked_files() .iter_mut() - .map(|file| util::to_root_relative_path(file)) + .map(|file| util::to_workdir_relative_path(file)) .collect(); store_path_to_tree(&file_entries, "".to_string().into()) diff --git a/src/utils/util.rs b/src/utils/util.rs index cf56731..4dcda84 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -153,7 +153,6 @@ pub fn list_files(path: &Path) -> io::Result> { } /// 获取相对于dir的相对路径 -/// XXX 是否只能用在windows?是否检查dir是否是path的父目录? pub fn get_relative_path(path: &Path, dir: &Path) -> PathBuf { let path = if path.is_relative() { get_absolute_path(path) @@ -165,7 +164,7 @@ pub fn get_relative_path(path: &Path, dir: &Path) -> PathBuf { } /// 获取相较于工作区(Working Dir)的相对路径 -pub fn to_root_relative_path(path: &Path) -> PathBuf { //todo: rename +pub fn to_workdir_relative_path(path: &Path) -> PathBuf { get_relative_path(path, &get_working_dir().unwrap()) }