|
8 | 8 | // |
9 | 9 | // vp run pr-merge-check <pr> [<pr>...] |
10 | 10 | // |
11 | | -// Merges origin/main and each PR head, in order, in a temporary worktree next |
12 | | -// to this checkout, then runs check:changed against origin/main. Pass every PR |
13 | | -// of a batch in one run. Exit 0 means merge them now, in the same order: each |
14 | | -// new commit on main makes the result older. |
| 11 | +// Merges GitHub's main and each PR head, in order, in a temporary worktree |
| 12 | +// next to this checkout, then runs check:changed against that main. PR numbers |
| 13 | +// are GitHub's: with `origin` on the GitCafe mirror, fetches use the `github` |
| 14 | +// remote. Pass every PR of a batch in one run. Exit 0 prints the merge commands, |
| 15 | +// pinned to the tested heads. Run them now, in order: each new commit on main |
| 16 | +// makes the result older. |
15 | 17 | import * as NodeChildProcess from "node:child_process"; |
16 | 18 | import * as NodeFS from "node:fs"; |
17 | 19 | import * as NodePath from "node:path"; |
@@ -40,41 +42,54 @@ const run = (command: string, args: ReadonlyArray<string>, cwd: string) => |
40 | 42 | const git = (cwd: string, ...args: Array<string>) => |
41 | 43 | NodeChildProcess.execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); |
42 | 44 |
|
43 | | -git(root, "fetch", "--quiet", "origin", "+refs/heads/main:refs/remotes/origin/main"); |
44 | | -const heads = prs.map((pr) => { |
45 | | - git(root, "fetch", "--quiet", "origin", `pull/${pr}/head`); |
46 | | - return { pr, sha: git(root, "rev-parse", "FETCH_HEAD") }; |
47 | | -}); |
| 45 | +const hasGitHubRemote = |
| 46 | + NodeChildProcess.spawnSync("git", ["remote", "get-url", "github"], { cwd: root }).status === 0; |
| 47 | +const remote = hasGitHubRemote ? "github" : "origin"; |
| 48 | +// Signing can prompt or fail, which would read as a conflict. |
| 49 | +const merge = ["-c", "commit.gpgsign=false", "merge", "--quiet", "--no-ff"]; |
48 | 50 |
|
| 51 | +// The worktree has its own FETCH_HEAD, so a fetch in this checkout or in |
| 52 | +// another run cannot swap the commit under test. |
49 | 53 | const dir = NodePath.resolve(root, "..", `${NodePath.basename(root)}-merge-check-${process.pid}`); |
50 | | -git(root, "worktree", "add", "--quiet", "--detach", dir, "origin/main"); |
51 | | -// Ctrl-C reaches the running child too. Staying alive lets `finally` remove the worktree. |
52 | | -process.on("SIGINT", () => {}); |
| 54 | +git(root, "worktree", "add", "--quiet", "--detach", "--no-checkout", dir); |
| 55 | +// A terminal signal reaches the running child too. Staying alive lets |
| 56 | +// `finally` remove the worktree. SIGKILL still leaves it behind. |
| 57 | +for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.on(signal, () => {}); |
53 | 58 |
|
| 59 | +const fetchHead = (ref: string) => { |
| 60 | + git(dir, "fetch", "--quiet", remote, ref); |
| 61 | + return git(dir, "rev-parse", "FETCH_HEAD"); |
| 62 | +}; |
| 63 | + |
| 64 | +const heads: Array<{ pr: string; sha: string }> = []; |
54 | 65 | let ok = false; |
55 | 66 | try { |
56 | | - const conflict = heads.find( |
57 | | - ({ pr, sha }) => |
58 | | - !run("git", ["merge", "--quiet", "--no-ff", "-m", `merge-check #${pr}`, sha], dir), |
59 | | - ); |
| 67 | + const base = fetchHead("refs/heads/main"); |
| 68 | + git(dir, "checkout", "--quiet", "--detach", base); |
| 69 | + const conflict = prs.find((pr) => { |
| 70 | + const sha = fetchHead(`pull/${pr}/head`); |
| 71 | + heads.push({ pr, sha }); |
| 72 | + return !run("git", [...merge, "-m", `merge-check #${pr}`, sha], dir); |
| 73 | + }); |
60 | 74 | if (conflict) { |
61 | | - console.error(`\npr-merge-check: #${conflict.pr} does not merge cleanly onto main.`); |
| 75 | + console.error(`\npr-merge-check: #${conflict} does not merge cleanly onto main.`); |
62 | 76 | } else { |
63 | 77 | // A lockfile that two PRs edited can merge as text and still not match |
64 | 78 | // the manifests. The frozen install fails on that. |
65 | 79 | ok = |
66 | 80 | run("vp", ["i", "--frozen-lockfile", "--", "--yes"], dir) && |
67 | | - run("node", ["scripts/check-changed.ts", "origin/main"], dir); |
| 81 | + run("node", ["scripts/check-changed.ts", base], dir); |
68 | 82 | } |
69 | 83 | } finally { |
70 | 84 | git(root, "worktree", "remove", "--force", dir); |
71 | 85 | NodeFS.rmSync(dir, { recursive: true, force: true }); |
72 | 86 | } |
73 | 87 |
|
74 | 88 | const list = prs.map((pr) => `#${pr}`).join(" "); |
75 | | -console.log( |
76 | | - ok |
77 | | - ? `\npr-merge-check: ${list} pass on main. Merge them now, in this order.` |
78 | | - : `\npr-merge-check: ${list} fail on main. Do not merge.`, |
79 | | -); |
80 | | -if (!ok) process.exit(1); |
| 89 | +if (!ok) { |
| 90 | + console.log(`\npr-merge-check: ${list} fail on main. Do not merge.`); |
| 91 | + process.exit(1); |
| 92 | +} |
| 93 | +console.log(`\npr-merge-check: ${list} pass on main. Merge them now, in this order:`); |
| 94 | +for (const { pr, sha } of heads) |
| 95 | + console.log(` gh pr merge ${pr} --squash --match-head-commit ${sha}`); |
0 commit comments