From b9b8d9c704e3b4a6864f92ccafe85c2f185ad76d Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Fri, 22 Dec 2023 14:49:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dadd=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=A3=80=E6=B5=8Bworkdir(=E8=80=8C=E9=9D=9EcurDir)=E4=B8=ADdel?= =?UTF-8?q?eted=20files=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/add.rs | 8 ++++---- src/models/index.rs | 4 ++-- src/utils/util.rs | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index c8a42dc..c082882 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -33,7 +33,7 @@ pub fn add(files: Vec, all: bool, mut update: bool) { dot = false; } - let path = if all || update { + let dir = if all || update { println!("{}", "--all || --update 运行于工作区目录".bright_green()); get_working_dir().unwrap() } else if dot { @@ -43,13 +43,13 @@ pub fn add(files: Vec, all: bool, mut update: bool) { panic!(); }; - println!("Working on [{}]\n", path.to_str().unwrap().bright_blue()); - files = list_files(&path).unwrap(); + println!("Working on [{}]\n", dir.to_str().unwrap().bright_blue()); + files = list_files(&dir).unwrap(); if update { files.retain(|file| index.contains(file)); } - files.extend(index.get_deleted_files()); //包含已删除的文件 + files.extend(index.get_deleted_files(&dir)); //包含已删除的文件 } for file in &files { diff --git a/src/models/index.rs b/src/models/index.rs index 9d88db1..0612e0e 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -97,10 +97,10 @@ impl Index { } /// 与暂存区比较,获取工作区中被删除的文件 - pub fn get_deleted_files(&self) -> Vec { + pub fn get_deleted_files(&self, dir: &Path) -> Vec { let mut files = Vec::new(); self.entries.keys().for_each(|file| { - if !file.exists() { + if !file.exists() && util::is_parent_dir(file, dir) { files.push(file.clone()); } }); diff --git a/src/utils/util.rs b/src/utils/util.rs index 7bf2f24..e242b15 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -144,6 +144,12 @@ pub fn is_inside_dir(file: &Path, dir: &Path) -> bool { } } +/// 检测dir是否是file的父目录 (不论文件是否存在) +pub fn is_parent_dir(file: &Path, dir: &Path) -> bool { + let file = get_absolute_path(file); + file.starts_with(dir) +} + /// 检查文件是否在工作区内, 若不存在则false pub fn is_inside_workdir(file: &Path) -> bool { is_inside_dir(file, &get_working_dir().unwrap())