Skip to content

Commit 8ddbec5

Browse files
committed
chore(gates): retain committed rename lineage after review edits
1 parent 94f13ac commit 8ddbec5

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

‎scripts/__tests__/committed-source-tree.ts‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
5251
export 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
}

‎scripts/__tests__/eager-closure-budgets.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,27 @@ test('a renamed entry is followed to its path at the base, not treated as first-
522522
).toBe(null);
523523
});
524524

525+
test('later edits preserve a committed rename but deletion ends its lineage', () => {
526+
const repo = mkGitFixtureRepo('renamed-since-history-');
527+
const base = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: repo }).toString().trim();
528+
const moved = 'packages/demo/src/moved.ts';
529+
execFileSync('git', ['mv', 'packages/demo/src/entry.ts', moved], { cwd: repo });
530+
execFileSync('git', ['commit', '-qm', 'move'], { cwd: repo });
531+
fs.appendFileSync(path.join(repo, moved), '// public API documentation\n'.repeat(100));
532+
execFileSync('git', ['commit', '-aqm', 'document'], { cwd: repo });
533+
const paired = renamedSince(repo, base, 'HEAD');
534+
expect(paired.get(moved)).toBe('packages/demo/src/entry.ts');
535+
expect(baseProductionPathOf(repo, moved, createCommittedSourceTree(repo, base), paired)).toBe(
536+
'packages/demo/src/entry.ts',
537+
);
538+
execFileSync('git', ['rm', moved], { cwd: repo });
539+
execFileSync('git', ['commit', '-qm', 'remove'], { cwd: repo });
540+
fs.writeFileSync(path.join(repo, moved), 'export const unrelated = true;\n');
541+
execFileSync('git', ['add', moved], { cwd: repo });
542+
execFileSync('git', ['commit', '-qm', 'new entry'], { cwd: repo });
543+
expect(renamedSince(repo, base, 'HEAD').has(moved)).toBe(false);
544+
});
545+
525546
test('rename detection survives a git mv with an edit and a hostile renameLimit', () => {
526547
// The daemon move is the load-bearing case: `git mv` PLUS a per-file edit makes the rename
527548
// inexact, and a clone-side `diff.renameLimit` below the file count silently disables the

0 commit comments

Comments
 (0)