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

A fork or pull opened while a repository is being deleted is refused, and a crash never leaves the delete mark behind #15228

Merged opened this

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

Merged

mason/forge:mason/15034-a-fork-or-pull-opened-while-a-repository → main 4 commits fork 2 threads · 2 unresolved

  • Merged. mason/15034-a-fork-or-pull-opened-while-a-repository was merged into main as b315064.

Review

Sign in to review this change.
12 files changed +353 −5
src/actions/propose_change.ts +1
@@ -121,6 +121,7 @@ function forkFailure(e: Awaited<ReturnType<typeof forkRepo>> extends infer R ? (
121121 case "already_forked":
122122 case "fork_failed":
123123 case "limit":
124 case "deleting":
124125 return { kind: "fork", reason: e.replace(/_/g, " ") };
125126 case "not_found":
126127 return "not_found";
src/core/migration-pins/20261121170000-repo-deleting.pin added +1
@@ -0,0 +1,1 @@
1 cb43e57e72dc8fb0e1dfb4bcc5c7efac358713b4a0948a6a286c4e981ccdee33
src/core/migrations/20261121170000-repo-deleting.ts added +6
@@ -0,0 +1,6 @@
1 // When a delete of this repository began (#15034): while it stands, a fork of it or a pull
2 // from or into it is refused, so neither lands between the delete's checks and its removal.
3 // Time-bound in `repo_deleting.ts`, so a crash mid-delete never leaves it standing.
4 export default `
5 ALTER TABLE repo ADD COLUMN deleting_at TEXT;
6 `;
src/core/pulls.ts +16 −2
@@ -48,6 +48,7 @@ import { approvalRecord, listReviews, reviewSummary, type ApprovalRecord } from
4848 import { checkStale, checksHold } from "../checks/stale.ts";
4949 import { checkSummary } from "./runs.ts";
5050 import { writeStamp } from "./write_clock.ts";
51 import { isDeleting } from "./repo_deleting.ts";
5152
5253 export interface Pull extends Issue {
5354 readonly baseBranch: string;
@@ -166,7 +167,9 @@ export type CreatePullError =
166167 | "unknown_head"
167168 | "title_too_long"
168169 | "body_too_long"
169 | "nothing_to_merge";
170 | "nothing_to_merge"
171 /** The base or the head repository is being deleted (`repo_deleting.ts`). */
172 | "deleting";
170173
171174 export interface CreatePullInput {
172175 /** The base (upstream) repo the PR is created in. */
@@ -251,6 +254,11 @@ async function followBase(
251254 if (tip !== undefined) await writeRef(headDir, `refs/heads/${branch}`, tip);
252255 }
253256
257 /** The repository a pull's head lives in: a fork's, or the base's own. */
258 function headRepoIdOf(input: Pick<CreatePullInput, "repoId" | "headRepoId">): string {
259 return input.headRepoId ?? input.repoId;
260 }
261
254262 export async function createPull(
255263 db: Database,
256264 dir: string,
@@ -263,6 +271,7 @@ export async function createPull(
263271 input.headRepoId !== undefined && input.headRepoId !== input.repoId;
264272 if (!crossRepo && input.baseBranch === input.headBranch)
265273 return err("same_branch");
274 if (isDeleting(db, [input.repoId, headRepoIdOf(input)])) return err("deleting");
266275
267276 const baseOid = await resolveOid(dir, `refs/heads/${input.baseBranch}`);
268277 if (!baseOid) return err("unknown_base");
@@ -290,6 +299,11 @@ export async function createPull(
290299 if (!headOid) return err("unknown_head");
291300 if ((await aheadCount(at, baseOid, headOid)) === 0)
292301 return err("nothing_to_merge");
302 // After the last await and before the row: a delete that began meanwhile
303 // has already looked for pulls naming its repository, and would not see
304 // this one (`repo_deleting.ts`).
305 if (isDeleting(db, [input.repoId, headRepoIdOf(input)]))

This re-check after the last await is untested: no test would fail if it were removed. Add one that marks the repo while createPull is in flight.

306 return err("deleting");
293307
294308 // The fix for a sealed report is sealed with it: its diff says where
295309 // the hole is (`sealed.ts`).
@@ -309,7 +323,7 @@ export async function createPull(
309323 issue.value.id,
310324 );
311325
312 const headRepoId = input.headRepoId ?? input.repoId;
326 const headRepoId = headRepoIdOf(input);
313327 db.query(
314328 `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, reverts_issue_id, variant_group)
315329 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)`,
src/core/repo_deleting.ts added +42
@@ -0,0 +1,42 @@
1 /**
2 * The delete mark (#15034): `deleteRepo` sets it before its first await, and
3 * while it stands a fork of the repository, or a pull from or into it, is
4 * refused with `deleting` — so neither can land between the delete's checks
5 * (no pull names it as head, every borrower detached) and its removal.
6 *
7 * The mark is time-bound: it stands for `DELETING_MS` from when it was last
8 * set, and the delete sets it again after each slow step. A process that dies
9 * mid-delete leaves a stale mark nobody reads as standing, so a crash never
10 * leaves a repository that can be neither forked nor pulled.
11 */
12
13 import type { Database } from "bun:sqlite";
14
15 /** How long a mark stands without being set again. */
16 export const DELETING_MS = 15 * 60_000;
17
18 /** Set (or refresh) the mark on `repoId`. Synchronous: call it before any await. */
19 export function markDeleting(db: Database, repoId: string, now: number = Date.now()): void {
20 db.query("UPDATE repo SET deleting_at = ?1 WHERE id = ?2").run(new Date(now).toISOString(), repoId);
21 }
22
23 /** Take the mark back: the delete was refused and the repository stays. */
24 export function clearDeleting(db: Database, repoId: string): void {
25 db.query("UPDATE repo SET deleting_at = NULL WHERE id = ?1").run(repoId);
26 }
27
28 /** Whether any of `repoIds` carries a mark set within `DELETING_MS` of `now`. */
29 export function isDeleting(db: Database, repoIds: readonly string[], now: number = Date.now()): boolean {
30 const since = new Date(now - DELETING_MS).toISOString();
31 return repoIds.some(
32 (repoId) =>
33 db
34 .query<{ n: number }, [string, string]>(
35 "SELECT 1 AS n FROM repo WHERE id = ?1 AND deleting_at IS NOT NULL AND deleting_at > ?2",
36 )
37 .get(repoId, since) !== null,
38 );
39 }
40
41 /** What a refused fork or pull says, in every surface's words. */
42 export const DELETING_SAYS = "the repository is being deleted";
src/core/repos.ts +44 −1
@@ -27,6 +27,7 @@ import { createLog } from "../kernel/log.ts";
2727 import { err, ok, type Result } from "../kernel/result.ts";
2828 import { appsNaming, isAppMember } from "./app_members.ts";
2929 import { forgetListings } from "./listings.ts";
30 import { clearDeleting, isDeleting, markDeleting } from "./repo_deleting.ts";
3031 import { forgetRepoCursors } from "./origins.ts";
3132 import { NOT_AGENT, NOT_ARCHIVED, NOT_UNCLAIMED } from "./offered.ts";
3233 import { likeEscape, NOT_STANDING, pageLimit } from "./issues.ts";
@@ -443,7 +444,9 @@ export type ForkRepoError =
443444 | "already_forked"
444445 | "fork_failed"
445446 /** The owner's forks in the last day reached `perDay` (#888). */
446 | "limit";
447 | "limit"
448 /** The parent is being deleted (`repo_deleting.ts`). */
449 | "deleting";
447450
448451 const DAY_MS = 86_400_000;
449452
@@ -464,6 +467,9 @@ export function forksSince(db: Database, ownerId: string, since: string): string
464467 /** A marker the row's transaction throws when the day's forks are made. */
465468 class OverLimit extends Error {}
466469
470 /** A marker the row's transaction throws when the parent's delete began during the copy. */
471 class ParentDeleting extends Error {}
472
467473 /**
468474 * Fork a repo into another owner. The git objects are shared with the parent
469475 * via alternates (see forkBareRepo), so this is instant and storage-bounded.
@@ -509,6 +515,7 @@ export async function forkRepo(
509515 ((options.perDay !== undefined && forksSince(db, ownerId, since).length >= options.perDay) ||
510516 (options.forgePerDay !== undefined && contributionForksSince(db, since) >= options.forgePerDay));
511517 if (overLimit()) return err("limit");
518 if (isDeleting(db, [parent.id], now)) return err("deleting");
512519 if (!isSafeName(forkName)) return err("invalid_name");
513520 const owner = db
514521 .query<
@@ -554,6 +561,10 @@ export async function forkRepo(
554561 // Counted again with the write: the git copy above took a moment,
555562 // and another fork may have landed in it.
556563 if (overLimit()) throw new OverLimit();
564 // Asked again with the write: a delete that began during the copy
565 // listed its borrowers without this one, and would pull its objects
566 // out from under it.
567 if (isDeleting(db, [parent.id])) throw new ParentDeleting();
557568 db.query(
558569 `INSERT INTO repo (id, owner_id, name, visibility, description, default_branch, fork_parent_id, sealed_for)
559570 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)`,
@@ -581,6 +592,7 @@ export async function forkRepo(
581592 );
582593 } catch (error) {
583594 if (error instanceof OverLimit) return err("limit");
595 if (error instanceof ParentDeleting) return err("deleting");
584596 throw error;
585597 }
586598 return ok(getRepoById(db, repoId)!);
@@ -1151,11 +1163,40 @@ export function listReposByOwner(
11511163 * rule. A sealed repository's row goes with its base's (`ON DELETE CASCADE`),
11521164 * so its directory is removed after the base's row, leaving no directory
11531165 * without a row to hold the name.
1166 * The whole delete runs under its mark (`repo_deleting.ts`): a fork of it, or
1167 * a pull from or into it, opened meanwhile is refused with `deleting`.
11541168 */
11551169 export async function deleteRepo(
11561170 db: Database,
11571171 reposRoot: string,
11581172 repo: Repo,
1173 ): Promise<Result<void, "delete_failed" | "has_pulls">> {
1174 // Before the first await: from here no fork of it and no pull from or into
1175 // it lands (`repo_deleting.ts`), so the checks below stay true until the end.
1176 markDeleting(db, repo.id);
1177 let deleted: Result<void, "delete_failed" | "has_pulls">;
outdated

If removeRepo throws, the mark is left standing for DELETING_MS. Use try/catch, clearing the mark on a throw and rethrowing, and test it.

1178 try {
1179 deleted = await removeRepo(db, reposRoot, repo);
1180 } catch (error) {
1181 // A delete that threw is refused too: its mark must not stand.
1182 clearDeleting(db, repo.id);
1183 throw error;
1184 }
1185 // A refused delete leaves the repository as it was, mark and all.
1186 if (!deleted.ok) clearDeleting(db, repo.id);
1187 return deleted;
1188 }
1189
1190 /**
1191 * `deleteRepo`'s work, under its mark. Everything that can refuse the delete
1192 * comes before the row goes; the sealed repositories' directories go only
1193 * after it, so a refused delete destroys nothing but its own directory's
1194 * files where `rm` got partway.
1195 */
1196 async function removeRepo(
1197 db: Database,
1198 reposRoot: string,
1199 repo: Repo,
11591200 ): Promise<Result<void, "delete_failed" | "has_pulls">> {
11601201 // A pull names its head by this row, and a pull with no head row reads as
11611202 // one from its base's own branch, which runs without approval. So a fork a
@@ -1183,6 +1224,8 @@ export async function deleteRepo(
11831224 return err("delete_failed");
11841225 }
11851226 clearForkParent(db, fork.id);
1227 // A repack can take a while: the mark is time-bound, so it is set again.
1228 markDeleting(db, repo.id);
11861229 }
11871230
11881231 const dir = repoDir(reposRoot, repo.ownerName, repo.name);
src/http/api/upstream.ts +3
@@ -6,6 +6,7 @@
66 * `actions/repos.ts`.
77 */
88
9 import { DELETING_SAYS } from "../../core/repo_deleting.ts";
910 import { nestedForks } from "../../actions/fork_nested.ts";
1011 import { forksSince } from "../../core/repos.ts";
1112 import { forkRepo, FORKS_PER_DAY, repositoryLimitMessage } from "../../actions/repos.ts";
@@ -167,6 +168,8 @@ export function upstreamRoutes(router: Router<Handler>): void {
167168 ? tryAgain("limit", 429, `at most ${FORKS_PER_DAY} forks a day: try again tomorrow`, forkWait(ctx.app.db, ctx.auth.user!.id))
168169 : forked.error === "fork_failed"
169170 ? apiError("fork_failed", 422, "the fork could not be created")
171 : forked.error === "deleting"
172 ? apiError("deleting", 409, DELETING_SAYS)
170173 : respondAccess(forked.error, 422);
171174 return json(forked.value, 201);
172175 });
src/http/mcp/tools/fork.ts +2
@@ -1,4 +1,5 @@
11 import { forkRepo, FORKS_PER_DAY, repositoryLimitMessage } from "../../../actions/repos.ts";
2 import { DELETING_SAYS } from "../../../core/repo_deleting.ts";
23 import { VERIFY_FIRST } from "../../../actions/verify_words.ts";
34 import { GUEST_REFUSALS, isGuestRefusal } from "../../../core/contribute.ts";
45 import { NOT_A_MEMBER } from "../../../core/roles.ts";
@@ -70,6 +71,7 @@ export const tool: Tool = {
7071 if (e === "limit") return bad(e, `at most ${FORKS_PER_DAY} forks a day: try again tomorrow`);
7172 if (e === "already_forked") return bad(e, "you already have a fork of this repository: commit to it");
7273 if (e === "not_found") return bad(e, "no such repository");
74 if (e === "deleting") return bad(e, DELETING_SAYS);
7375 return bad(e, "the fork could not be made");
7476 },
7577 };
src/tui/screens/common.ts +2
@@ -7,6 +7,7 @@
77 * of them. Nothing here knows about repos or issues — that is the point.
88 */
99
10 import { DELETING_SAYS } from "../../core/repo_deleting.ts";
1011 import { CHECK_STALE_REFUSAL } from "../../checks/stale.ts";
1112 import { PASSWORD_TOO_LONG } from "../../core/hash-gate.ts";
1213 import { refusalText } from "../../http/refusal.ts";
@@ -1016,6 +1017,7 @@ const MESSAGES: Record<string, string> = {
10161017 unknown_head: "the head branch does not exist",
10171018 same_branch: "the base and head branches are the same",
10181019 nothing_to_merge: "there is nothing to merge",
1020 deleting: DELETING_SAYS,
10191021 invalid_branch: "that branch name is not allowed",
10201022 conflict: "the branches conflict",
10211023 closed: "this pull is closed",
src/web/pages/pulls.ts +2
@@ -5,6 +5,7 @@
55 * `src/web/studio/lists.ts`).
66 */
77
8 import { DELETING_SAYS } from "../../core/repo_deleting.ts";
89 import { openPull } from "../../actions/pulls.ts";
910 import { VERIFY_FIRST } from "../../actions/verify_words.ts";
1011 import { GUEST_REFUSALS } from "../../core/contribute.ts";
@@ -216,6 +217,7 @@ const COMPOSE_ERRORS: Record<string, string> = {
216217 unknown_head: "That compare branch does not exist.",
217218 nothing_to_merge: "The compare branch has no commits beyond base.",
218219 invalid_branch: "That branch name is not valid.",
220 deleting: `Not opened: ${DELETING_SAYS}.`,
219221 sealed_in_public:
220222 "This fixes a sealed security report, and this branch is public: anyone can read the fix, and with it where the hole is. Work on it privately — open the report and choose “Work on this privately” — or publish anyway below.",
221223 };
src/web/pages/repo.ts +5 −2
@@ -8,6 +8,7 @@
88 * path segment and can never smuggle a path of its own.
99 */
1010
11 import { DELETING_SAYS } from "../../core/repo_deleting.ts";
1112 import { downloadHeaders } from "../../drop/store.ts";
1213 import { WAVE_CAP } from "../render/waveform.ts";
1314 import { repoLine } from "../describe.ts";
@@ -392,8 +393,10 @@ export async function forkSubmit(ctx: Ctx): Promise<Response> {
392393 : forked.error === "storage_limit"
393394 ? `Not made: ${storageLimitMessage(ctx.app, ctx.auth.user.id)}.`
394395 : forked.error === "limit"
395 ? `That is today's forks made (${FORKS_PER_DAY} a day). Try again tomorrow.`
396 : "That fork could not be created.";
396 ? `That is today's forks made (${FORKS_PER_DAY} a day). Try again tomorrow.`
397 : forked.error === "deleting"
398 ? `Not made: ${DELETING_SAYS}.`
399 : "That fork could not be created.";
397400 return flashTo(ctx, repoBase(repo), { tone: "danger", message: reason });
398401 }
399402 return flashTo(ctx, repoBase(forked.value), {
test/repo-deleting.test.ts added +229
@@ -0,0 +1,229 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { chmod, mkdtemp, rm } from "node:fs/promises";
3 import { existsSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createPull } from "../src/core/pulls.ts";
8 import { clearDeleting, DELETING_MS, DELETING_SAYS, isDeleting, markDeleting } from "../src/core/repo_deleting.ts";
9 import { createRepo, deleteRepo, forkRepo, getRepoById, type Repo } from "../src/core/repos.ts";
10 import { repoDir } from "../src/git/repos.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12 import { commitFiles } from "../src/git/write.ts";
13 import { signUpProven as signUp } from "./helpers/verified.ts";
14 import { tokenFor } from "./helpers/tokens.ts";
15 import { promote } from "./helpers/members.ts";
16
17 /**
18 * The delete mark (#15034): `deleteRepo` sets it before its first await, so a
19 * fork or a pull opened while the delete runs is refused with `deleting`; a
20 * refused delete takes it back and destroys nothing; and a mark a crash left
21 * behind stops standing on its own.
22 */
23
24 /** As root, a read-only directory does not stop a write: the failure cannot be made. */
25 const unprivileged = process.getuid?.() !== 0;
26
27 describe("a repository being deleted", () => {
28 let dataDir: string;
29 let app: App;
30 let servers: Servers;
31 let n = 0;
32
33 const user = async (prefix: string) => {
34 n += 1;
35 const u = await signUp(app, {
36 username: `${prefix}${n}`,
37 email: `${prefix}${n}@forge.test`,
38 password: "a-long-passphrase",
39 });
40 if (!u.ok) throw new Error("signup failed");
41 return u.value;
42 };
43
44 const commit = async (repo: Repo, branch: string, subject: string) => {
45 const c = await commitFiles(repoDir(app.reposRoot, repo.ownerName, repo.name), {
46 branch,
47 message: subject,
48 files: [{ path: `${subject}.txt`, content: subject }],
49 author: { name: "T", email: "[email protected]" },
50 });
51 if (!c.ok) throw new Error("commit failed");
52 };
53
54 /** A public parent with one commit on main. */
55 const parent = async () => {
56 const owner = await user("owner");
57 const made = await createRepo(app.db, app.reposRoot, {
58 ownerId: owner.id,
59 name: "parent",
60 visibility: "public",
61 });
62 if (!made.ok) throw new Error("create failed");
63 await commit(made.value, "main", "one");
64 return { owner, repo: made.value };
65 };
66
67 const fork = async (of: Repo) => {
68 const who = await user("forker");
69 const r = await forkRepo(app.db, app.reposRoot, of, who.id);
70 if (!r.ok) throw new Error(`fork failed: ${r.error}`);
71 return { who, repo: r.value };
72 };
73
74 /** A pull from `head`'s `work` branch into `base`'s main. */
75 const pullFrom = (base: Repo, head: Repo, authorId: string) =>
76 createPull(app.db, repoDir(app.reposRoot, base.ownerName, base.name), {
77 repoId: base.id,
78 authorId,
79 title: "work",
80 baseBranch: "main",
81 headBranch: "work",
82 headRepoId: head.id,
83 headDir: repoDir(app.reposRoot, head.ownerName, head.name),
84 });
85
86 beforeAll(async () => {
87 dataDir = await mkdtemp(join(tmpdir(), "forge-deleting-"));
88 app = createApp({ dataDir, port: 0 });
89 servers = await serve(app);
90 });
91
92 afterAll(async () => {
93 servers.stop();
94 app.close();
95 await rm(dataDir, { recursive: true, force: true });
96 });
97
98 test("the mark is set before the delete's first await, and a fork asked for meanwhile is refused", async () => {
99 const { repo } = await parent();
100 const other = await user("late");
101 const deleting = deleteRepo(app.db, app.reposRoot, repo);
102 expect(isDeleting(app.db, [repo.id])).toBe(true);
103 expect(await forkRepo(app.db, app.reposRoot, repo, other.id)).toEqual({ ok: false, error: "deleting" });
104 expect(await deleting).toEqual({ ok: true, value: undefined });
105 expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false);
106 });
107
108 test("a fork whose copy was under way when the delete began is refused at its write, and leaves no directory", async () => {
109 const { repo } = await parent();
110 const other = await user("inflight");
111 // The mark alone, set while the copy runs: the write asks again.
112 const forking = forkRepo(app.db, app.reposRoot, repo, other.id);
113 markDeleting(app.db, repo.id);
114 expect(await forking).toEqual({ ok: false, error: "deleting" });
115 expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false);
116 });
117
118 test("a fork racing the whole delete never lands a row that borrows a removed store", async () => {
119 const { repo } = await parent();
120 const other = await user("racer");
121 const forking = forkRepo(app.db, app.reposRoot, repo, other.id);
122 const deleting = deleteRepo(app.db, app.reposRoot, repo);
123 const forked = await forking;
124 expect(await deleting).toEqual({ ok: true, value: undefined });
125 // Refused at its write, or its copy failed as the store went: either way, no fork.
126 expect(forked.ok).toBe(false);
127 expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false);
128 });
129
130 test("a pull from a fork being deleted is refused, and so is one into a base being deleted", async () => {
131 const { repo: base } = await parent();
132 const { who, repo: head } = await fork(base);
133 await commit(head, "work", "two");
134 markDeleting(app.db, head.id);
135 expect(await pullFrom(base, head, who.id)).toEqual({ ok: false, error: "deleting" });
136 clearDeleting(app.db, head.id);
137 markDeleting(app.db, base.id);
138 expect(await pullFrom(base, head, who.id)).toEqual({ ok: false, error: "deleting" });
139 });
140
141 test("a pull whose head was being read when the delete began is refused before its row", async () => {
142 const { repo: base } = await parent();
143 const { who, repo: head } = await fork(base);
144 await commit(head, "work", "two");
145 // The mark alone, set after createPull's first check passed: the check
146 // after its last await is what refuses.
147 const opening = pullFrom(base, head, who.id);
148 markDeleting(app.db, head.id);
149 expect(await opening).toEqual({ ok: false, error: "deleting" });
150 const rows = app.db
151 .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM pull WHERE head_repo_id = ?1")
152 .get(head.id);
153 expect(rows?.n).toBe(0);
154 });
155
156 test("a delete that throws takes its mark back before the error goes on", async () => {
157 const { repo } = await parent();
158 // A name no path may hold: the delete throws as it reaches the directory.
159 const broken: Repo = { ...repo, name: "../escape" };
160 await expect(deleteRepo(app.db, app.reposRoot, broken)).rejects.toThrow();
161 expect(isDeleting(app.db, [repo.id])).toBe(false);
162 expect(getRepoById(app.db, repo.id)).toBeDefined();
163 });
164
165 test("the API answers a fork of a repository being deleted with 409 deleting", async () => {
166 const { repo } = await parent();
167 const other = await user("api");
168 promote(app.db, other);
169 markDeleting(app.db, repo.id);
170 const res = await fetch(`${servers.url}/api/v1/repos/${repo.ownerName}/${repo.name}/forks`, {
171 method: "POST",
172 headers: {
173 authorization: `Bearer ${tokenFor(app, other.username, "rw")}`,
174 "content-type": "application/json",
175 },
176 body: "{}",
177 });
178 expect(res.status).toBe(409);
179 expect(JSON.stringify(await res.json())).toContain(DELETING_SAYS);
180 expect(existsSync(repoDir(app.reposRoot, other.username, repo.name))).toBe(false);
181 });
182
183 test("a refused delete takes its mark back: the repository forks and pulls again", async () => {
184 const { repo: base } = await parent();
185 const { who, repo: head } = await fork(base);
186 await commit(head, "work", "two");
187 const opened = await pullFrom(base, head, who.id);
188 expect(opened.ok).toBe(true);
189 expect(await deleteRepo(app.db, app.reposRoot, head)).toEqual({ ok: false, error: "has_pulls" });
190 expect(isDeleting(app.db, [head.id])).toBe(false);
191 const again = await user("again");
192 expect((await forkRepo(app.db, app.reposRoot, head, again.id)).ok).toBe(true);
193 });
194
195 test("a mark a crash left behind stops standing once its time is up", async () => {
196 const { repo } = await parent();
197 const crashedAt = Date.now() - DELETING_MS - 1000;
198 markDeleting(app.db, repo.id, crashedAt);
199 // Nothing cleared it: the process that set it is gone.
200 expect(getRepoById(app.db, repo.id)).toBeDefined();
201 expect(isDeleting(app.db, [repo.id])).toBe(false);
202 const other = await user("after");
203 expect((await forkRepo(app.db, app.reposRoot, repo, other.id)).ok).toBe(true);
204 // The same mark, still within its time, refuses.
205 markDeleting(app.db, repo.id, Date.now() - DELETING_MS + 60_000);
206 const third = await user("within");
207 expect(await forkRepo(app.db, app.reposRoot, repo, third.id)).toEqual({ ok: false, error: "deleting" });
208 });
209
210 test.skipIf(!unprivileged)("a delete refused at a detach destroys no sealed repository", async () => {
211 const { repo: base } = await parent();
212 const reporter = await user("sealer");
213 const sealed = await forkRepo(app.db, app.reposRoot, base, reporter.id, undefined, { sealed: true });
214 if (!sealed.ok) throw new Error(`sealed fork failed: ${sealed.error}`);
215 const { repo: plain } = await fork(base);
216 const sealedDir = repoDir(app.reposRoot, sealed.value.ownerName, sealed.value.name);
217 const pack = join(repoDir(app.reposRoot, plain.ownerName, plain.name), "objects", "pack");
218 await chmod(pack, 0o555);
219 try {
220 expect(await deleteRepo(app.db, app.reposRoot, base)).toEqual({ ok: false, error: "delete_failed" });
221 } finally {
222 await chmod(pack, 0o755);
223 }
224 expect(existsSync(join(sealedDir, "HEAD"))).toBe(true);
225 expect(getRepoById(app.db, sealed.value.id)?.sealedFor).toBe(base.id);
226 expect(getRepoById(app.db, base.id)).toBeDefined();
227 expect(isDeleting(app.db, [base.id])).toBe(false);
228 });
229 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs