@@ -29,33 +29,32 @@ def _relative_file(to_file, frm_file):
2929
3030 parent_count = to_parent_count - frm_parent_count
3131
32- to_segments = _spaths .normalize (_spaths . join ("/" , to_file ) ).split ("/" )[:- 1 ]
33- frm_segments = _spaths .normalize (_spaths . join ("/" , frm_file ) ).split ("/" )[:- 1 ]
32+ to_segments = _spaths .normalize (to_file if to_file . startswith ("/" ) else "/" + to_file ).split ("/" )[:- 1 ]
33+ frm_segments = _spaths .normalize (frm_file if frm_file . startswith ("/" ) else "/" + frm_file ).split ("/" )[:- 1 ]
3434
35- if len (to_segments ) == 0 and len (frm_segments ) == 0 :
35+ to_segments_len = len (to_segments )
36+ frm_segments_len = len (frm_segments )
37+
38+ if to_segments_len == 0 and frm_segments_len == 0 :
3639 return to_file
3740
3841 # since we prefix a "/" and normalize, the first segment is always "". So split point will be at least 1
3942 split_point = 1
4043
4144 # if either of the paths starts with ../ then assume that any shared paths are a coincidence
42- if to_segments [0 ] != ".." and frm_segments != ".." :
43- longest_common = []
44- for to_seg , frm_seg in zip (to_segments , frm_segments ):
45- if to_seg == frm_seg :
46- longest_common .append (to_seg )
47- else :
45+ if to_segments [0 ] != ".." and frm_segments [0 ] != ".." :
46+ i = 0
47+ for _ in to_segments if to_segments_len <= frm_segments_len else frm_segments :
48+ if to_segments [i ] != frm_segments [i ]:
4849 break
50+ i += 1
51+ split_point = i
4952
50- split_point = len (longest_common )
53+ segments = [".." ] * (frm_segments_len - split_point + parent_count )
54+ segments .extend (to_segments [split_point :])
55+ segments .append (to_file [to_file .rfind ("/" ) + 1 :])
5156
52- return _spaths .join (
53- * (
54- [".." ] * (len (frm_segments ) - split_point + parent_count ) +
55- to_segments [split_point :] +
56- [_spaths .basename (to_file )]
57- )
58- )
57+ return "/" .join (segments )
5958
6059def _to_output_relative_path (file ):
6160 """
@@ -127,7 +126,7 @@ def _to_repository_relative_path(file):
127126 """
128127
129128 if file .short_path .startswith ("../" ):
130- return "/" . join ( file .short_path .split ("/" )[ 2 :])
129+ return file . short_path [ file .short_path .find ("/" , 3 ) + 1 :]
131130 else :
132131 return file .short_path
133132
0 commit comments