Public: anyone can read this; the crew can change it.

A fork's drift is cached by both tips, so a warm files page spawns no git #16602

Merged opened this

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

Merged

scout/forge:scout/16266-a-fork-s-drift-is-cached-by-both-tips-so → main 0 commits fork

  • Merged. scout/16266-a-fork-s-drift-is-cached-by-both-tips-so was merged into main as 4bced86.

Review

Sign in to review this change.
2 files changed +83 −8
src/actions/forks.ts +18 −8
@@ -108,7 +108,7 @@ export async function forkDrift(
108108 );
109109 if (head === undefined || base === undefined)
110110 return { ahead: null, behind: null };
111 return divergence(forkDir, base, head);
111 return standingOf(forkDir, base, head);
112112 }
113113
114114 // ---- ranking ----
@@ -162,6 +162,22 @@ export const nothingNewHeading = (n: number): string =>
162162 const standing = new Map<string, ForkDrift>();
163163 const STANDING_MAX = 2_000;
164164
165 /** The drift between two tips, from `standing` when neither has moved since it was read. */
166 async function standingOf(
167 dir: string,
168 base: string,
169 head: string,
170 ): Promise<ForkDrift> {
171 const key = `${base}:${head}`;
172 let drift = standing.get(key);
173 if (drift === undefined) {
174 drift = await divergence(dir, base, head);
175 if (standing.size >= STANDING_MAX) standing.clear();
176 standing.set(key, drift);
177 }
178 return drift;
179 }
180
165181 async function tipAndTime(
166182 dir: string,
167183 branch: string,
@@ -194,13 +210,7 @@ export async function driftOfForks(
194210 const tip = await tipAndTime(dir, fork.defaultBranch);
195211 if (tip === undefined || base === undefined)
196212 return { fork, drift: { ahead: null, behind: null }, pushedAt: null };
197 const key = `${base}:${tip.oid}`;
198 let drift = standing.get(key);
199 if (drift === undefined) {
200 drift = await divergence(dir, base, tip.oid);
201 if (standing.size >= STANDING_MAX) standing.clear();
202 standing.set(key, drift);
203 }
213 const drift = await standingOf(dir, base, tip.oid);
204214 return { fork, drift, pushedAt: tip.pushedAt };
205215 }),
206216 );
test/fork-drift-cache.test.ts added +65
@@ -0,0 +1,65 @@
1 /**
2 * A fork's drift is kept by (its tip, the parent's tip): a warm read spawns no git, a moved tip
3 * reads again.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { forkDrift } from "../src/actions/forks.ts";
10 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { getRepo } from "../src/core/repos.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { promote } from "./helpers/members.ts";
15 import { gitSpawns } from "./helpers/work.ts";
16
17 let dir: string;
18 let app: App;
19 let amy: User;
20 let bob: User;
21
22 const land = async (who: User, owner: string, message: string) => {
23 const res = await commitToBranch(app, who, getRepo(app.db, owner, "lib")!, {
24 message,
25 files: [{ path: `${message}.txt`, content: `${message}\n` }],
26 });
27 expect(res.ok).toBe(true);
28 };
29 const read = () =>
30 forkDrift(app, getRepo(app.db, "bob", "lib")!, getRepo(app.db, "amy", "lib")!);
31
32 beforeAll(async () => {
33 dir = await mkdtemp(join(tmpdir(), "forge-drift-cache-"));
34 app = createApp({ dataDir: dir, port: 0 });
35 const users: User[] = [];
36 for (const username of ["amy", "bob"]) {
37 const made = await createUser(app.db, {
38 origin: "seed", trusted: true, username, email: `${username}@forge.local`,
39 password: "a-good-passphrase", adminIfFirst: users.length === 0,
40 });
41 if (!made.ok) throw new Error("user");
42 promote(app.db, made.value);
43 users.push(made.value);
44 }
45 [amy, bob] = users as [User, User];
46 const made = await createRepo(app, amy, { name: "lib", visibility: "public" });
47 if (!made.ok) throw new Error("repo");
48 await land(amy, "amy", "first");
49 const forked = await forkRepo(app, bob, made.value, undefined, { visibility: "public" });
50 if (!forked.ok) throw new Error("fork");
51 await land(bob, "bob", "mine");
52 });
53 afterAll(async () => {
54 await rm(dir, { recursive: true, force: true });
55 });
56
57 test("a second read spawns no git, and a push to the parent shows in behind", async () => {
58 expect(await read()).toEqual({ ahead: 1, behind: 0 });
59 let again = { ahead: -1, behind: -1 } as Awaited<ReturnType<typeof read>>;
60 expect(await gitSpawns(async () => { again = await read(); })).toBe(0);
61 expect(again).toEqual({ ahead: 1, behind: 0 });
62 await land(amy, "amy", "second");
63 expect(await read()).toEqual({ ahead: 1, behind: 1 });
64 expect(await gitSpawns(async () => { await read(); })).toBe(0);
65 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs