Skip to content

Commit 1d0fdb3

Browse files
committed
chore(gates): end rename provenance when a path is replaced
1 parent 8ddbec5 commit 1d0fdb3

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export function renamedSince(
5353
base: string,
5454
head?: string,
5555
): ReadonlyMap<string, string> {
56-
const renamed = new Map<string, string>();
56+
const origins = new Map<string, string | null>();
57+
const ended = new Set<string>();
5758
const flags = ['--name-status', '-M', '--diff-filter=ADR', '-z'];
5859
const follow = (status: Buffer): void => {
5960
const fields = status.toString('utf8').split('\0');
@@ -65,11 +66,14 @@ export function renamedSince(
6566
if (kind.startsWith('R')) {
6667
const to = fields[index++];
6768
if (!to) continue;
68-
const origin = renamed.get(from) ?? from;
69-
renamed.delete(from);
70-
renamed.set(to, origin);
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);
7174
} else {
72-
renamed.delete(from);
75+
if (typeof origins.get(from) === 'string') ended.add(from);
76+
origins.set(from, null);
7377
}
7478
}
7579
};
@@ -86,6 +90,10 @@ export function renamedSince(
8690
]),
8791
);
8892
if (!head) follow(git(repoRoot, ['-c', 'diff.renameLimit=0', 'diff', ...flags, 'HEAD']));
93+
const renamed = new Map<string, string>();
94+
for (const [file, origin] of origins) {
95+
if (origin !== null) renamed.set(file, origin);
96+
}
8997
const aggregate = git(repoRoot, [
9098
'-c',
9199
'diff.renameLimit=0',
@@ -101,7 +109,7 @@ export function renamedSince(
101109
const from = aggregate[index++];
102110
if (kind?.startsWith('R')) {
103111
const to = aggregate[index++];
104-
if (from && to && !renamed.has(to)) renamed.set(to, from);
112+
if (from && to && !renamed.has(to) && !ended.has(to)) renamed.set(to, from);
105113
}
106114
}
107115
return renamed;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,31 @@ test('later edits preserve a committed rename but deletion ends its lineage', ()
537537
);
538538
execFileSync('git', ['rm', moved], { cwd: repo });
539539
execFileSync('git', ['commit', '-qm', 'remove'], { cwd: repo });
540-
fs.writeFileSync(path.join(repo, moved), 'export const unrelated = true;\n');
540+
fs.writeFileSync(
541+
path.join(repo, moved),
542+
createCommittedSourceTree(repo, base).readFile(path.join(repo, 'packages/demo/src/entry.ts')),
543+
);
541544
execFileSync('git', ['add', moved], { cwd: repo });
542545
execFileSync('git', ['commit', '-qm', 'new entry'], { cwd: repo });
543546
expect(renamedSince(repo, base, 'HEAD').has(moved)).toBe(false);
544547
});
545548

549+
test('a new file at a moved source cannot claim the original lineage when renamed', () => {
550+
const repo = mkGitFixtureRepo('renamed-since-reused-source-');
551+
const base = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: repo }).toString().trim();
552+
const original = 'packages/demo/src/entry.ts';
553+
execFileSync('git', ['mv', original, 'packages/demo/src/moved.ts'], { cwd: repo });
554+
execFileSync('git', ['commit', '-qm', 'move'], { cwd: repo });
555+
fs.writeFileSync(path.join(repo, original), 'export const a = 1;\n');
556+
execFileSync('git', ['add', original], { cwd: repo });
557+
execFileSync('git', ['commit', '-qm', 'new source'], { cwd: repo });
558+
execFileSync('git', ['mv', original, 'packages/demo/src/new.ts'], { cwd: repo });
559+
execFileSync('git', ['commit', '-qm', 'move new source'], { cwd: repo });
560+
const paired = renamedSince(repo, base, 'HEAD');
561+
expect(paired.get('packages/demo/src/moved.ts')).toBe(original);
562+
expect(paired.has('packages/demo/src/new.ts')).toBe(false);
563+
});
564+
546565
test('rename detection survives a git mv with an edit and a hostile renameLimit', () => {
547566
// The daemon move is the load-bearing case: `git mv` PLUS a per-file edit makes the rename
548567
// inexact, and a clone-side `diff.renameLimit` below the file count silently disables the

0 commit comments

Comments
 (0)