mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-02-03 10:14:13 +08:00
删除Index析构自动保存,时机不可控,改为手动+main结束自动save
This commit is contained in:
@@ -31,12 +31,14 @@ pub fn add(raw_paths: Vec<String>, 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() {
|
||||
//文件被删除
|
||||
|
||||
@@ -31,6 +31,7 @@ pub fn commit(message: String, allow_empty: bool) {
|
||||
}
|
||||
|
||||
println!("commit hash: {:?}", commit_hash);
|
||||
index.save();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -38,5 +38,6 @@ pub fn remove(files: Vec<String>, cached: bool, recursive: bool) -> io::Result<(
|
||||
}
|
||||
println!("removed [{}]", file.bright_green());
|
||||
}
|
||||
index.save();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -136,6 +136,7 @@ pub fn restore_index(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathBuf,
|
||||
}
|
||||
}
|
||||
}
|
||||
index.save();
|
||||
}
|
||||
/**
|
||||
对于工作区中的新文件,若已跟踪,则删除;若未跟踪,则保留<br>
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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<PathBuf, FileMetaData> {
|
||||
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不是空的
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user