Skip to content

Commit c277d72

Browse files
committed
fix(gates): preserve rename history on PR merge commits
1 parent 6b12297 commit c277d72

3 files changed

Lines changed: 133 additions & 2 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { expect, test } from 'vitest';
2+
import { execFileSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import { eagerClosureGraphOf } from '@agent-device/test-kit';
6+
import { createCommittedSourceTree, renamedSince } from './committed-source-tree.ts';
7+
import { mkGitFixtureRepo } from './eager-closure-gate-fixtures.ts';
8+
9+
test('a PR merge retains branch rename history and measures the merged tree', () => {
10+
const repo = mkGitFixtureRepo('renamed-since-pr-merge-');
11+
const git = (...args: string[]) => execFileSync('git', args, { cwd: repo }).toString().trim();
12+
const original = 'packages/demo/src/entry.ts';
13+
const moved = 'packages/demo/src/moved.ts';
14+
const base = git('rev-parse', 'HEAD');
15+
git('checkout', '-qb', 'pr');
16+
git('mv', original, moved);
17+
git('commit', '-qm', 'move');
18+
fs.appendFileSync(
19+
path.join(repo, moved),
20+
"export * from './main.ts';\n" + '// documentation\n'.repeat(100),
21+
);
22+
git('commit', '-aqm', 'document');
23+
const pr = git('rev-parse', 'HEAD');
24+
git('checkout', '-qb', 'advanced-main', base);
25+
fs.writeFileSync(path.join(repo, 'packages/demo/src/main.ts'), 'export const main = 1;\n');
26+
git('add', '.');
27+
git('commit', '-qm', 'advance main');
28+
const main = git('rev-parse', 'HEAD');
29+
git('merge', '--no-ff', '-qm', 'merge PR', pr);
30+
const merge = git('rev-parse', 'HEAD');
31+
expect(git('rev-parse', 'HEAD^1')).toBe(main);
32+
expect(git('rev-parse', 'HEAD^2')).toBe(pr);
33+
expect(renamedSince(repo, main, merge).get(moved)).toBe(renamedSince(repo, base, pr).get(moved));
34+
expect(renamedSince(repo, main, merge).get(moved)).toBe(original);
35+
expect(
36+
createCommittedSourceTree(repo, merge).isFile(path.join(repo, 'packages/demo/src/main.ts')),
37+
).toBe(true);
38+
expect(
39+
eagerClosureGraphOf(path.join(repo, moved), createCommittedSourceTree(repo, pr)).size,
40+
).toBe(1);
41+
expect(
42+
eagerClosureGraphOf(path.join(repo, moved), createCommittedSourceTree(repo, merge)).size,
43+
).toBe(2);
44+
git('checkout', '-qb', 'deleted-in-merge', main);
45+
git('merge', '--no-ff', '--no-commit', pr);
46+
git('rm', '-f', moved);
47+
git('commit', '-qm', 'merge without entry');
48+
expect(renamedSince(repo, main, 'HEAD').has(moved)).toBe(false);
49+
git('checkout', '-q', 'advanced-main');
50+
git('rm', moved);
51+
git('commit', '-qm', 'delete moved entry');
52+
fs.writeFileSync(path.join(repo, moved), 'export const replacement = true;\n');
53+
git('add', moved);
54+
git('commit', '-qm', 'replace entry');
55+
expect(renamedSince(repo, main, 'HEAD').has(moved)).toBe(false);
56+
});
57+
58+
test('a merge does not claim an origin replaced on its first parent', () => {
59+
const repo = mkGitFixtureRepo('renamed-since-pr-replaced-base-');
60+
const git = (...args: string[]) => execFileSync('git', args, { cwd: repo }).toString().trim();
61+
const original = 'packages/demo/src/entry.ts';
62+
const moved = 'packages/demo/src/moved.ts';
63+
const base = git('rev-parse', 'HEAD');
64+
git('checkout', '-qb', 'pr');
65+
git('mv', original, moved);
66+
git('commit', '-qm', 'move');
67+
fs.appendFileSync(path.join(repo, moved), '// documentation\n'.repeat(100));
68+
git('commit', '-aqm', 'document');
69+
const pr = git('rev-parse', 'HEAD');
70+
git('checkout', '-qb', 'advanced-main', base);
71+
fs.writeFileSync(path.join(repo, original), 'export const replacement = true;\n');
72+
git('commit', '-aqm', 'replace original');
73+
const main = git('rev-parse', 'HEAD');
74+
try {
75+
git('merge', '--no-ff', '--no-commit', pr);
76+
} catch {
77+
git('rm', original);
78+
}
79+
git('commit', '-qm', 'merge PR');
80+
expect(renamedSince(repo, main, 'HEAD').has(moved)).toBe(false);
81+
expect(renamedSince(repo, pr, 'HEAD').has(moved)).toBe(false);
82+
});

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,41 @@ function nameStatusRecords(output: Buffer): NameStatusRecord[] {
6060
return records;
6161
}
6262

63+
function endOriginsChangedOnBase(
64+
repoRoot: string,
65+
historyBase: string,
66+
baseCommit: string,
67+
origins: Map<string, string | null>,
68+
ended: Set<string>,
69+
): void {
70+
const changed = nameStatusRecords(
71+
git(repoRoot, [
72+
'-c',
73+
'diff.renameLimit=0',
74+
'log',
75+
'--first-parent',
76+
'--format=',
77+
'--name-status',
78+
'-M',
79+
'-z',
80+
`${historyBase}..${baseCommit}`,
81+
]),
82+
);
83+
const changedPaths = new Set(changed.flatMap(({ from, to }) => [from, to]));
84+
for (const [file, origin] of origins) {
85+
if (origin !== null && changedPaths.has(origin)) {
86+
origins.set(file, null);
87+
ended.add(file);
88+
}
89+
}
90+
}
91+
6392
/**
6493
* Head path -> base path. Follow committed moves before the aggregate diff, so later edits do not
6594
* erase a recorded rename. Added or deleted paths end that lineage. The aggregate diff still finds
66-
* a move spread across commits or staged in the working tree. Unlimited detection handles bulk moves.
95+
* a move spread across commits or staged in the working tree. For a merge into the measured base,
96+
* follow its second parent's history, invalidate origins changed on the first parent, and apply the
97+
* second-parent-to-merge diff. Unlimited detection handles bulk moves.
6798
*/
6899
export function renamedSince(
69100
repoRoot: string,
@@ -84,6 +115,19 @@ export function renamedSince(
84115
else ended.delete(to);
85116
}
86117
};
118+
const measured = head ?? 'HEAD';
119+
const parents = git(repoRoot, ['rev-list', '--parents', '-n', '1', measured])
120+
.toString('utf8')
121+
.trim()
122+
.split(' ');
123+
const baseCommit = git(repoRoot, ['rev-parse', `${base}^{commit}`])
124+
.toString('utf8')
125+
.trim();
126+
const isMergeIntoBase = parents.length === 3 && parents[1] === baseCommit;
127+
const historyHead = isMergeIntoBase ? parents[2]! : measured;
128+
const historyBase = isMergeIntoBase
129+
? git(repoRoot, ['merge-base', baseCommit, historyHead]).toString('utf8').trim()
130+
: base;
87131
follow(
88132
git(repoRoot, [
89133
'-c',
@@ -93,9 +137,13 @@ export function renamedSince(
93137
'--first-parent',
94138
'--format=',
95139
...flags,
96-
`${base}..${head ?? 'HEAD'}`,
140+
`${historyBase}..${historyHead}`,
97141
]),
98142
);
143+
if (isMergeIntoBase) {
144+
endOriginsChangedOnBase(repoRoot, historyBase, baseCommit, origins, ended);
145+
follow(git(repoRoot, ['-c', 'diff.renameLimit=0', 'diff', ...flags, historyHead, measured]));
146+
}
99147
if (!head) follow(git(repoRoot, ['-c', 'diff.renameLimit=0', 'diff', ...flags, 'HEAD']));
100148
const renamed = new Map<string, string>();
101149
for (const [file, origin] of origins) {

‎vitest.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export default defineConfig({
154154
'scripts/__tests__/help-conformance-sample-outputs.test.ts',
155155
'scripts/__tests__/help-conformance-topic-coverage.test.ts',
156156
'scripts/__tests__/eager-closure-budgets.test.ts',
157+
'scripts/__tests__/committed-source-tree.test.ts',
157158
'scripts/__tests__/mutation-scope-budget.test.ts',
158159
'scripts/__tests__/closure-growth-rule.test.ts',
159160
'scripts/__tests__/agent-setup-startup-contract.test.ts',

0 commit comments

Comments
 (0)