优化add逻辑

This commit is contained in:
mrbeanc
2023-12-20 11:58:40 +08:00
parent ee0360fa66
commit 5d5187a91c
2 changed files with 36 additions and 7 deletions

View File

@@ -50,13 +50,17 @@ fn add_a_file(file: &Path, index: &mut Index) {
println!("add a file: {}", get_relative_path(file, get_working_dir().unwrap()).display());
if !file.exists() { //文件被删除
index.remove(file);
} else {
let blob = Blob::new(file);
let file_data = FileMetaData::new(&blob, file);
} else { //文件存在
if !index.contains(file) { //文件未被跟踪
index.add(file.to_path_buf(), file_data);
let blob = Blob::new(file);
index.add(file.to_path_buf(), FileMetaData::new(&blob, file));
} else { //文件已被跟踪,可能被修改
index.update(file.to_path_buf(), file_data);
if index.is_modified(file) { //文件被修改,但不一定内容更改
let blob = Blob::new(file); //到这一步才创建blob是为了优化
if !index.verify_hash(file, &blob.get_hash()) { //比较hash 确认内容更改
index.update(file.to_path_buf(), FileMetaData::new(&blob, file));
}
}
}
}
}