@@ -43,6 +43,23 @@ export function headCommit(repoRoot: string): string {
4343 return git ( repoRoot , [ 'rev-parse' , 'HEAD' ] ) . toString ( 'utf8' ) . trim ( ) ;
4444}
4545
46+ type NameStatusRecord = Readonly < { renamed : boolean ; from : string ; to : string } > ;
47+
48+ /** Parses `--name-status -z` output; a path that was not renamed has the same `from` and `to`. */
49+ function nameStatusRecords ( output : Buffer ) : NameStatusRecord [ ] {
50+ const fields = output . toString ( 'utf8' ) . split ( '\0' ) ;
51+ const records : NameStatusRecord [ ] = [ ] ;
52+ for ( let index = 0 ; index < fields . length ; ) {
53+ const kind = fields [ index ++ ] ?. trim ( ) ;
54+ if ( ! kind ) continue ;
55+ const renamed = kind . startsWith ( 'R' ) ;
56+ const from = fields [ index ++ ] ;
57+ const to = renamed ? fields [ index ++ ] : from ;
58+ if ( from && to ) records . push ( { renamed, from, to } ) ;
59+ }
60+ return records ;
61+ }
62+
4663/**
4764 * Head path -> base path. Follow committed moves before the aggregate diff, so later edits do not
4865 * erase a recorded rename. Added or deleted paths end that lineage. The aggregate diff still finds
@@ -57,24 +74,14 @@ export function renamedSince(
5774 const ended = new Set < string > ( ) ;
5875 const flags = [ '--name-status' , '-M' , '--diff-filter=ADR' , '-z' ] ;
5976 const follow = ( status : Buffer ) : void => {
60- const fields = status . toString ( 'utf8' ) . split ( '\0' ) ;
61- for ( let index = 0 ; index < fields . length ; ) {
62- const kind = fields [ index ++ ] ?. trim ( ) ;
63- if ( ! kind ) continue ;
64- const from = fields [ index ++ ] ;
65- if ( ! from ) continue ;
66- if ( kind . startsWith ( 'R' ) ) {
67- const to = fields [ index ++ ] ;
68- if ( ! to ) continue ;
69- const origin = origins . has ( from ) ? origins . get ( from ) ! : from ;
70- origins . set ( from , null ) ;
71- origins . set ( to , origin ) ;
72- if ( origin === null ) ended . add ( to ) ;
73- else ended . delete ( to ) ;
74- } else {
75- if ( typeof origins . get ( from ) === 'string' ) ended . add ( from ) ;
76- origins . set ( from , null ) ;
77- }
77+ for ( const { renamed, from, to } of nameStatusRecords ( status ) ) {
78+ const origin = renamed && origins . has ( from ) ? origins . get ( from ) ! : from ;
79+ if ( ! renamed && typeof origins . get ( from ) === 'string' ) ended . add ( from ) ;
80+ origins . set ( from , null ) ;
81+ if ( ! renamed ) continue ;
82+ origins . set ( to , origin ) ;
83+ if ( origin === null ) ended . add ( to ) ;
84+ else ended . delete ( to ) ;
7885 }
7986 } ;
8087 follow (
@@ -102,18 +109,11 @@ export function renamedSince(
102109 ...flags ,
103110 base ,
104111 ...( head ? [ head ] : [ ] ) ,
105- ] )
106- . toString ( 'utf8' )
107- . split ( '\0' ) ;
108- for ( let index = 0 ; index < aggregate . length ; ) {
109- const kind = aggregate [ index ++ ] ;
110- const from = aggregate [ index ++ ] ;
111- if ( kind ?. startsWith ( 'R' ) ) {
112- const to = aggregate [ index ++ ] ;
113- if ( from && to && ! renamed . has ( to ) && ! ended . has ( to ) && ! claimedOrigins . has ( from ) ) {
114- renamed . set ( to , from ) ;
115- claimedOrigins . add ( from ) ;
116- }
112+ ] ) ;
113+ for ( const { renamed : isRename , from, to } of nameStatusRecords ( aggregate ) ) {
114+ if ( isRename && ! renamed . has ( to ) && ! ended . has ( to ) && ! claimedOrigins . has ( from ) ) {
115+ renamed . set ( to , from ) ;
116+ claimedOrigins . add ( from ) ;
117117 }
118118 }
119119 return renamed ;
0 commit comments