From 49a7220ad851771c7b18b3dc1894dea7559095e6 Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Fri, 22 Dec 2023 14:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=8B=E5=8A=A8=E5=AE=9E=E7=8E=B0=E7=9B=B8?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E5=BE=84->=E7=BB=9D=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=EF=BC=8C=E8=A7=A3=E5=86=B3add=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E6=96=87=E4=BB=B6panic=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 | 1 - src/utils/util.rs | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index 3211245..c8a42dc 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -58,7 +58,6 @@ pub fn add(files: Vec, all: bool, mut update: bool) { } fn add_a_file(file: &Path, index: &mut Index) { - //TODO 文件不存在会报错 if !is_inside_workdir(file) && file.exists() { //文件不在工作区内 println!("fatal: '{}' is outside repository at '{}'", file.display(), get_working_dir().unwrap().display()); diff --git a/src/utils/util.rs b/src/utils/util.rs index 6b62da7..7bf2f24 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -251,8 +251,8 @@ pub fn get_file_mode(path: &Path) -> String { } } -/// 清除Windows下的绝对路径前缀"\\\\?\\" -/// Windows 系统中的文件路径格式 +/// 清除Windows下的绝对路径前缀"\\\\?\\" (由[PathBuf::canonicalize]函数产生) +///
Windows 系统中的文件路径格式 pub fn clean_win_abs_path_pre(path: PathBuf) -> PathBuf { #[cfg(windows)] { @@ -270,14 +270,28 @@ pub fn clean_win_abs_path_pre(path: PathBuf) -> PathBuf { } } -/// 获取绝对路径(相对于当前current_dir) +/// 获取绝对路径(相对于目录current_dir) 不论是否存在 pub fn get_absolute_path(path: &Path) -> PathBuf { - //TODO 不能处理不存在的文件 if path.is_absolute() { path.to_path_buf() } else { - let abs_path = path.canonicalize().unwrap(); //这一步会统一路径分隔符 - clean_win_abs_path_pre(abs_path) + /*let abs_path = path.canonicalize().unwrap(); //这一步会统一路径分隔符 //canonicalize()不能处理不存在的文件 + clean_win_abs_path_pre(abs_path)*/ + // 所以决定手动解析相对路径中的../ ./ + let mut abs_path = std::env::current_dir().unwrap(); //cur_dir + for component in path.components() { + match component { + std::path::Component::ParentDir => { + if !abs_path.pop() { + panic!("relative path parse error"); + } + } + std::path::Component::Normal(part) => abs_path.push(part), + std::path::Component::CurDir => {} + _ => {} + } + } + abs_path } } @@ -323,6 +337,19 @@ mod tests { } } + #[test] + fn test_get_absolute_path() { + let path = Path::new("mit_test_storage/../src/main.rs"); + let abs_path = get_absolute_path(path); + println!("{:?}", abs_path); + + let mut cur_dir = std::env::current_dir().unwrap(); + cur_dir.push("mit_test_storage"); + cur_dir.pop(); + cur_dir.push("src/main.rs"); + assert_eq!(abs_path, cur_dir); + } + #[test] fn test_is_inside_repo() { setup_test_with_mit();