Skip to content

Commit 2fdfa8d

Browse files
committed
chore(gates): share rename canonicalization and prove it in a fixture repo
Non-blocking #3546 review items: renamedSince gains an optional head ref and an explicit unlimited diff.renameLimit (a clone-side limit silently disables inexact detection, which the daemon move is for every file it touched); wire-compat's tagToHeadRenames now flips the shared helper instead of re-implementing the diff. A fixture-repo test covers git mv + edit plus a hostile renameLimit, in both directions the gates consume. Generated with assistance.
1 parent 12feeeb commit 2fdfa8d

3 files changed

Lines changed: 70 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,29 @@ export function headCommit(repoRoot: string): string {
4242
return git(repoRoot, ['rev-parse', 'HEAD']).toString('utf8').trim();
4343
}
4444

45-
/** Files renamed since `base`, current path -> path at `base`, so a rename is not a new entry. */
46-
export function renamedSince(repoRoot: string, base: string): ReadonlyMap<string, string> {
45+
/**
46+
* Files renamed between `base` and `head` (the working tree unless a commit is given),
47+
* head path -> base path, so a rename is not a new entry. The explicit rename limit keeps
48+
* inexact detection on regardless of the clone's `diff.renameLimit` config — a bulk move
49+
* with edits (the daemon extraction) is exactly the inexact case the ratchets must see.
50+
*/
51+
export function renamedSince(
52+
repoRoot: string,
53+
base: string,
54+
head?: string,
55+
): ReadonlyMap<string, string> {
4756
const renamed = new Map<string, string>();
48-
const status = git(repoRoot, ['diff', '--name-status', '-M', '--diff-filter=R', '-z', base]);
57+
const status = git(repoRoot, [
58+
'-c',
59+
'diff.renameLimit=0',
60+
'diff',
61+
'--name-status',
62+
'-M',
63+
'--diff-filter=R',
64+
'-z',
65+
base,
66+
...(head ? [head] : []),
67+
]);
4968
const fields = status.toString('utf8').split('\0');
5069
for (let index = 0; index + 2 < fields.length; index += 3) {
5170
const [from, to] = [fields[index + 1], fields[index + 2]];

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,48 @@ test('a renamed entry is followed to its path at the base, not treated as first-
506506
).toBe(null);
507507
});
508508

509+
test('rename detection survives a git mv with an edit and a hostile renameLimit', () => {
510+
// The daemon move is the load-bearing case: `git mv` PLUS a per-file edit makes the rename
511+
// inexact, and a clone-side `diff.renameLimit` below the file count silently disables the
512+
// scan. `renamedSince` pins the limit off, so both must still pair.
513+
const repo = mkGitFixtureRepo('renamed-since-inexact-');
514+
// git mv PLUS an edit: inexact rename detection has to score similarity to pair this one.
515+
// The fixture files are one statement long, so an appended edit would be a rewrite git
516+
// refuses to pair at all; pad in the base commit first, then move and touch a line the
517+
// way the daemon files were touched.
518+
for (const file of ['packages/demo/src/entry.ts', 'packages/demo/src/facades/top.ts'])
519+
fs.appendFileSync(
520+
path.join(repo, file),
521+
Array.from({ length: 40 }, (_, i) => `export const pad${i} = ${i};`).join('\n') + '\n',
522+
);
523+
execFileSync('git', ['commit', '-aqm', 'pad'], { cwd: repo });
524+
execFileSync('git', ['mv', 'packages/demo/src/entry.ts', 'packages/demo/src/moved.ts'], {
525+
cwd: repo,
526+
});
527+
fs.appendFileSync(path.join(repo, 'packages/demo/src/moved.ts'), '\nexport const touched = 1;\n');
528+
execFileSync('git', ['mv', 'packages/demo/src/facades/top.ts', 'packages/demo/src/facades/top2.ts'], {
529+
cwd: repo,
530+
});
531+
fs.appendFileSync(
532+
path.join(repo, 'packages/demo/src/facades/top2.ts'),
533+
'\nexport const t2 = 1;\n',
534+
);
535+
execFileSync('git', ['add', '-A'], { cwd: repo });
536+
execFileSync('git', ['commit', '-qm', 'two moved edits'], { cwd: repo });
537+
const withEdits = renamedSince(repo, 'HEAD~1', 'HEAD');
538+
expect(withEdits.get('packages/demo/src/moved.ts')).toBe('packages/demo/src/entry.ts');
539+
expect(withEdits.get('packages/demo/src/facades/top2.ts')).toBe('packages/demo/src/facades/top.ts');
540+
// A clone-side limit BELOW the two inexact renames must not disable detection: the helper
541+
// passes its own `-c diff.renameLimit=0` (unlimited).
542+
execFileSync('git', ['config', 'diff.renameLimit', '1'], { cwd: repo });
543+
const underLimit = renamedSince(repo, 'HEAD~1', 'HEAD');
544+
expect(underLimit.get('packages/demo/src/moved.ts')).toBe('packages/demo/src/entry.ts');
545+
expect(underLimit.get('packages/demo/src/facades/top2.ts')).toBe('packages/demo/src/facades/top.ts');
546+
// The base -> head direction the wire-compat ledger canonicalizes with (#3546 review).
547+
const baseToHead = new Map([...underLimit].map(([headPath, basePath]) => [basePath, headPath]));
548+
expect(baseToHead.get('packages/demo/src/entry.ts')).toBe('packages/demo/src/moved.ts');
549+
});
550+
509551
test('discovery is recursive and reads TRACKED files only', () => {
510552
// Two holes in one fixture, because both are about discovery seeing the wrong set of files.
511553
//

‎scripts/wire-compat/run.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import path from 'node:path';
2222
import { repoGit, requireUnshallowHistory } from '../lib/repo-git.ts';
23+
import { renamedSince } from '../__tests__/committed-source-tree.ts';
2324
import { digestDeclaration } from '../../test/wire-compat/declaration-digest.ts';
2425
import {
2526
digestWireSurface,
@@ -81,15 +82,11 @@ if (!baseline) {
8182
* Digests travel unchanged: a pure move is not a wire change.
8283
*/
8384
function tagToHeadRenames(tag: string): Map<string, string> {
84-
const fields = git(['diff', '--name-status', '-M', '--diff-filter=R', '-z', tag, 'HEAD'])
85-
.split('\0')
86-
.filter(Boolean);
87-
const renames = new Map<string, string>();
88-
for (let index = 0; index + 2 < fields.length; index += 3) {
89-
const [, from, to] = fields.slice(index, index + 3) as [string, string, string];
90-
renames.set(from, to);
91-
}
92-
return renames;
85+
// head -> base at [tag, HEAD]; flipped into the base -> head direction the ledger needs.
86+
const flipped = new Map<string, string>();
87+
for (const [headPath, basePath] of renamedSince(repoRoot, tag, 'HEAD'))
88+
flipped.set(basePath, headPath);
89+
return flipped;
9390
}
9491

9592
function canonicalizeLedgerRef(ref: string, renames: ReadonlyMap<string, string>): string {

0 commit comments

Comments
 (0)