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

A fork's page shows its whole chain of ancestors as a breadcrumb, nearest first, each a link #14661

Merged opened this

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

Merged

scout/forge:scout/14537-a-fork-s-page-shows-its-whole-chain-of-a → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/14537-a-fork-s-page-shows-its-whole-chain-of-a was merged into main as b25bc8f.

Review

Sign in to review this change.
5 files changed +203 −0
docs/features/fork-breadcrumb.md added +13
@@ -0,0 +1,13 @@
1 ---
2 topic: Your apps
3 row: A fork's whole chain of ancestors, root first, in the terminal
4 ---
5 # Fork breadcrumb
6
7 A fork two or more levels down shows where it came from without opening each parent: `a → b → c`, root first, the fork itself last (#14537).
8
9 **In the terminal.** Open the repository, then its forks screen: the first row is the chain, `ann/app → a private forge → cy/app`. Each ancestor you may read is named. One you may not read is "a private forge", never named. A repository with no upstream shows no chain, and a severed fork stops at the cut. A one-level fork shows no chain: its "forked from" line already says it.
10
11 **Long chains.** The root and the nearest three ancestors show; the middle reads `… N more`. Only those are checked for readability.
12
13 The chain is `ancestry` in `src/core/fork_ancestry.ts`: one gate (`canRead`) and one wording, which the repository page's breadcrumb will read too. Tests: `test/fork-breadcrumb.test.ts`.
src/core/fork_ancestry.ts added +42
@@ -0,0 +1,42 @@
1 /**
2 * A fork's chain of ancestors as the viewer may read it, root first (#14537): the one reading the
3 * page's breadcrumb and the terminal's forks screen share, so both hide a repository the viewer
4 * cannot read the same way ("a private forge") and neither names it.
5 *
6 * A chain shows only from two ancestors up: one level is the "forked from" line's. A long chain
7 * keeps its root and its nearest `TAIL` ancestors and says how many it left out; `canRead` runs
8 * only for the ones kept, never for the whole chain.
9 */
10
11 import type { Database } from "bun:sqlite";
12 import { canRead, forkAncestors, type Repo } from "./repos.ts";
13 import type { User } from "./users.ts";
14
15 export const PRIVATE_FORGE = "a private forge";
16 /** Ancestors kept beside the root when a chain is long. */
17 const TAIL = 3;
18
19 export type Ancestor =
20 | { kind: "repo"; name: string; href: string }
21 | { kind: "private" }
22 | { kind: "gap"; hidden: number };
23
24 export function ancestry(db: Database, repo: Repo, viewer: User | undefined): Ancestor[] | null {
25 const up = forkAncestors(db, repo).reverse();
26 if (up.length < 2) return null;
27 const keep = up.length > TAIL + 1 ? [up[0]!, ...up.slice(-TAIL)] : up;
28 const one = (r: Repo): Ancestor =>
29 canRead(db, r, viewer)
30 ? { kind: "repo", name: `${r.ownerName}/${r.name}`, href: `/${r.ownerName}/${r.name}` }
31 : { kind: "private" };
32 if (keep.length === up.length) return up.map(one);
33 return [one(keep[0]!), { kind: "gap", hidden: up.length - keep.length }, ...keep.slice(1).map(one)];
34 }
35
36 /** The terminal's line: "a → b → c", the fork itself last. */
37 export function ancestryLine(parts: readonly Ancestor[], repo: Repo): string {
38 return [
39 ...parts.map((p) => (p.kind === "repo" ? p.name : p.kind === "private" ? PRIVATE_FORGE : `… ${p.hidden} more`)),
40 `${repo.ownerName}/${repo.name}`,
41 ].join(" → ");
42 }
src/core/repos.ts +25
@@ -764,6 +764,31 @@ export function forkParentFor(
764764 : undefined;
765765 }
766766
767 /**
768 * Every ancestor of a fork, nearest first, in one recursive read (no query per
769 * hop). It stops where `fork_parent_id` is null, so a severed fork ends at the
770 * cut; `UNION` and the depth cap keep a damaged cycle from running on.
771 */
772 export function forkAncestors(db: Database, repo: Repo): Repo[] {
773 if (repo.forkParentId === null) return [];
774 const rows = db
775 .query<RepoRow & { depth: number }, [string]>(
776 `WITH RECURSIVE up(id, depth) AS (
777 SELECT ?1, 0
778 UNION
779 SELECT repo.fork_parent_id, up.depth + 1 FROM repo JOIN up ON repo.id = up.id
780 WHERE repo.fork_parent_id IS NOT NULL AND up.depth < 32
781 )
782 SELECT repo.*, user.username AS owner_name, origin.kind AS origin_kind,
783 origin.software AS origin_software, up.depth AS depth
784 FROM up JOIN repo ON repo.id = up.id JOIN user ON user.id = repo.owner_id
785 LEFT JOIN origin ON origin.id = repo.origin_id
786 WHERE up.depth > 0 ORDER BY up.depth`,
787 )
788 .all(repo.id);
789 return rows.map(toRepo);
790 }
791
767792 /**
768793 * Forget a fork's parent. Only once the fork owns its objects (`detachFork`):
769794 * a row without a parent over a store that still borrows one is a fork that
src/tui/screens/forks.ts +7
@@ -26,6 +26,7 @@ import { upstreamCounts } from "../../actions/upstream_counts.ts";
2626 import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, followWords, lastModeChange, setFollowMode, takeOrTell, takeWords } from "../../actions/follow.ts";
2727 import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts";
2828 import { convergeFollowed, followedAs, followedOf } from "../../actions/upstream_sync.ts";
29 import { ancestry, ancestryLine } from "../../core/fork_ancestry.ts";
2930 import { forkOffers, sentOffers } from "../../core/fork_offers.ts";
3031 import { offerSizes, sizeLine } from "../../git/offer_size.ts";
3132 import { agoWords } from "../../actions/follow_words.ts";
@@ -239,7 +240,13 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
239240 }),
240241 );
241242 };
243 /** The chain above this fork, as the page's breadcrumb says it (two ancestors up or more). */
244 const chain = (): ForkRow[] => {
245 const parts = ancestry(f.db, repo, f.user);
246 return parts === null ? [] : [{ repo, name: ancestryLine(parts, repo), words: "", heading: "" }];
247 };
242248 const rows = (): ForkRow[] => [
249 ...chain(),
243250 ...(open === undefined || relation === undefined ? [] : [openRow(open, relation.parent)]),
244251 ...following,
245252 ...offered,
test/fork-breadcrumb.test.ts added +116
@@ -0,0 +1,116 @@
1 /**
2 * A fork's chain of ancestors (#14537): root first, private hidden, cut at a sever, the same line
3 * on the terminal's forks screen.
4 */
5 import { afterAll, beforeAll, describe, 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 { commitToBranch, createRepo, forkRepo, updateRepoSettings } from "../src/actions/repos.ts";
10 import { createApp, type App } from "../src/app.ts";
11 import { ancestry, ancestryLine, PRIVATE_FORGE } from "../src/core/fork_ancestry.ts";
12 import { clearForkParent, forkAncestors, type Repo } from "../src/core/repos.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { createForgeApp } from "../src/tui/forge.ts";
15 import { forksScreen } from "../src/tui/screens/forks.ts";
16 import { headlessTerminal } from "../src/tui/term/terminal.ts";
17 import { promote } from "./helpers/members.ts";
18 import { settleApp } from "./helpers/tui.ts";
19
20 const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => {
21 if (!r.ok) throw new Error(JSON.stringify(r.error));
22 return r.value;
23 };
24
25 let dir: string;
26 let app: App;
27 const u: Record<string, User> = {};
28 let a: Repo, b: Repo, c: Repo;
29 const line = (r: Repo, who?: User) => {
30 const parts = ancestry(app.db, r, who);
31 return parts === null ? null : ancestryLine(parts, r);
32 };
33
34 beforeAll(async () => {
35 dir = await mkdtemp(join(tmpdir(), "forge-breadcrumb-"));
36 app = createApp({ dataDir: dir, port: 0 });
37 for (const name of ["ann", "ben", "cy", "zed"]) {
38 u[name] = must(
39 await createUser(app.db, {
40 origin: "seed",
41 trusted: true,
42 username: name,
43 email: `${name}@f.dev`,
44 password: "a-good-passphrase",
45 adminIfFirst: name === "ann",
46 }),
47 );
48 promote(app.db, u[name]!);
49 }
50 a = must(await createRepo(app, u["ann"]!, { name: "app", visibility: "public" }));
51 must(await commitToBranch(app, u["ann"]!, a, { message: "x", files: [{ path: "x.txt", content: "x\n" }] }));
52 b = must(await forkRepo(app, u["ben"]!, a, undefined, { visibility: "public" }));
53 c = must(await forkRepo(app, u["cy"]!, b, undefined, { visibility: "public" }));
54 }, 60_000);
55
56 afterAll(async () => {
57 app?.close();
58 await rm(dir, { recursive: true, force: true });
59 });
60
61 describe("fork ancestry", () => {
62 test("no upstream shows nothing", () => {
63 expect(ancestry(app.db, a, u["zed"])).toBeNull();
64 });
65
66 test("a one-level fork shows nothing: the 'forked from' line says it", () => {
67 expect(forkAncestors(app.db, b).length).toBe(1);
68 expect(ancestry(app.db, b, u["zed"])).toBeNull();
69 });
70
71 test("three levels read root first, each readable ancestor linked", () => {
72 expect(forkAncestors(app.db, c).map((r) => r.id)).toEqual([b.id, a.id]);
73 expect(line(c, u["zed"])).toBe("ann/app → ben/app → cy/app");
74 expect(ancestry(app.db, c, u["zed"])!.map((p) => (p.kind === "repo" ? p.href : p.kind))).toEqual(["/ann/app", "/ben/app"]);
75 });
76
77 test("a private middle is 'a private forge' to a stranger, named to its owner", async () => {
78 must(await updateRepoSettings(app, u["ben"]!, b, { visibility: "private" }));
79 expect(line(c, u["zed"])).toBe(`ann/app → ${PRIVATE_FORGE} → cy/app`);
80 expect(line(c, undefined)).not.toContain("ben/app");
81 expect(line(c, u["ben"])).toBe("ann/app → ben/app → cy/app");
82 });
83
84 test("the terminal's forks screen leads with the same chain", async () => {
85 const terminal = headlessTerminal(120, 30);
86 const { app: tui, session } = createForgeApp(
87 app,
88 { sshUsername: "zed", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: u["zed"]! },
89 { terminal, exitHooks: false },
90 );
91 tui.start();
92 tui.ctx.push(forksScreen(session, tui.ctx, c));
93 await settleApp(tui);
94 const text = tui.screen.toText();
95 tui.stop?.();
96 expect(text).toContain(`ann/app → ${PRIVATE_FORGE} → cy/app`);
97 expect(text).not.toContain("ben/app");
98 });
99
100 test("a long chain keeps its root and nearest three, and says what it left out", async () => {
101 let tip = c;
102 for (const n of ["ann", "ben", "cy"]) tip = must(await forkRepo(app, u[n]!, tip, `d${tip.name}`, { visibility: "public" }));
103 for (const n of ["zed", "ann"]) tip = must(await forkRepo(app, u[n]!, tip, `e${tip.name}`, { visibility: "public" }));
104 const parts = ancestry(app.db, tip, u["ann"])!;
105 expect(parts.length).toBe(5);
106 expect(parts[1]).toEqual({ kind: "gap", hidden: forkAncestors(app.db, tip).length - 4 });
107 expect(ancestryLine(parts, tip)).toContain("more");
108 });
109
110 test("a severed fork stops at the cut", () => {
111 clearForkParent(app.db, c.id);
112 const fresh = { ...c, forkParentId: null };
113 expect(ancestry(app.db, fresh, u["zed"])).toBeNull();
114 expect(forkAncestors(app.db, fresh)).toEqual([]);
115 });
116 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs