Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/closura-arc-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,15 @@ jobs:
node --test scripts/fork-drift-receipt.test.mjs scripts/fork-sync-plan.test.mjs
# Exit 1 means the release line is past its drift budget, which is
# reported rather than failed. Exit 2 means the receipt itself broke.
node scripts/fork-drift-receipt.mjs --base "origin/${{ github.base_ref || github.ref_name }}" || rc=$?
# `--head` is the pull request head (the pushed commit on a push), so
# the receipt measures the candidate merge of base and head, and it
# records all three tip SHAs so the numbers can be reproduced later.
# The head is in the shallow checkout: it is a parent of the merge ref.
node scripts/fork-drift-receipt.mjs --base "origin/${{ github.base_ref || github.ref_name }}" --head "$DRIFT_HEAD_SHA" || rc=$?
if [ "${rc:-0}" -eq 2 ]; then exit 2; fi
shell: bash
env:
DRIFT_HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}

- name: Guard indispensable AssemblyAI voice implementation
shell: bash
Expand Down
10 changes: 10 additions & 0 deletions docs/operations/fork-sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ git fetch upstream
node scripts/fork-drift-receipt.mjs --base origin/release/native-0.1.10
```

In a pull request, ARC CI adds `--head <PR head SHA>`, so the receipt measures
the candidate (the merge of the target branch and the PR head) rather than the
target branch alone. Every receipt records `baseSha`, `headSha` and
`upstreamSha` and prints a `reproduce` command. Running that command later
gives the same numbers even after the branches have moved:

```sh
node scripts/fork-drift-receipt.mjs --base <baseSha> --head <headSha> --upstream <upstreamSha>
```

Run `DRY_RUN=1 UPSTREAM_URL=https://github.com/pingdotgg/t3code.git bash
scripts/upstream-sync.sh` to probe merge conflicts without committing, pushing
or contacting the GitHub API.
84 changes: 65 additions & 19 deletions scripts/fork-drift-receipt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,64 @@
// Prints one JSON object and exits 1 when the line is behind by more than the
// budget. Read-only: it never fetches, merges or writes. The weekly receipt is
// `node scripts/fork-drift-receipt.mjs --base origin/release/native-0.1.10`.
// In a pull request, CI also passes `--head <PR head SHA>` so the receipt
// measures the candidate, not just the target branch.

import * as NodeChildProcess from "node:child_process";

export const UPSTREAM_REF = "upstream/main";
export const CONFLICT_BUDGET = 25;
export const BEHIND_BUDGET = 300;

export function measure(root, baseRef, upstreamRef = UPSTREAM_REF) {
const revList = git(root, ["rev-list", "--left-right", "--count", `${baseRef}...${upstreamRef}`]);
const [aheadRaw, behindRaw] = revList.trim().split(/\s+/);
const ahead = Number(aheadRaw);
const behind = Number(behindRaw);
if (!Number.isInteger(ahead) || !Number.isInteger(behind)) {
throw new Error(`unexpected rev-list output: ${revList.trim()}`);
}
const mergeBase = git(root, ["merge-base", baseRef, upstreamRef]).trim();
const names = git(root, ["diff", "--name-only", `${baseRef}...${upstreamRef}`])
// Every ref is resolved to its tip SHA once, and all numbers come from those
// SHAs, so a later run with `--base <baseSha> --head <headSha> --upstream
// <upstreamSha>` reproduces the receipt after the refs have moved.
//
// `head` is the pull request candidate. Drift is measured for the commit that
// would land, which is the merge of `base` and `head`: commits in either tip
// but not in upstream are ahead, upstream commits in neither tip are behind.
// Without `head` (a push or the weekly run) the candidate is `base` itself.
export function measure(root, { baseRef, headRef = baseRef, upstreamRef = UPSTREAM_REF }) {
const baseSha = resolveCommit(root, baseRef);
const headSha = resolveCommit(root, headRef);
const upstreamSha = resolveCommit(root, upstreamRef);
const tips = baseSha === headSha ? [baseSha] : [baseSha, headSha];
const ahead = count(root, [...tips, `^${upstreamSha}`]);
const behind = count(root, [upstreamSha, ...tips.map((sha) => `^${sha}`)]);
// With more than two commits, merge-base pairs the first with a hypothetical
// merge of the rest, which is exactly the candidate merge of base and head.
const mergeBase = git(root, ["merge-base", upstreamSha, ...tips]).trim();
const names = git(root, ["diff", "--name-only", mergeBase, upstreamSha])
.split("\n")
.filter((name) => name.length > 0);
return { baseRef, upstreamRef, mergeBase, ahead, behind, changedFiles: names.length };
return {
baseRef,
baseSha,
headRef,
headSha,
upstreamRef,
upstreamSha,
mergeBase,
ahead,
behind,
changedFiles: names.length,
};
}

export function resolveCommit(root, ref) {
if (typeof ref !== "string" || ref.length === 0 || ref.startsWith("-")) {
throw new Error(`invalid ref: ${String(ref)}`);
}
return git(root, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]).trim();
}

function count(root, revs) {
const raw = git(root, ["rev-list", "--count", ...revs]).trim();
const value = Number(raw);
if (!Number.isInteger(value)) {
throw new Error(`unexpected rev-list output: ${raw}`);
}
return value;
}

export function budgetFindings(drift, behindBudget = BEHIND_BUDGET) {
Expand All @@ -38,19 +76,27 @@ function git(root, args) {
return NodeChildProcess.execFileSync("git", ["-C", root, ...args], { encoding: "utf8" });
}

function option(args, name) {
const index = args.indexOf(name);
if (index === -1) return undefined;
const value = args[index + 1];
if (!value || value.startsWith("--")) {
throw new Error(`${name} needs a ref`);
}
return value;
}

function main() {
const args = process.argv.slice(2);
const baseIndex = args.indexOf("--base");
const baseRef = baseIndex === -1 ? "HEAD" : args[baseIndex + 1];
if (!baseRef) {
throw new Error("--base needs a ref");
}
const rootIndex = args.indexOf("--root");
const root = rootIndex === -1 ? process.cwd() : args[rootIndex + 1];
const drift = measure(root, baseRef);
const baseRef = option(args, "--base") ?? "HEAD";
const headRef = option(args, "--head") ?? baseRef;
const upstreamRef = option(args, "--upstream") ?? UPSTREAM_REF;
const root = option(args, "--root") ?? process.cwd();
const drift = measure(root, { baseRef, headRef, upstreamRef });
const findings = budgetFindings(drift);
const receipt = {
...drift,
reproduce: `node scripts/fork-drift-receipt.mjs --base ${drift.baseSha} --head ${drift.headSha} --upstream ${drift.upstreamSha}`,
behindBudget: BEHIND_BUDGET,
conflictBudget: CONFLICT_BUDGET,
findings,
Expand Down
81 changes: 78 additions & 3 deletions scripts/fork-drift-receipt.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import * as NodeTest from "node:test";

import { BEHIND_BUDGET, budgetFindings, measure } from "./fork-drift-receipt.mjs";
import { BEHIND_BUDGET, budgetFindings, measure, resolveCommit } from "./fork-drift-receipt.mjs";

function git(root, args) {
return NodeChildProcess.execFileSync("git", ["-C", root, ...args], { encoding: "utf8" });
Expand Down Expand Up @@ -45,7 +45,7 @@ NodeTest.test("counts commits the release line is behind", () => {
commit(root, "ours", "release");
commit(root, "theirs-a", "upstream");
commit(root, "theirs-b", "upstream");
const drift = measure(root, "release", "upstream");
const drift = measure(root, { baseRef: "release", upstreamRef: "upstream" });
NodeAssert.equal(drift.ahead, 1);
NodeAssert.equal(drift.behind, 2);
NodeAssert.equal(drift.changedFiles, 2);
Expand All @@ -65,8 +65,83 @@ NodeTest.test("a missing ref fails closed", () => {
const root = repo();
try {
commit(root, "base", "release");
NodeAssert.throws(() => measure(root, "release", "missing"), /missing/);
NodeAssert.throws(
() => measure(root, { baseRef: "release", upstreamRef: "missing" }),
/missing/,
);
NodeAssert.throws(
() => measure(root, { baseRef: "release", headRef: "missing", upstreamRef: "release" }),
/missing/,
);
} finally {
NodeFS.rmSync(root, { recursive: true, force: true });
}
});

NodeTest.test("records tip SHAs and reproduces after the refs move", () => {
const root = repo();
try {
commit(root, "base", "release");
git(root, ["branch", "upstream"]);
commit(root, "theirs-a", "upstream");
commit(root, "theirs-b", "upstream");
git(root, ["checkout", "-q", "release"]);
git(root, ["checkout", "-q", "-b", "pr"]);
git(root, ["merge", "-q", "--no-edit", "--no-ff", "upstream"]);
commit(root, "ours", "release");

const drift = measure(root, { baseRef: "release", headRef: "pr", upstreamRef: "upstream" });
NodeAssert.equal(drift.baseSha, resolveCommit(root, "release"));
NodeAssert.equal(drift.headSha, resolveCommit(root, "pr"));
NodeAssert.equal(drift.upstreamSha, resolveCommit(root, "upstream"));
NodeAssert.match(drift.baseSha, /^[0-9a-f]{40}$/);
// The candidate is the merge of release and the pr: it carries the sync,
// so it is not behind, while the target branch alone is two behind.
NodeAssert.equal(drift.behind, 0);
NodeAssert.equal(drift.ahead, 2);
NodeAssert.equal(drift.mergeBase, drift.upstreamSha);
NodeAssert.equal(drift.changedFiles, 0);
NodeAssert.equal(measure(root, { baseRef: "release", upstreamRef: "upstream" }).behind, 2);

commit(root, "later-ours", "release");
commit(root, "later-pr", "pr");
commit(root, "theirs-c", "upstream");
NodeAssert.notEqual(resolveCommit(root, "upstream"), drift.upstreamSha);

const replay = measure(root, {
baseRef: drift.baseSha,
headRef: drift.headSha,
upstreamRef: drift.upstreamSha,
});
for (const key of [
"baseSha",
"headSha",
"upstreamSha",
"mergeBase",
"ahead",
"behind",
"changedFiles",
]) {
NodeAssert.equal(replay[key], drift[key], key);
}
} finally {
NodeFS.rmSync(root, { recursive: true, force: true });
}
});

NodeTest.test("without a head the candidate is the base", () => {
const root = repo();
try {
const drift = measure(root, { baseRef: "release", upstreamRef: "release" });
NodeAssert.equal(drift.headRef, "release");
NodeAssert.equal(drift.headSha, drift.baseSha);
NodeAssert.equal(drift.ahead, 0);
NodeAssert.equal(drift.behind, 0);
} finally {
NodeFS.rmSync(root, { recursive: true, force: true });
}
});

NodeTest.test("an option-shaped ref is refused", () => {
NodeAssert.throws(() => resolveCommit(process.cwd(), "--all"), /invalid ref/);
});
Loading