From 801fbab720e1bb94c32bb1aa10966a0637a10e63 Mon Sep 17 00:00:00 2001 From: ccQpein Date: Fri, 30 Sep 2022 05:03:33 -0400 Subject: [PATCH] fix(directory): don't strip duplicate directory names twice (#4295) * Fix directory issue in a special case * add None test --- src/modules/directory.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/modules/directory.rs b/src/modules/directory.rs index be778bb0..ca703ab7 100644 --- a/src/modules/directory.rs +++ b/src/modules/directory.rs @@ -111,7 +111,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { || ((num_segments_after_root - 1) as i64) < config.truncation_length { let root = repo_path_vec[0]; - let before = dir_string.replace(&contracted_path, ""); + let before = before_root_dir(&dir_string, &contracted_path); [prefix + before.as_str(), root.to_string(), after_repo_root] } else { ["".to_string(), "".to_string(), prefix + dir_string.as_str()] @@ -339,6 +339,14 @@ fn convert_path_sep(path: &str) -> String { return PathBuf::from_slash(path).to_string_lossy().into_owned(); } +/// Get the path before the git repo root by trim the most right repo name. +fn before_root_dir(path: &str, repo: &str) -> String { + match path.rsplit_once(repo) { + Some((a, _)) => a.to_string(), + None => path.to_string(), + } +} + #[cfg(test)] mod tests { use super::*; @@ -1788,4 +1796,22 @@ mod tests { assert_eq!(expected, actual); tmp_dir.close() } + + #[test] + fn parent_and_sub_git_repo_are_in_same_name_folder() { + assert_eq!( + before_root_dir("~/user/gitrepo/gitrepo", "gitrepo"), + "~/user/gitrepo/".to_string() + ); + + assert_eq!( + before_root_dir("~/user/gitrepo-diff/gitrepo", "gitrepo"), + "~/user/gitrepo-diff/".to_string() + ); + + assert_eq!( + before_root_dir("~/user/gitrepo-diff/gitrepo", "aaa"), + "~/user/gitrepo-diff/gitrepo".to_string() + ); + } }