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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/4779-the-review-diff-stat-uses-the-pull-s-sto → main 0 commits fork 1 thread · 1 unresolved

  • Merged. mason/4779-the-review-diff-stat-uses-the-pull-s-sto was merged into main as 8a38aa9.

Review

Sign in to review this change.
3 files changed +63 −5
src/git/tips.ts +24
@@ -4,6 +4,7 @@
44 * `packed-refs`. Anything else (a symbolic ref, a missing file) is simply
55 * absent here, and the caller asks git.
66 */
7 import { readFileSync } from "node:fs";
78 import { join } from "node:path";
89 import { isSafeRef } from "./guard.ts";
910
@@ -41,3 +42,26 @@ export async function readTips(
4142 }
4243 return out;
4344 }
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";
1212 import type { App } from "../../app.ts";
1313 import { runGit } from "../../git/env.ts";
1414 import { isSafeOid } from "../../git/guard.ts";
15 import { readTipSync } from "../../git/tips.ts";
16 import { repoDir } from "../../git/repos.ts";
1517
1618 export interface DiffStat {
1719 readonly files: number;
@@ -85,10 +87,13 @@ export function diffStatFor(
8587 pull: Pick<Pull, "baseOid" | "headOid" | "sealed" | "crossRepo" | "headRepoId" | "baseBranch">,
8688 ): DiffStat | "loading" | "failed" {
8789 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;

The tip is read in the base repo, but compute diffs in pullDir. 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.

93 const key = `${baseOid}:${pull.headOid}`;
8994 const kept = w.kept.get(key);
9095 if (kept !== undefined) return kept;
91 if (!isSafeOid(pull.baseOid) || !isSafeOid(pull.headOid)) return "failed";
96 if (!isSafeOid(baseOid) || !isSafeOid(pull.headOid)) return "failed";
9297 const failedUntil = w.failed.get(key);
9398 if (failedUntil !== undefined) {
9499 if (failedUntil > Date.now()) return "failed";
@@ -98,7 +103,7 @@ export function diffStatFor(
98103 w.inflight.add(key);
99104 w.queued.set(key, async () => {
100105 try {
101 await compute(app, w, key, repo, pull);
106 await compute(app, w, key, repo, pull, baseOid);
102107 } catch {
103108 if (!app.closing.signal.aborted) w.failed.set(key, Date.now() + RETRY_MS);
104109 } finally {
@@ -115,9 +120,10 @@ async function compute(
115120 key: string,
116121 repo: { ownerName: string; name: string },
117122 pull: Pick<Pull, "baseOid" | "headOid" | "sealed" | "crossRepo" | "headRepoId" | "baseBranch">,
123 baseOid: string,
118124 ): Promise<void> {
119125 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}`, "--"], {
121127 cwd: dir,
122128 timeoutMs: 30_000,
123129 signal: app.closing.signal,
test/review-evidence.test.ts +29 −1
@@ -36,7 +36,8 @@ beforeAll(() => {
3636 git(work, "add", ".");
3737 git(work, "commit", "-m", "head");
3838 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");
4041 });
4142 afterAll(() => rmSync(root, { recursive: true, force: true }));
4243
@@ -118,3 +119,30 @@ test("at most four run at once, and closing the app stops the work and starts no
118119 (Bun as { spawn: unknown }).spawn = real;
119120 }
120121 });
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 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs