Public: anyone can read this; the crew can change it.
The /review diff stat uses the pull's stored base oid, not the live base tip #16925
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/4779-the-review-diff-stat-uses-the-pull-s-sto → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +63
−5
src/git/tips.ts +24
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | * `packed-refs`. Anything else (a symbolic ref, a missing file) is simply |
| 5 | 5 | * absent here, and the caller asks git. |
| 6 | 6 | */ |
| 7 | import { readFileSync } from "node:fs"; | |
| 7 | 8 | import { join } from "node:path"; |
| 8 | 9 | import { isSafeRef } from "./guard.ts"; |
| 9 | 10 | |
| @@ -41,3 +42,26 @@ export async function readTips( | ||
| 41 | 42 | } |
| 42 | 43 | return out; |
| 43 | 44 | } |
| 45 | ||
| 46 | /** | |
| 47 | * One ref's tip read synchronously from its file (loose, else packed): for a caller that cannot | |
| 48 | * wait. | |
| 49 | */ | |
| 50 | export function readTipSync(dir: string, name: string): string | undefined { | |
| 51 | if (!isSafeRef(name) || !name.startsWith("refs/")) return undefined; | |
| 52 | try { | |
| 53 | const text = readFileSync(join(dir, name), "utf8").trim(); | |
| 54 | if (OID.test(text)) return text; | |
| 55 | } catch { | |
| 56 | // not loose: it may be packed | |
| 57 | } | |
| 58 | try { | |
| 59 | for (const line of readFileSync(join(dir, "packed-refs"), "utf8").split("\n")) { | |
| 60 | const space = line.indexOf(" "); | |
| 61 | if (space > 0 && line.slice(space + 1) === name && OID.test(line.slice(0, space))) return line.slice(0, space); | |
| 62 | } | |
| 63 | } catch { | |
| 64 | // no packed-refs | |
| 65 | } | |
| 66 | return undefined; | |
| 67 | } |
src/web/studio/review_evidence.ts +10 −4
| @@ -12,6 +12,8 @@ import { pullDir, type Pull } from "../../core/pulls.ts"; | ||
| 12 | 12 | import type { App } from "../../app.ts"; |
| 13 | 13 | import { runGit } from "../../git/env.ts"; |
| 14 | 14 | import { isSafeOid } from "../../git/guard.ts"; |
| 15 | import { readTipSync } from "../../git/tips.ts"; | |
| 16 | import { repoDir } from "../../git/repos.ts"; | |
| 15 | 17 | |
| 16 | 18 | export interface DiffStat { |
| 17 | 19 | readonly files: number; |
| @@ -85,10 +87,13 @@ export function diffStatFor( | ||
| 85 | 87 | pull: Pick<Pull, "baseOid" | "headOid" | "sealed" | "crossRepo" | "headRepoId" | "baseBranch">, |
| 86 | 88 | ): DiffStat | "loading" | "failed" { |
| 87 | 89 | const w = workOf(app); |
| 88 | const key = `${pull.baseOid}:${pull.headOid}`; | |
| 90 | // The base is the branch's tip now, not the oid stored when the pull opened: | |
| 91 | // the stat is what landing would change. Read from the ref file, no process. | |
| 92 | const baseOid = readTipSync(repoDir(app.reposRoot, repo.ownerName, repo.name), `refs/heads/${pull.baseBranch}`) ?? pull.baseOid; | |
| 93 | const key = `${baseOid}:${pull.headOid}`; | |
| 89 | 94 | const kept = w.kept.get(key); |
| 90 | 95 | if (kept !== undefined) return kept; |
| 91 | if (!isSafeOid(pull.baseOid) || !isSafeOid(pull.headOid)) return "failed"; | |
| 96 | if (!isSafeOid(baseOid) || !isSafeOid(pull.headOid)) return "failed"; | |
| 92 | 97 | const failedUntil = w.failed.get(key); |
| 93 | 98 | if (failedUntil !== undefined) { |
| 94 | 99 | if (failedUntil > Date.now()) return "failed"; |
| @@ -98,7 +103,7 @@ export function diffStatFor( | ||
| 98 | 103 | w.inflight.add(key); |
| 99 | 104 | w.queued.set(key, async () => { |
| 100 | 105 | try { |
| 101 | await compute(app, w, key, repo, pull); | |
| 106 | await compute(app, w, key, repo, pull, baseOid); | |
| 102 | 107 | } catch { |
| 103 | 108 | if (!app.closing.signal.aborted) w.failed.set(key, Date.now() + RETRY_MS); |
| 104 | 109 | } finally { |
| @@ -115,9 +120,10 @@ async function compute( | ||
| 115 | 120 | key: string, |
| 116 | 121 | repo: { ownerName: string; name: string }, |
| 117 | 122 | pull: Pick<Pull, "baseOid" | "headOid" | "sealed" | "crossRepo" | "headRepoId" | "baseBranch">, |
| 123 | baseOid: string, | |
| 118 | 124 | ): Promise<void> { |
| 119 | 125 | const dir = await pullDir(app.db, app.reposRoot, repo, pull); |
| 120 | const out = await runGit(["diff", "--numstat", "--no-color", "--no-renames", `${pull.baseOid}...${pull.headOid}`, "--"], { | |
| 126 | const out = await runGit(["diff", "--numstat", "--no-color", "--no-renames", `${baseOid}...${pull.headOid}`, "--"], { | |
| 121 | 127 | cwd: dir, |
| 122 | 128 | timeoutMs: 30_000, |
| 123 | 129 | signal: app.closing.signal, |
test/review-evidence.test.ts +29 −1
| @@ -36,7 +36,8 @@ beforeAll(() => { | ||
| 36 | 36 | git(work, "add", "."); |
| 37 | 37 | git(work, "commit", "-m", "head"); |
| 38 | 38 | head = git(work, "rev-parse", "HEAD"); |
| 39 | git(work, "push", dir, "main"); | |
| 39 | // main stays at the base, as a pull's base branch does until something lands. | |
| 40 | git(work, "push", dir, `${base}:refs/heads/main`, "HEAD:refs/heads/topic"); | |
| 40 | 41 | }); |
| 41 | 42 | afterAll(() => rmSync(root, { recursive: true, force: true })); |
| 42 | 43 | |
| @@ -118,3 +119,30 @@ test("at most four run at once, and closing the app stops the work and starts no | ||
| 118 | 119 | (Bun as { spawn: unknown }).spawn = real; |
| 119 | 120 | } |
| 120 | 121 | }); |
| 122 | ||
| 123 | test("the stat is taken against the base branch's live tip, not the oid stored when the pull opened", async () => { | |
| 124 | const dir = join(root, "o2", "r.git"); | |
| 125 | mkdirSync(dir, { recursive: true }); | |
| 126 | git(dir, "init", "--bare", "-b", "main"); | |
| 127 | const work = join(root, "work2"); | |
| 128 | mkdirSync(work); | |
| 129 | git(work, "init", "-b", "main"); | |
| 130 | writeFileSync(join(work, "a.txt"), "one\n"); | |
| 131 | git(work, "add", "."); | |
| 132 | git(work, "commit", "-m", "old base"); | |
| 133 | const oldBase = git(work, "rev-parse", "HEAD"); | |
| 134 | git(work, "checkout", "-b", "topic"); | |
| 135 | writeFileSync(join(work, "t.txt"), "t\n"); | |
| 136 | git(work, "add", "."); | |
| 137 | git(work, "commit", "-m", "topic"); | |
| 138 | const topic = git(work, "rev-parse", "HEAD"); | |
| 139 | git(work, "checkout", "main"); | |
| 140 | git(work, "merge", "--no-ff", "-m", "land topic", topic); | |
| 141 | git(work, "push", dir, "main"); | |
| 142 | // The base moved and already holds the head: against the live tip nothing is left to land. | |
| 143 | const app = fakeApp(); | |
| 144 | const pull = pullOf(oldBase, topic); | |
| 145 | expect(diffStatFor(app, { ownerName: "o2", name: "r" }, pull)).toBe("loading"); | |
| 146 | await settled(app); | |
| 147 | expect(diffStatFor(app, { ownerName: "o2", name: "r" }, pull)).toEqual({ files: 0, additions: 0, deletions: 0 }); | |
| 148 | }); |
new above
offline
The tip is read in the base repo, but
computediffs inpullDir. For a sealed cross-repo pull that is the head repo, which may not hold this oid yet. Fall back to the stored oid when the diff fails, and test it.