简化Blob::restore调用

This commit is contained in:
mrbeanc
2023-12-30 13:38:19 +08:00
parent ebbc134117
commit 1a79141090
2 changed files with 10 additions and 6 deletions

View File

@@ -75,7 +75,7 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
//文件不存在于workdir
if target_blobs.contains_key(path) {
//文件存在于target_commit (deleted),需要恢复
Blob::load(&target_blobs[path]).restore(&path);
Blob::restore(&target_blobs[path], &path);
} else {
//在target_commit和workdir中都不存在(非法路径) 用户输入
println!("fatal: pathspec '{}' did not match any files", path.display());
@@ -86,7 +86,7 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
//文件已修改(modified)
let file_hash = util::calc_file_hash(&path); //TODO tree没有存修改时间所以这里只能用hash判断
if file_hash != target_blobs[path] {
Blob::load(&target_blobs[path]).restore(&path);
Blob::restore(&target_blobs[path], &path);
}
} else {
//新文件也分两种情况1.已跟踪,需要删除 2.未跟踪,保留

View File

@@ -24,18 +24,22 @@ impl Blob {
}
/// 通过hash值加载blob从/objects/
#[allow(dead_code)]
pub fn load(hash: &String) -> Blob {
pub fn load(hash: &Hash) -> Blob {
let s = Store::new();
let data = s.load(hash);
Blob { hash: hash.clone(), data }
}
///将hash对应的blob还原到file
pub fn restore(&self, file: &Path) {
///blob还原到file
pub fn restore_to_file(&self, file: &Path) {
util::write(file, &self.data).unwrap();
}
/// 将hash对应的blob还原到file, static方法对[Blob::restore_to_file]的封装
pub fn restore(hash: &Hash, file: &Path) {
Blob::load(hash).restore_to_file(file);
}
/// 写入文件;优化:文件已存在时不做操作
pub fn save(&self) {
let s = Store::new();