diff --git a/src/commands/add.rs b/src/commands/add.rs index b041dc2..2033f3a 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -51,7 +51,7 @@ fn add_a_file(file: &Path, index: &mut Index) { return; } - let rel_path = util::to_cur_relative_path(file); + let rel_path = util::get_relative_path(file); if !file.exists() { //文件被删除 index.remove(file); diff --git a/src/commands/status.rs b/src/commands/status.rs index dafe241..9ea6a3a 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -59,7 +59,7 @@ impl Changes { [&mut change.new, &mut change.modified, &mut change.deleted] .iter_mut() .for_each(|paths| { - **paths = util::map(&**paths, |p| util::get_relative_path(p, &cur_dir)); + **paths = util::map(&**paths, |p| util::get_relative_path_to_dir(p, &cur_dir)); }); change } diff --git a/src/models/index.rs b/src/models/index.rs index 5ba51b7..f0b2aee 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -184,7 +184,7 @@ impl Index { .entries .iter() .map(|(path, value)| { - let relative_path = util::get_relative_path(path, &self.working_dir); + let relative_path = util::get_relative_path_to_dir(path, &self.working_dir); (relative_path, value.clone()) }) .collect(); diff --git a/src/utils/util.rs b/src/utils/util.rs index 3b67156..ea1394a 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -218,7 +218,7 @@ pub fn list_workdir_files() -> Vec { } /// 获取相对于dir的 规范化 相对路径(不包含../ ./) -pub fn get_relative_path(path: &Path, dir: &Path) -> PathBuf { +pub fn get_relative_path_to_dir(path: &Path, dir: &Path) -> PathBuf { // 先统一为绝对路径 let abs_path = if path.is_relative() { get_absolute_path(path) @@ -253,12 +253,12 @@ pub fn get_common_dir(p1: &Path, p2: &Path) -> PathBuf { /// 获取相较于工作区(Working Dir)的相对路径 pub fn to_workdir_relative_path(path: &Path) -> PathBuf { - get_relative_path(path, &get_working_dir().unwrap()) + get_relative_path_to_dir(path, &get_working_dir().unwrap()) } /// 获取相较于当前目录的 规范化 相对路径(不包含../ ./) -pub fn to_cur_relative_path(path: &Path) -> PathBuf { - get_relative_path(path, &cur_dir()) +pub fn get_relative_path(path: &Path) -> PathBuf { + get_relative_path_to_dir(path, &cur_dir()) } /// 获取相较于工作区(Working Dir)的绝对路径 @@ -439,7 +439,7 @@ mod tests { fn test_get_relative_path() { test::setup_with_clean_mit(); let path = Path::new("../../src\\main.rs"); - let rel_path = get_relative_path(&path, &cur_dir()); + let rel_path = get_relative_path_to_dir(&path, &cur_dir()); println!("{:?}", rel_path); assert_eq!(rel_path, path);