Public: anyone can read this; the crew can change it.
Deleting or transferring a repository counts its sealed forks as borrowers of its object store #15054
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15033-deleting-or-transferring-a-repository-co → main 8 commits fork 2 threads · 2 unresolved
13 files
changed +267
−15
docs/features/repo-dir-in-the-way.md added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | --- | |
| 2 | topic: What a forge is | |
| 3 | row: A leftover folder refuses a new repository's name, and says so | |
| 4 | --- | |
| 5 | # A leftover folder refuses a new repository's name, and says so | |
| 6 | ||
| 7 | `createRepo` claims the repository's directory with a plain `mkdir` before git runs. When the | |
| 8 | directory is already there but no repository row owns it (a create still under way, or a folder | |
| 9 | left behind), the create is refused as `dir_in_the_way`, not `name_taken`: no repository has that | |
| 10 | name, so "already has a repository by that name" would send the person looking for one that is | |
| 11 | not there. | |
| 12 | ||
| 13 | - The web (`/new`, `newRepoSubmit` in `src/web/pages/site.ts`) shows `REPO_DIR_IN_THE_WAY` from | |
| 14 | `src/actions/repo_words.ts` beside the Name field: "No repository has that name, but a folder on | |
| 15 | the forge's disk already holds it … Pick another name: this one stays held until that folder is removed from the disk." | |
| 16 | - The terminal (both doors): the new-repository screen (`src/tui/screens/newrepo.ts`) shows the | |
| 17 | refusal as a danger toast, worded by `dir_in_the_way` in the messages of | |
| 18 | `src/tui/screens/common.ts`: "a folder on disk already holds that name, with no repository | |
| 19 | behind it: pick another". | |
| 20 | ||
| 21 | Deleting a base repository now also removes its sealed repositories' directories, so a delete | |
| 22 | leaves no such leftover behind. | |
| 23 | ||
| 24 | Try it: with a folder already under the owner's repository directory and no repository by that | |
| 25 | name, open `/new`, give that name and submit; the message above sits beside Name. In the terminal, | |
| 26 | press the new-repository key and give the same name. | |
| 27 | ||
| 28 | Pinned by `test/repo-borrowers.test.ts` (a leftover directory, and two creates racing for one name, | |
| 29 | both answer `dir_in_the_way`). |
scripts/doorless-allow.json −1
| @@ -86,7 +86,6 @@ | ||
| 86 | 86 | "src/web/pages/security.ts", |
| 87 | 87 | "src/web/pages/settings.ts", |
| 88 | 88 | "src/web/pages/signup-passkey.ts", |
| 89 | "src/web/pages/site.ts", | |
| 90 | 89 | "src/web/pages/spend-tab.ts", |
| 91 | 90 | "src/web/pages/spend.ts", |
| 92 | 91 | "src/web/pages/start_claim.ts", |
src/actions/apps.ts +2 −1
| @@ -11,7 +11,7 @@ import { getVariant } from "../core/variants.ts"; | ||
| 11 | 11 | import { pullDir } from "../core/pulls.ts"; |
| 12 | 12 | import { mayView, sealedPreviewPull } from "../core/sealed.ts"; |
| 13 | 13 | import { deriveAppName } from "../web/pages/apps-name.ts"; |
| 14 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; | |
| 14 | import { REPO_DIR_IN_THE_WAY, REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; | |
| 15 | 15 | import { NO_SUCH_AGENT } from "./crew_limit_words.ts"; |
| 16 | 16 | import { requireVerified } from "./verify.ts"; |
| 17 | 17 | import { seatCrew } from "../spawn/starter_crew.ts"; |
| @@ -521,6 +521,7 @@ export const NEW_APP_ERRORS: Record<string, string> = { | ||
| 521 | 521 | name_taken: "That owner already has a repository by that name.", |
| 522 | 522 | invalid_name: REPO_NAME_RULES, |
| 523 | 523 | git_init_failed: REPO_INIT_FAILED, |
| 524 | dir_in_the_way: REPO_DIR_IN_THE_WAY, | |
| 524 | 525 | invalid_slug: |
| 525 | 526 | "That name makes no usable hostname — start it with a letter or digit.", |
| 526 | 527 | invalid_secret: |
src/actions/migrate.ts +3 −1
| @@ -73,7 +73,7 @@ import { | ||
| 73 | 73 | } from "../origins/jobs.ts"; |
| 74 | 74 | import type { Provider } from "../origins/types.ts"; |
| 75 | 75 | import { requireUser } from "./access.ts"; |
| 76 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; | |
| 76 | import { REPO_DIR_IN_THE_WAY, REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; | |
| 77 | 77 | import { enableApp } from "./apps.ts"; |
| 78 | 78 | import { addToCrew } from "./crew.ts"; |
| 79 | 79 | import { cleanOrigin, migrationBrief, migrationDirectionFor, setMigration } from "../apps/migration_mode.ts"; |
| @@ -322,6 +322,8 @@ export function migrateMessage(error: MigrateError, limit = AT_REPOSITORY_LIMIT) | ||
| 322 | 322 | return REPO_NAME_RULES; |
| 323 | 323 | case "git_init_failed": |
| 324 | 324 | return REPO_INIT_FAILED; |
| 325 | case "dir_in_the_way": | |
| 326 | return REPO_DIR_IN_THE_WAY; | |
| 325 | 327 | case "forbidden_owner": |
| 326 | 328 | return "You cannot create repositories for that owner."; |
| 327 | 329 | case "owner_admin": |
src/actions/repo_words.ts +4
| @@ -9,3 +9,7 @@ export const REPO_NAME_RULES = "Names start with a letter or digit; no spaces, n | ||
| 9 | 9 | |
| 10 | 10 | /** `createRepo` failed as `git_init_failed`. */ |
| 11 | 11 | export const REPO_INIT_FAILED = "The repository could not be initialised on disk."; |
| 12 | ||
| 13 | /** `createRepo` refused as `dir_in_the_way`: a directory, but no repository. */ | |
| 14 | export const REPO_DIR_IN_THE_WAY = | |
| 15 | "No repository has that name, but a folder on the forge's disk already holds it (a create still under way, or one left behind). Pick another name: this one stays held until that folder is removed from the disk."; |
src/core/repos.ts +71 −9
| @@ -9,7 +9,8 @@ import { forgetAppFilters } from "./ask_filters.ts"; | ||
| 9 | 9 | import { isPacksMirror } from "../packs/owner.ts"; |
| 10 | 10 | import { contributionForksSince, isContributionFork } from "./contribute.ts"; |
| 11 | 11 | import type { Database } from "bun:sqlite"; |
| 12 | import { rmSync } from "node:fs"; | |
| 12 | import { mkdirSync, rmSync } from "node:fs"; | |
| 13 | import { dirname } from "node:path"; | |
| 13 | 14 | import { isSafeName } from "../git/guard.ts"; |
| 14 | 15 | import { installHooks } from "../git/hooks.ts"; |
| 15 | 16 | import { closeReader } from "../git/read.ts"; |
| @@ -359,7 +360,12 @@ export function withCounts(db: Database, repos: Repo[]): Repo[] { | ||
| 359 | 360 | })); |
| 360 | 361 | } |
| 361 | 362 | |
| 362 | export type CreateRepoError = "invalid_name" | "name_taken" | "git_init_failed"; | |
| 363 | /** | |
| 364 | * `dir_in_the_way`: no repository has the name, but a directory on disk holds | |
| 365 | * it (a create still under way, or one left behind), and createRepo never | |
| 366 | * takes over a directory it did not make. | |
| 367 | */ | |
| 368 | export type CreateRepoError = "invalid_name" | "name_taken" | "dir_in_the_way" | "git_init_failed"; | |
| 363 | 369 | |
| 364 | 370 | export interface CreateRepoInput { |
| 365 | 371 | ownerId: string; |
| @@ -397,8 +403,16 @@ export async function createRepo( | ||
| 397 | 403 | // Git first, row second: a row that exists is a repo you can clone. |
| 398 | 404 | const dir = repoDir(reposRoot, owner.username, input.name); |
| 399 | 405 | const branch = input.defaultBranch ?? "main"; |
| 406 | // The directory is claimed before git runs: a plain `mkdir` fails when | |
| 407 | // another create (or anything else) already made it, so a refused row | |
| 408 | // below only ever removes a directory this call made. With no row behind | |
| 409 | // it, that is not `name_taken`: no page would show what holds the name. | |
| 410 | if (!claimDir(dir)) return err("dir_in_the_way"); | |
| 400 | 411 | const init = await initBareRepo(dir, { defaultBranch: branch }); |
| 401 | if (!init.ok) return err("git_init_failed"); | |
| 412 | if (!init.ok) { | |
| 413 | unclaimDir(dir); | |
| 414 | return err("git_init_failed"); | |
| 415 | } | |
| 402 | 416 | await installHooks(dir); |
| 403 | 417 | |
| 404 | 418 | const repoId = id(); |
| @@ -583,6 +597,26 @@ function rowOrUndo(dir: string, insert: () => unknown): void { | ||
| 583 | 597 | } |
| 584 | 598 | } |
| 585 | 599 | |
| 600 | /** | |
| 601 | * Make `dir` and nothing else: its parents may exist, it may not. False when | |
| 602 | * it already does, so the caller never takes over a directory it did not make. | |
| 603 | */ | |
| 604 | function claimDir(dir: string): boolean { | |
| 605 | mkdirSync(dirname(dir), { recursive: true }); | |
| 606 | try { | |
| 607 | mkdirSync(dir); | |
| 608 | return true; | |
| 609 | } catch (error) { | |
| 610 | if ((error as NodeJS.ErrnoException).code === "EEXIST") return false; | |
| 611 | throw error; | |
| 612 | } | |
| 613 | } | |
| 614 | ||
| 615 | /** Give back a directory `claimDir` made, when git could not fill it. */ | |
| 616 | function unclaimDir(dir: string): void { | |
| 617 | rmSync(dir, { recursive: true, force: true }); | |
| 618 | } | |
| 619 | ||
| 586 | 620 | /** The private repository `ownerId` works `baseId`'s sealed reports in, if any. */ |
| 587 | 621 | export function getSealedFork( |
| 588 | 622 | db: Database, |
| @@ -673,6 +707,21 @@ export function listForks(db: Database, repoId: string): Repo[] { | ||
| 673 | 707 | .map(toRepo); |
outdated
Rewrap this comment: the line runs long and 'them.' sits alone on the next one. |
||
| 674 | 708 | } |
| 675 | 709 | |
| 710 | /** | |
| 711 | * Every repository whose object store borrows this one's through alternates: | |
| 712 | * its forks and the sealed repositories made for it, which are copied the | |
| 713 | * same way but name their base by `sealed_for`. Deleting or moving the store | |
| 714 | * must reach each of them. | |
| 715 | */ | |
| 716 | export function listBorrowers(db: Database, repoId: string): Repo[] { | |
| 717 | return db | |
| 718 | .query<RepoRow, [string]>( | |
| 719 | `${SELECT} WHERE repo.fork_parent_id = ?1 OR repo.sealed_for = ?1 ORDER BY repo.id`, | |
| 720 | ) | |
| 721 | .all(repoId) | |
| 722 | .map(toRepo); | |
| 723 | } | |
| 724 | ||
| 676 | 725 | /** |
| 677 | 726 | * The repository this one was forked from, while that parent is still here |
| 678 | 727 | * and the viewer may read it. A fork's lineage — the parent's name, and how |
| @@ -1091,9 +1140,12 @@ export function listReposByOwner( | ||
| 1091 | 1140 | * the repository stays listed and a retry finds it (`delete_failed`). A |
| 1092 | 1141 | * directory that is already missing leaves nothing to orphan, so the row still |
| 1093 | 1142 | * goes and `delete_failed` is still reported. |
| 1094 | * Before removing the objects, detach every fork that borrows them (repack so | |
| 1095 | * the fork owns its objects, then drop its alternates link) — otherwise a | |
| 1096 | * fork would be silently corrupted. This is the detach-before-delete rule. | |
| 1143 | * Before removing the objects, detach every fork or sealed repository that | |
| 1144 | * borrows them (repack so it owns its objects, then drop its alternates link) | |
| 1145 | * — otherwise it would be silently corrupted. This is the detach-before-delete | |
| 1146 | * rule. A sealed repository's row goes with its base's (`ON DELETE CASCADE`), | |
| 1147 | * so its directory is removed after the base's row, leaving no directory | |
| 1148 | * without a row to hold the name. | |
| 1097 | 1149 | */ |
| 1098 | 1150 | export async function deleteRepo( |
| 1099 | 1151 | db: Database, |
| @@ -1112,8 +1164,9 @@ export async function deleteRepo( | ||
| 1112 | 1164 | .get(repo.id) !== null |
| 1113 | 1165 | ) |
| 1114 | 1166 | return err("has_pulls"); |
| 1115 | const forks = listForks(db, repo.id); | |
| 1116 | for (const fork of forks) { | |
| 1167 | // Forks and sealed repositories alike borrow this store (`listBorrowers`). | |
| 1168 | const borrowers = listBorrowers(db, repo.id); | |
| 1169 | for (const fork of borrowers) { | |
| 1117 | 1170 | const forkDir = repoDir(reposRoot, fork.ownerName, fork.name); |
| 1118 | 1171 | const detached = await detachFork(forkDir); |
| 1119 | 1172 | if (!detached.ok) { |
| @@ -1149,8 +1202,17 @@ export async function deleteRepo( | ||
| 1149 | 1202 | forgetAppFilters(db, repo.id); |
| 1150 | 1203 | db.query("DELETE FROM repo WHERE id = ?1").run(repo.id); |
| 1151 | 1204 | forgetRepoCursors(db, repo.id); |
| 1205 | // The cascade took each sealed repository's row; its directory follows. | |
| 1206 | let sealedGone = true; | |
| 1207 | for (const sealed of borrowers) { | |
| 1208 | if (sealed.sealedFor !== repo.id) continue; | |
| 1209 | const sealedDir = repoDir(reposRoot, sealed.ownerName, sealed.name); | |
| 1210 | closeReader(sealedDir); | |
| 1211 | const swept = await deleteRepoDir(sealedDir); | |
| 1212 | if (!swept.ok && (await repoDirExists(sealedDir))) sealedGone = false; | |
| 1213 | } | |
| 1152 | 1214 | forgetListings(db); |
| 1153 | if (!removed.ok) return err("delete_failed"); | |
| 1215 | if (!removed.ok || !sealedGone) return err("delete_failed"); | |
| 1154 | 1216 | return ok(); |
| 1155 | 1217 | } |
| 1156 | 1218 |
src/core/transfer.ts +2 −2
| @@ -19,7 +19,7 @@ import { repoDir, repoDirExists } from "../git/repos.ts"; | ||
| 19 | 19 | import { id } from "../kernel/id.ts"; |
| 20 | 20 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 21 | 21 | import { forgetListings } from "./listings.ts"; |
| 22 | import { getRepo, getRepoById, listForks, type Repo } from "./repos.ts"; | |
| 22 | import { getRepo, getRepoById, listBorrowers, type Repo } from "./repos.ts"; | |
| 23 | 23 | import type { User } from "./users.ts"; |
| 24 | 24 | |
| 25 | 25 | export type TransferError = "name_taken" | "same_owner" | "same_name" | "invalid_name" | "move_failed"; |
| @@ -169,7 +169,7 @@ async function moveNow( | ||
| 169 | 169 | await rmdir(dirname(from)).catch(() => undefined); |
| 170 | 170 | const objectsFrom = join(from, "objects"); |
| 171 | 171 | const objectsTo = join(to, "objects"); |
| 172 | for (const fork of listForks(db, repo.id)) { | |
| 172 | for (const fork of listBorrowers(db, repo.id)) { | |
| 173 | 173 | await rewriteAlternates(repoDir(reposRoot, fork.ownerName, fork.name), objectsFrom, objectsTo).catch( |
| 174 | 174 | () => undefined, |
| 175 | 175 | ); |
src/kernel/destroy_reach.ts +4
| @@ -80,6 +80,10 @@ export const ALLOWED_REMOVALS: readonly { unit: string; why: string }[] = [ | ||
| 80 | 80 | "src/checks/install_evict.ts:evictInstallCache", |
| 81 | 81 | "src/apps/checkout.ts:exportTree", |
| 82 | 82 | ]), |
| 83 | { | |
| 84 | unit: "src/core/repos.ts:unclaimDir", | |
| 85 | why: "removes the directory `claimDir` has just made for the repository being created, when git could not init it", | |
| 86 | }, | |
| 83 | 87 | { unit: "src/core/repos.ts:rowOrUndo", why: "removes the directory git has just made for the repository being created, when its row is refused" }, |
| 84 | 88 | { unit: "src/git/locks.ts:sweepRefLock", why: "removes the one lock file of the ref a merge moves, when older than any live update (issue 12143): a name checked to be under refs/heads, never another file" }, |
| 85 | 89 | { unit: "src/git/write.ts:commitFiles", why: "removes its own temporary index file" }, |
src/origins/mirror.ts +3
| @@ -41,6 +41,7 @@ export type MirrorError = | ||
| 41 | 41 | | "name_taken" |
| 42 | 42 | | "already_mirrored" |
| 43 | 43 | | "git_init_failed" |
| 44 | | "dir_in_the_way" | |
| 44 | 45 | | "unreachable" |
| 45 | 46 | | "repository_limit" |
| 46 | 47 | | "storage_limit"; |
| @@ -315,6 +316,8 @@ export function mirrorMessage(error: MirrorError): string { | ||
| 315 | 316 | return "this repository is already mirrored here"; |
| 316 | 317 | case "git_init_failed": |
| 317 | 318 | return "the local repository could not be created"; |
| 319 | case "dir_in_the_way": | |
| 320 | return "a folder on disk already holds that name, with no repository behind it"; | |
| 318 | 321 | case "unreachable": |
| 319 | 322 | return "the origin could not be reached"; |
| 320 | 323 | case "repository_limit": |
src/tui/screens/common.ts +1
| @@ -1023,6 +1023,7 @@ const MESSAGES: Record<string, string> = { | ||
| 1023 | 1023 | protected: "that cannot be deleted", |
| 1024 | 1024 | delete_failed: "the delete could not be completed", |
| 1025 | 1025 | git_init_failed: "git could not create the repository", |
| 1026 | dir_in_the_way: "a folder on disk already holds that name, with no repository behind it: pick another", | |
| 1026 | 1027 | fork_failed: "the fork could not be created", |
| 1027 | 1028 | bad_anchor: "that line is not part of this diff", |
| 1028 | 1029 | invalid_url: "that URL is not valid (a git:// address is not supported: use its https:// address)", |
src/web/pages/site.ts +2 −1
| @@ -21,7 +21,7 @@ import { clientAddress } from "../../http/peer.ts"; | ||
| 21 | 21 | import { verifyEmail } from "../../actions/verify.ts"; |
| 22 | 22 | import { STRANGER_MAX } from "../../http/limits.ts"; |
| 23 | 23 | import { createRepo, repositoryLimitMessage, resolveOwner } from "../../actions/repos.ts"; |
| 24 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "../../actions/repo_words.ts"; | |
| 24 | import { REPO_DIR_IN_THE_WAY, REPO_INIT_FAILED, REPO_NAME_RULES } from "../../actions/repo_words.ts"; | |
| 25 | 25 | import { NOTE_MAX, requestOf } from "../../core/requests.ts"; |
| 26 | 26 | import { DECLINED_MESSAGE, REQUEST_IN_MESSAGE, STILL_WAITING_MESSAGE } from "../../actions/users.ts"; |
| 27 | 27 | import { HUMAN_CHECK_MESSAGE, INVITE_SPENT_MESSAGE, PASSKEY_REQUIRED_MESSAGE, SIGNUP_CLOSED_MESSAGE, signUp, signupClosed } from "../../actions/users.ts"; |
| @@ -948,6 +948,7 @@ export async function newRepoSubmit(ctx: Ctx): Promise<Response> { | ||
| 948 | 948 | name_taken: "That owner already has a repository by that name.", |
| 949 | 949 | invalid_name: REPO_NAME_RULES, |
| 950 | 950 | git_init_failed: REPO_INIT_FAILED, |
| 951 | dir_in_the_way: REPO_DIR_IN_THE_WAY, | |
| 951 | 952 | }; |
| 952 | 953 | return page( |
| 953 | 954 | { |
test/api-forge-view.test.ts +10
| @@ -4,9 +4,11 @@ | ||
| 4 | 4 | * board use, and costs one build per window however often it is polled. |
| 5 | 5 | */ |
| 6 | 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 7 | import { mkdirSync, renameSync } from "node:fs"; | |
| 7 | 8 | import { mkdtemp, rm } from "node:fs/promises"; |
| 8 | 9 | import { tmpdir } from "node:os"; |
| 9 | 10 | import { join } from "node:path"; |
| 11 | import { repoDir } from "../src/git/repos.ts"; | |
| 10 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 13 | import { DOOR_TTL_MS, foundryHref } from "../src/actions/foundry_door.ts"; |
| 12 | 14 | import { forgeView, VIEW_APPS } from "../src/actions/forge_view.ts"; |
| @@ -173,6 +175,12 @@ describe("the forge view over the API", () => { | ||
| 173 | 175 | const fake = await createRepo(app, adminUser, { name: "foundry", visibility: "public" }); |
| 174 | 176 | if (!fake.ok) throw new Error(String(fake.error)); |
| 175 | 177 | app.db.query("UPDATE repo SET owner_id = ?1 WHERE id = ?2").run(mallory.value.id, fake.value.id); |
| 178 | // The row moved by hand; its directory moves with it, as a transfer's does. | |
| 179 | mkdirSync(join(app.reposRoot, mallory.value.username), { recursive: true }); | |
| 180 | renameSync( | |
| 181 | repoDir(app.reposRoot, adminUser.username, "foundry"), | |
| 182 | repoDir(app.reposRoot, mallory.value.username, "foundry"), | |
| 183 | ); | |
| 176 | 184 | createAppRecord(app.db, { repoId: fake.value.id, slug: "foundry", access: "public" }); |
| 177 | 185 | lapse(); |
| 178 | 186 | expect(door()).toBeUndefined(); |
| @@ -246,6 +254,8 @@ describe("the forge view over the API", () => { | ||
| 246 | 254 | app.db.query("DELETE FROM app WHERE slug = 'foundry'").run(); |
| 247 | 255 | // A person who signed up as trevato, where no admin holds the name: no door. |
| 248 | 256 | app.db.query("UPDATE user SET username = 'trevato-org' WHERE id = ?1").run(org.id); |
| 257 | // The rename by hand moves the owner's directory too, as a real rename does. | |
| 258 | renameSync(join(app.reposRoot, "trevato"), join(app.reposRoot, "trevato-org")); | |
| 249 | 259 | const squatter = await createUser(app.db, { username: "trevato", email: "[email protected]", password: "a-long-passphrase-here", origin: "seed", trusted: true }); |
| 250 | 260 | if (!squatter.ok) throw new Error(squatter.error); |
| 251 | 261 | const theirs = await createRepoRow(app.db, app.reposRoot, { ownerId: squatter.value.id, name: "foundry", visibility: "public" }); |
test/repo-borrowers.test.ts added +136
| @@ -0,0 +1,136 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createOrg } from "../src/actions/orgs.ts"; | |
| 8 | import { createRepo, deleteRepo, forkRepo, getRepo, listBorrowers, type Repo } from "../src/core/repos.ts"; | |
| 9 | import { transferRepo } from "../src/core/transfer.ts"; | |
| 10 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 11 | import { repoDir } from "../src/git/repos.ts"; | |
| 12 | import { commitFiles } from "../src/git/write.ts"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | ||
| 15 | quietLogs(); | |
| 16 | ||
| 17 | /** | |
| 18 | * A sealed repository is copied from its base with `--shared`, as a fork is, | |
| 19 | * so it borrows the base's objects: deleting or moving the base reaches it | |
| 20 | * (#15033). And `createRepo` claims its directory before git runs, so it | |
| 21 | * never takes over, or later removes, one it did not make. | |
| 22 | */ | |
| 23 | describe("the borrowers of a repository's objects", () => { | |
| 24 | let dataDir = ""; | |
| 25 | let app: App; | |
| 26 | let alice: User; | |
| 27 | let bob: User; | |
| 28 | let n = 0; | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | dataDir = await mkdtemp(join(tmpdir(), "forge-borrowers-")); | |
| 32 | app = createApp({ dataDir, port: 0 }); | |
| 33 | const made = async (username: string, admin: boolean) => { | |
| 34 | const u = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username, | |
| 38 | email: `${username}@forge.local`, | |
| 39 | password: "a-long-borrowers-passphrase", | |
| 40 | adminIfFirst: admin, | |
| 41 | }); | |
| 42 | if (!u.ok) throw new Error(u.error); | |
| 43 | return u.value; | |
| 44 | }; | |
| 45 | alice = await made("alice", true); | |
| 46 | bob = await made("bob", false); | |
| 47 | const org = createOrg(app, alice, "lab"); | |
| 48 | if (!org.ok) throw new Error(org.error); | |
| 49 | }); | |
| 50 | ||
| 51 | afterAll(async () => { | |
| 52 | app.close(); | |
| 53 | await rm(dataDir, { recursive: true, force: true }); | |
| 54 | }); | |
| 55 | ||
| 56 | /** A base with one commit, a fork and a sealed repository of it, both bob's. */ | |
| 57 | const family = async () => { | |
| 58 | n += 1; | |
| 59 | const made = await createRepo(app.db, app.reposRoot, { ownerId: alice.id, name: `base${n}`, visibility: "public" }); | |
| 60 | if (!made.ok) throw new Error(made.error); | |
| 61 | const c = await commitFiles(repoDir(app.reposRoot, "alice", made.value.name), { | |
| 62 | branch: "main", | |
| 63 | message: "seed", | |
| 64 | files: [{ path: "a.txt", content: "a" }], | |
| 65 | author: { name: "T", email: "[email protected]" }, | |
| 66 | }); | |
| 67 | if (!c.ok) throw new Error("commit failed"); | |
| 68 | const fork = await forkRepo(app.db, app.reposRoot, made.value, bob.id); | |
| 69 | if (!fork.ok) throw new Error(fork.error); | |
| 70 | const sealed = await forkRepo(app.db, app.reposRoot, made.value, bob.id, undefined, { sealed: true }); | |
| 71 | if (!sealed.ok) throw new Error(sealed.error); | |
| 72 | return { base: made.value, fork: fork.value, sealed: sealed.value, oid: c.value.commitOid }; | |
| 73 | }; | |
| 74 | ||
| 75 | const has = (dir: string, oid: string) => | |
| 76 | Bun.spawnSync(["git", "cat-file", "-e", `${oid}^{commit}`], { cwd: dir }).exitCode === 0; | |
| 77 | ||
| 78 | test("listBorrowers names the forks and the sealed repositories", async () => { | |
| 79 | const { base, fork, sealed } = await family(); | |
| 80 | const ids = listBorrowers(app.db, base.id).map((r: Repo) => r.id); | |
| 81 | expect(ids.sort()).toEqual([fork.id, sealed.id].sort()); | |
| 82 | }); | |
| 83 | ||
| 84 | test("deleting the base takes its sealed repository's row and directory, and spares the fork", async () => { | |
| 85 | const { base, fork, sealed, oid } = await family(); | |
| 86 | const sealedDir = repoDir(app.reposRoot, "bob", sealed.name); | |
| 87 | const forkDir = repoDir(app.reposRoot, "bob", fork.name); | |
| 88 | expect(existsSync(join(sealedDir, "objects", "info", "alternates"))).toBe(true); | |
| 89 | const gone = await deleteRepo(app.db, app.reposRoot, base); | |
| 90 | expect(gone.ok).toBe(true); | |
| 91 | expect(getRepo(app.db, "alice", base.name)).toBeUndefined(); | |
| 92 | // The row cascades with its base; no directory is left to hold the name. | |
| 93 | expect(getRepo(app.db, "bob", sealed.name)).toBeUndefined(); | |
| 94 | expect(existsSync(sealedDir)).toBe(false); | |
| 95 | const again = await createRepo(app.db, app.reposRoot, { ownerId: bob.id, name: sealed.name }); | |
| 96 | expect(again.ok).toBe(true); | |
| 97 | // The fork keeps its own copy of the objects. | |
| 98 | expect(existsSync(join(forkDir, "objects", "info", "alternates"))).toBe(false); | |
| 99 | expect(has(forkDir, oid)).toBe(true); | |
| 100 | }); | |
| 101 | ||
| 102 | test("moving the base rewrites its sealed repository's alternates", async () => { | |
| 103 | const { base, sealed, oid } = await family(); | |
| 104 | const sealedDir = repoDir(app.reposRoot, "bob", sealed.name); | |
| 105 | const moved = await transferRepo(app.db, app.reposRoot, base, getUserByName(app.db, "lab")!, alice.id); | |
| 106 | expect(moved.ok).toBe(true); | |
| 107 | const alternates = readFileSync(join(sealedDir, "objects", "info", "alternates"), "utf8"); | |
| 108 | expect(alternates).toContain(join(repoDir(app.reposRoot, "lab", base.name), "objects")); | |
| 109 | expect(has(sealedDir, oid)).toBe(true); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a leftover directory with no row is refused in its own words, and left whole", async () => { | |
| 113 | const dir = repoDir(app.reposRoot, "alice", "claimed"); | |
| 114 | mkdirSync(dir, { recursive: true }); | |
| 115 | writeFileSync(join(dir, "someone-elses"), "kept"); | |
| 116 | const made = await createRepo(app.db, app.reposRoot, { ownerId: alice.id, name: "claimed" }); | |
| 117 | expect(made.ok).toBe(false); | |
| 118 | // Not `name_taken`: no repository holds the name, so none could be shown. | |
| 119 | if (!made.ok) expect(made.error).toBe("dir_in_the_way"); | |
| 120 | expect(readFileSync(join(dir, "someone-elses"), "utf8")).toBe("kept"); | |
| 121 | expect(existsSync(join(dir, "HEAD"))).toBe(false); | |
| 122 | }); | |
| 123 | ||
| 124 | test("a second create while the first is still in git is refused, and the first's directory stays", async () => { | |
| 125 | // The first runs up to its first await (git init) before the second | |
| 126 | // starts, so the second meets the first's directory with no row yet. | |
| 127 | const first = createRepo(app.db, app.reposRoot, { ownerId: alice.id, name: "twin" }); | |
| 128 | const second = await createRepo(app.db, app.reposRoot, { ownerId: alice.id, name: "twin" }); | |
| 129 | expect(second.ok).toBe(false); | |
| 130 | if (!second.ok) expect(second.error).toBe("dir_in_the_way"); | |
| 131 | const won = await first; | |
| 132 | expect(won.ok).toBe(true); | |
| 133 | expect(getRepo(app.db, "alice", "twin")).toBeDefined(); | |
| 134 | expect(existsSync(join(repoDir(app.reposRoot, "alice", "twin"), "HEAD"))).toBe(true); | |
| 135 | }); | |
| 136 | }); |
new above
offline
An unowned leftover directory now answers name_taken forever with no repository to show. Distinguish 'a row owns this' from 'orphan directory' and handle the orphan (sweep it, or give a message a person can act on), with a test.