@@ -44,32 +44,65 @@ export function headCommit(repoRoot: string): string {
4444}
4545
4646/**
47- * Files renamed between `base` and `head` (the working tree unless a commit is given),
48- * head path -> base path, so a rename is not a new entry. The explicit rename limit keeps
49- * inexact detection on regardless of the clone's `diff.renameLimit` config — a bulk move
50- * with edits (the daemon extraction) is exactly the inexact case the ratchets must see.
47+ * Head path -> base path. Follow committed moves before the aggregate diff, so later edits do not
48+ * erase a recorded rename. Added or deleted paths end that lineage. The aggregate diff still finds
49+ * a move spread across commits or staged in the working tree. Unlimited detection handles bulk moves.
5150 */
5251export function renamedSince (
5352 repoRoot : string ,
5453 base : string ,
5554 head ?: string ,
5655) : ReadonlyMap < string , string > {
5756 const renamed = new Map < string , string > ( ) ;
58- const status = git ( repoRoot , [
57+ const flags = [ '--name-status' , '-M' , '--diff-filter=ADR' , '-z' ] ;
58+ const follow = ( status : Buffer ) : void => {
59+ const fields = status . toString ( 'utf8' ) . split ( '\0' ) ;
60+ for ( let index = 0 ; index < fields . length ; ) {
61+ const kind = fields [ index ++ ] ?. trim ( ) ;
62+ if ( ! kind ) continue ;
63+ const from = fields [ index ++ ] ;
64+ if ( ! from ) continue ;
65+ if ( kind . startsWith ( 'R' ) ) {
66+ const to = fields [ index ++ ] ;
67+ if ( ! to ) continue ;
68+ const origin = renamed . get ( from ) ?? from ;
69+ renamed . delete ( from ) ;
70+ renamed . set ( to , origin ) ;
71+ } else {
72+ renamed . delete ( from ) ;
73+ }
74+ }
75+ } ;
76+ follow (
77+ git ( repoRoot , [
78+ '-c' ,
79+ 'diff.renameLimit=0' ,
80+ 'log' ,
81+ '--reverse' ,
82+ '--first-parent' ,
83+ '--format=' ,
84+ ...flags ,
85+ `${ base } ..${ head ?? 'HEAD' } ` ,
86+ ] ) ,
87+ ) ;
88+ if ( ! head ) follow ( git ( repoRoot , [ '-c' , 'diff.renameLimit=0' , 'diff' , ...flags , 'HEAD' ] ) ) ;
89+ const aggregate = git ( repoRoot , [
5990 '-c' ,
6091 'diff.renameLimit=0' ,
6192 'diff' ,
62- '--name-status' ,
63- '-M' ,
64- '--diff-filter=R' ,
65- '-z' ,
93+ ...flags ,
6694 base ,
6795 ...( head ? [ head ] : [ ] ) ,
68- ] ) ;
69- const fields = status . toString ( 'utf8' ) . split ( '\0' ) ;
70- for ( let index = 0 ; index + 2 < fields . length ; index += 3 ) {
71- const [ from , to ] = [ fields [ index + 1 ] , fields [ index + 2 ] ] ;
72- if ( from && to ) renamed . set ( to , from ) ;
96+ ] )
97+ . toString ( 'utf8' )
98+ . split ( '\0' ) ;
99+ for ( let index = 0 ; index < aggregate . length ; ) {
100+ const kind = aggregate [ index ++ ] ;
101+ const from = aggregate [ index ++ ] ;
102+ if ( kind ?. startsWith ( 'R' ) ) {
103+ const to = aggregate [ index ++ ] ;
104+ if ( from && to && ! renamed . has ( to ) ) renamed . set ( to , from ) ;
105+ }
73106 }
74107 return renamed ;
75108}
0 commit comments