fix: 修复add命令检测workdir(而非curDir)中deleted files的问题

This commit is contained in:
mrbeanc
2023-12-22 14:49:52 +08:00
parent 49a7220ad8
commit b9b8d9c704
3 changed files with 12 additions and 6 deletions

View File

@@ -33,7 +33,7 @@ pub fn add(files: Vec<String>, 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<String>, 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 {

View File

@@ -97,10 +97,10 @@ impl Index {
}
/// 与暂存区比较,获取工作区中被删除的文件
pub fn get_deleted_files(&self) -> Vec<PathBuf> {
pub fn get_deleted_files(&self, dir: &Path) -> Vec<PathBuf> {
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());
}
});

View File

@@ -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())