Skip to content

Commit ead88de

Browse files
TobiasDuelliclaude
andcommitted
chore(quality): take the core that finds the default branch
Copied from Frontend's fix-quality-core-default-branch (b2deefd0d2, draft PR GleapSDK/Frontend#3079), which is not on Frontend's master yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 60b44a4 commit ead88de

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

‎tools/quality/changes.mjs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpat
44
import { tmpdir } from 'node:os';
55
import { join, resolve } from 'node:path';
66

7-
const DEFAULT_BASE = 'origin/master';
8-
97
const git = ({ root, args, input, env }) =>
108
execFileSync('git', args, {
119
cwd: root,
@@ -34,23 +32,33 @@ export const forkPointOf = ({ root, base, head }) => gitText({ root, args: ['mer
3432

3533
export const headOf = ({ root }) => gitText({ root, args: ['rev-parse', 'HEAD'] });
3634

37-
// The closest remote branch this one left. origin/master wins a tie, and a branch that holds this one plus more
35+
// origin/HEAD as the clone recorded it, else origin/main or origin/master, whichever exists.
36+
const defaultBranchOf = ({ root, rows }) => {
37+
try {
38+
return gitText({ root, args: ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD'] });
39+
} catch {
40+
return ['origin/main', 'origin/master'].find((ref) => rows.some((row) => row.startsWith(`${ref} `)));
41+
}
42+
};
43+
44+
// The closest remote branch this one left. The default branch wins a tie, and a branch that holds this one plus more
3845
// is its child, not its base. A ref at this very commit is this work pushed under another name, unless the commit is
39-
// on origin/master anyway. Costs about 250 ms across 2000 refs, so the answer is kept per commit.
46+
// on the default branch anyway. Costs about 250 ms across 2000 refs, so the answer is kept per commit.
4047
const findBase = ({ root, head }) => {
4148
const branch = gitText({ root, args: ['branch', '--show-current'] });
4249
const skipped = new Set(['origin/HEAD', 'origin', `origin/${branch}`]);
4350
const refs = gitText({ root, args: ['for-each-ref', '--format=%(refname:short) %(ahead-behind:HEAD)', 'refs/remotes/origin'] });
4451
const rows = refs.split('\n').filter(Boolean);
45-
const defaultRow = rows.find((row) => row.startsWith(`${DEFAULT_BASE} `));
52+
const defaultBase = defaultBranchOf({ root, rows });
53+
const defaultRow = rows.find((row) => row.startsWith(`${defaultBase} `));
4654
const isAheadOfDefault = !defaultRow || Number(defaultRow.split(' ')[2]) > 0;
4755
let best = null;
4856
for (const row of rows) {
4957
const [ref, ahead, behind] = row.split(' ');
50-
const isChild = Number(ahead) > 0 && Number(behind) === 0 && ref !== DEFAULT_BASE;
51-
const isThisCommit = Number(ahead) === 0 && Number(behind) === 0 && ref !== DEFAULT_BASE;
58+
const isChild = Number(ahead) > 0 && Number(behind) === 0 && ref !== defaultBase;
59+
const isThisCommit = Number(ahead) === 0 && Number(behind) === 0 && ref !== defaultBase;
5260
if (skipped.has(ref) || isChild || (isThisCommit && isAheadOfDefault)) continue;
53-
const isBetter = !best || Number(behind) < best.behind || (Number(behind) === best.behind && ref === DEFAULT_BASE);
61+
const isBetter = !best || Number(behind) < best.behind || (Number(behind) === best.behind && ref === defaultBase);
5462
if (isBetter) best = { ref, behind: Number(behind) };
5563
}
5664
if (!best) throw new Error(`no remote branch to compare ${head.slice(0, 10)} with`);

‎tools/quality/check.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const main = async () => {
154154
try {
155155
return await runFile(values.file);
156156
} catch (error) {
157-
// An edit hook must not stop a session over a missing origin/master or typescript. CI still runs it.
157+
// An edit hook must not stop a session over a missing default branch or typescript. CI still runs it.
158158
console.error(`quality check skipped for ${values.file}: ${error.message}`);
159159
return 0;
160160
}

‎tools/quality/core.cases.mjs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,28 @@ export const defineCoreCases = ({ describe, it, afterAll, config, LIMITS, checkC
941941
git(repo, 'update-ref', 'refs/remotes/origin/sibling', 'HEAD');
942942
assert.ok(run(repo).out.includes('origin/master'));
943943
});
944+
// The hooks pass no --base. A branch without commits of its own sits on the default branch, which wins.
945+
it('takes the default branch from origin/HEAD', () => {
946+
const repo = makeRepo(EXISTING);
947+
put(repo, LOGIC, broken(90));
948+
git(repo, 'commit', '-qam', 'develop moved on');
949+
git(repo, 'update-ref', 'refs/remotes/origin/develop', 'HEAD');
950+
git(repo, 'symbolic-ref', 'refs/remotes/origin/HEAD', 'refs/remotes/origin/develop');
951+
const { code, out } = run(repo);
952+
assert.equal(code, 0, out);
953+
assert.ok(out.includes('origin/develop'), out);
954+
});
955+
it('without origin/HEAD, takes origin/main over an old merged branch', () => {
956+
const repo = makeRepo(EXISTING);
957+
git(repo, 'update-ref', 'refs/remotes/origin/old-fix', 'HEAD');
958+
git(repo, 'update-ref', '-d', 'refs/remotes/origin/master');
959+
put(repo, LOGIC, broken(90));
960+
git(repo, 'commit', '-qam', 'main moved on');
961+
git(repo, 'update-ref', 'refs/remotes/origin/main', 'HEAD');
962+
const { code, out } = run(repo);
963+
assert.equal(code, 0, out);
964+
assert.ok(out.includes('origin/main'), out);
965+
});
944966
it('does not write to the index or the object store of the checkout', () => {
945967
const repo = makeRepo(EXISTING);
946968
put(repo, pathFor('fresh'), moduleOf(1));

‎tools/quality/core.sha256‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6fc118c76ca20471bb6eeac82abf1a7bbbac8fe65e091b0d8d09a6b33bff38dc
1+
08c8424e02fba5e2718a721ad3f00c0374650ec713b995c724ac0e686bd63029

0 commit comments

Comments
 (0)