From 1a7914109030f8064c4ef20a71d1aac6c0e9564a Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Sat, 30 Dec 2023 13:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96Blob::restore=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/restore.rs | 4 ++-- src/models/blob.rs | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/commands/restore.rs b/src/commands/restore.rs index fcea20d..d96b064 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -75,7 +75,7 @@ pub fn restore_worktree(filter: Option<&Vec>, 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>, 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.未跟踪,保留 diff --git a/src/models/blob.rs b/src/models/blob.rs index 3acdf24..0d56ca7 100644 --- a/src/models/blob.rs +++ b/src/models/blob.rs @@ -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();