From 4e1e13f71ecbd97e2c6509dec6dbc449163af39c Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Thu, 28 Dec 2023 13:45:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4Index=E6=9E=90=E6=9E=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BF=9D=E5=AD=98=EF=BC=8C=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E6=8E=A7=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=89=8B?= =?UTF-8?q?=E5=8A=A8+main=E7=BB=93=E6=9D=9F=E8=87=AA=E5=8A=A8save?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/add.rs | 7 ++++--- src/commands/commit.rs | 1 + src/commands/remove.rs | 1 + src/commands/restore.rs | 4 ++-- src/main.rs | 3 +++ src/models/index.rs | 40 ++++++++++++++++++++-------------------- src/utils/util.rs | 2 +- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index 2a1b8a0..d3aeea6 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -31,12 +31,14 @@ pub fn add(raw_paths: Vec, all: bool, mut update: bool) { println!("{}", "--update 只对已跟踪文件进行操作 不包含new".bright_green()); } + let index = Index::get_instance(); for file in &files { - add_a_file(file); + add_a_file(file, index); } + index.save(); } -fn add_a_file(file: &Path) { +fn add_a_file(file: &Path, index: &mut Index) { let workdir = util::get_working_dir().unwrap(); if !util::is_sub_path(file, &workdir) { //文件不在工作区内 @@ -49,7 +51,6 @@ fn add_a_file(file: &Path) { return; } - let index = Index::get_instance(); let rel_path = util::to_cur_relative_path(file); if !file.exists() { //文件被删除 diff --git a/src/commands/commit.rs b/src/commands/commit.rs index de4a43c..b81190a 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -31,6 +31,7 @@ pub fn commit(message: String, allow_empty: bool) { } println!("commit hash: {:?}", commit_hash); + index.save(); } #[cfg(test)] diff --git a/src/commands/remove.rs b/src/commands/remove.rs index 2ff50b6..83ba5da 100644 --- a/src/commands/remove.rs +++ b/src/commands/remove.rs @@ -38,5 +38,6 @@ pub fn remove(files: Vec, cached: bool, recursive: bool) -> io::Result<( } println!("removed [{}]", file.bright_green()); } + index.save(); Ok(()) } diff --git a/src/commands/restore.rs b/src/commands/restore.rs index 6a5a1a3..3b669c4 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -136,6 +136,7 @@ pub fn restore_index(filter: Option<&Vec>, target_blobs: &Vec<(PathBuf, } } } + index.save(); } /** 对于工作区中的新文件,若已跟踪,则删除;若未跟踪,则保留
@@ -224,8 +225,7 @@ mod test { util::ensure_no_file(&path); cmd::add(vec![], true, false); //add -A cmd::restore(vec![".".to_string()], Some("HEAD".to_string()), false, true); - let index = Index::get_instance(); - assert!(index.get_tracked_files().is_empty()); + assert!(Index::get_instance().is_empty()); } #[test] diff --git a/src/main.rs b/src/main.rs index 20a6322..0545096 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ +use mit::models::Index; + mod cli; fn main() { color_backtrace::install(); // colorize backtrace cli::handle_command(); + Index::get_instance().save(); //兜底save } diff --git a/src/models/index.rs b/src/models/index.rs index 4d973ca..428cf90 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -56,10 +56,7 @@ pub struct Index { impl Index { /// 从index文件加载 fn new() -> Index { - let mut index = Index { - entries: HashMap::new(), - working_dir: util::get_working_dir().unwrap(), - }; + let mut index = Index::default(); index.load(); return index; } @@ -70,16 +67,10 @@ impl Index { unsafe { &mut INSTANCE } } - /// 重置index,主要用于测试,防止单例模式的影响 - pub fn reset() { + /// 重置index 从文件重新加载,主要用于测试,防止单例模式的影响 + pub fn reload() { let index = Index::get_instance(); - index.clear(); - *index = Index::new(); //drop happened 导致旧数据写入文件 - } - - fn clear(&mut self) { - self.entries.clear(); - self.working_dir.clear(); + index.load(); } /// 预处理路径,统一形式为绝对路径 @@ -159,6 +150,9 @@ impl Index { /// 从index文件加载数据 fn load(&mut self) { + self.entries.clear(); + self.working_dir = util::get_working_dir().unwrap(); + let path = Index::get_path(); if path.exists() { let json = fs::read_to_string(path).expect("无法读取index"); @@ -206,14 +200,9 @@ impl Index { pub fn get_tracked_entries(&self) -> HashMap { self.entries.clone() } -} -/// 析构自动保存 -impl Drop for Index { - fn drop(&mut self) { - //TODO! 优化为只有在修改后才保存 - self.save(); - // println!("{}", "Index auto saved".bright_green()); + pub fn is_empty(&self) -> bool { + self.entries.is_empty() } } @@ -253,4 +242,15 @@ mod tests { index.save(); println!("{:?}", index.entries); } + + #[test] + fn test_save_load() { + util::setup_test_with_empty_workdir(); + let index = Index::get_instance(); + let path = PathBuf::from(".mit/HEAD"); + index.add(path.clone(), FileMetaData::new(&Blob::new(&path), &path)); + assert!(Index::new().is_empty()); //未保存前,新读取的index应该是空的 + index.save(); + assert!(!Index::new().is_empty()); //保存后,新读取的index不是空的 + } } diff --git a/src/utils/util.rs b/src/utils/util.rs index 9013d9b..e00e244 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -47,7 +47,7 @@ fn setup_test_env() { pub fn init_mit() { let _ = crate::commands::init(); - Index::reset(); // 重置index, 以防止其他测试修改了index单例 + Index::reload(); // 重置index, 以防止其他测试修改了index单例 } /// with 初始化的干净的mit