@@ -17,13 +17,18 @@ pub fn get_git_relative_path<P>(abs_path: P) -> PathBuf
17
17
where
18
18
P : AsRef < Path > ,
19
19
{
20
- let abs_path = abs_path. as_ref ( ) ;
21
- match abs_path
22
- . canonicalize ( )
23
- . and_then ( |p| get_parent_git_repo_path ( & p) )
24
- {
25
- Ok ( repo_path) => abs_path. strip_prefix ( repo_path) . unwrap ( ) . to_path_buf ( ) ,
26
- Err ( _) => abs_path. to_path_buf ( ) ,
20
+ if let Ok ( canonicalized_abs_path) = abs_path. as_ref ( ) . canonicalize ( ) {
21
+ // `repo_path` is still canonicalized as it is a subpath of `canonicalized_abs_path`
22
+ if let Ok ( repo_path) = get_parent_git_repo_path ( & canonicalized_abs_path) {
23
+ canonicalized_abs_path
24
+ . strip_prefix ( repo_path)
25
+ . expect ( "Repository path is malformed." )
26
+ . to_path_buf ( )
27
+ } else {
28
+ canonicalized_abs_path
29
+ }
30
+ } else {
31
+ abs_path. as_ref ( ) . to_path_buf ( )
27
32
}
28
33
}
29
34
@@ -62,6 +67,18 @@ mod tests {
62
67
assert_eq ! ( relative_path, path_dir. canonicalize( ) . unwrap( ) ) ;
63
68
}
64
69
70
+ #[ test]
71
+ fn test_get_git_relative_path_not_found_with_symlink ( ) {
72
+ let dir = tempdir ( ) . unwrap ( ) ;
73
+ let path_dir = dir. path ( ) . join ( "folder" ) ;
74
+ fs:: create_dir_all ( & path_dir) . unwrap ( ) ;
75
+ let symlink = dir. path ( ) . join ( "symlink" ) ;
76
+ std:: os:: unix:: fs:: symlink ( & path_dir, & symlink) . unwrap ( ) ;
77
+
78
+ let relative_path = get_git_relative_path ( & symlink) ;
79
+ assert_eq ! ( relative_path, symlink. canonicalize( ) . unwrap( ) ) ;
80
+ }
81
+
65
82
#[ test]
66
83
fn test_get_formated_function_path ( ) {
67
84
let input = "std :: vec :: Vec :: new" ;
0 commit comments