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

Detaching a fork keeps objects its own forks still borrow (repack --keep-unreachable) #16892

Merged opened this

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

Merged

detach-keeps-lent → main 0 commits

  • Merged. detach-keeps-lent was merged into main as df30c44.

Review

Sign in to review this change.
2 files changed +66 −2
src/git/repos.ts +4 −2
@@ -168,8 +168,10 @@ export async function detachFork(
168168 // -a repacks everything into one local pack. Crucially we do NOT pass -l
169169 // (--local): without it, pack-objects copies the borrowed alternate objects
170170 // into this repo, which is exactly the absorption we need before cutting the
171 // alternates link.
172 const repack = await runGit(["repack", "-a", "-d"], {
171 // alternates link. --keep-unreachable: a plain repack never reads
172 // gc.pruneExpire, and without it `-d` drops what this repo's own refs no
173 // longer reach, which a fork of this fork may still point at (#12589).
174 const repack = await runGit(["repack", "-a", "-d", "--keep-unreachable"], {
173175 cwd: forkDir,
174176 timeoutMs: 300_000,
175177 });
test/detach-keeps-lent.test.ts added +62
@@ -0,0 +1,62 @@
1 /**
2 * A fork that lends objects to forks of its own keeps them when it is detached from its parent.
3 * `detachFork` repacks with `-a -d`, which writes only what the fork's own refs reach and drops the
4 * rest of its packs: a plain repack never reads `gc.pruneExpire`, so the setting that keeps a
5 * lender's objects (`keepLentObjects`, #12589) did not hold there. A fork of that fork whose branch
6 * lived only in those packs was left pointing at a commit that no longer existed, which the nightly
7 * bundle cannot back up (scout/forge's two branches, 2026-11-14). Production has such chains
8 * (trevato/cs-engine and trevato/effect-template both borrow and lend).
9 */
10 import { expect, test } from "bun:test";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { detachFork, forkBareRepo, initBareRepo } from "../src/git/repos.ts";
15
16 const git = async (cwd: string, ...args: string[]): Promise<{ code: number; out: string }> => {
17 const p = Bun.spawn(["git", ...args], {
18 cwd,
19 stdout: "pipe",
20 stderr: "pipe",
21 env: { PATH: process.env["PATH"] ?? "/usr/bin:/bin", HOME: cwd, GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_NOSYSTEM: "1", GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" },
22 });
23 const [out, code] = await Promise.all([new Response(p.stdout).text(), p.exited]);
24 return { code, out: out.trim() };
25 };
26
27 test("detaching a fork keeps the objects its own fork reaches, though its refs no longer do", async () => {
28 const root = await mkdtemp(join(tmpdir(), "detach-lent-"));
29 try {
30 // parent <- middle <- child: the child borrows from the middle, the middle from the parent.
31 const parent = join(root, "parent.git");
32 expect((await initBareRepo(parent)).ok).toBe(true);
33 const work = join(root, "w");
34 await git(root, "clone", parent, work);
35 await git(work, "commit", "--allow-empty", "-m", "one");
36 await git(work, "branch", "-M", "main");
37 expect((await git(work, "push", "origin", "main")).code).toBe(0);
38 const middle = join(root, "middle.git");
39 expect((await forkBareRepo(parent, middle)).ok).toBe(true);
40 // A branch pushed to the middle, packed there, and copied to the child when it forks.
41 await git(work, "checkout", "-b", "topic");
42 await git(work, "commit", "--allow-empty", "-m", "only-in-the-middle");
43 expect((await git(work, "push", middle, "topic")).code).toBe(0);
44 const sha = (await git(work, "rev-parse", "topic")).out;
45 expect((await git(middle, "repack", "-a", "-d", "-l")).code).toBe(0);
46 const child = join(root, "child.git");
47 expect((await forkBareRepo(middle, child)).ok).toBe(true);
48 // The middle drops the branch; only the child's ref reaches the commit now.
49 await git(middle, "update-ref", "-d", "refs/heads/topic");
50 await git(middle, "reflog", "expire", "--expire=now", "--all");
51 // The middle's parent goes, so the middle is detached: it repacks to own its objects.
52 expect((await detachFork(middle)).ok).toBe(true);
53 expect((await git(child, "cat-file", "-e", sha)).code).toBe(0);
54 expect((await git(child, "bundle", "create", join(root, "child.bundle"), "--all")).code).toBe(0);
55 // And the middle owns what it borrowed: it reads without its parent.
56 await rm(parent, { recursive: true, force: true });
57 expect((await git(middle, "cat-file", "-e", "main")).code).toBe(0);
58 expect((await git(child, "cat-file", "-e", "main")).code).toBe(0);
59 } finally {
60 await rm(root, { recursive: true, force: true });
61 }
62 });

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

Keyboard shortcuts

Docs