Public: anyone can read this; the crew can change it.
A branch's sentences live in one module, read by the branches page, MCP, the mirror and the terminal #5707
mason/forge:mason/5651-a-branch-s-sentences-live-in-one-module → main 2 commits fork
8 files
changed +85
−10
src/actions/branch_words.ts added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | /** | |
| 2 | * What making or deleting a branch says, said once (#5651). The branches | |
| 3 | * page, MCP, the mirror and the terminal read these, so every door words a | |
| 4 | * branch the same way; a copy typed back elsewhere fails | |
| 5 | * test/branch-words.test.ts. | |
| 6 | */ | |
| 7 | ||
| 8 | /** Creating a branch failed for a reason without its own sentence. */ | |
| 9 | export const BRANCH_NOT_CREATED = "That branch was not created."; | |
| 10 | ||
| 11 | /** Deleting a branch failed for a reason without its own sentence. */ | |
| 12 | export const BRANCH_NOT_DELETED = "That branch was not deleted."; | |
| 13 | ||
| 14 | /** What git's empty lease means when a mirror creates a ref upstream. */ | |
| 15 | export const MUST_NOT_ALREADY_EXIST = "must not already exist"; | |
| 16 | ||
| 17 | /** The terminal's answer to an empty branch name. */ | |
| 18 | export const GIVE_THE_BRANCH_A_NAME = "give the branch a name"; |
src/actions/branches.ts +3 −2
| @@ -35,6 +35,7 @@ import { redact, secretsIn } from "../origins/credentials.ts"; | ||
| 35 | 35 | import { pushToOrigin, type PushUpdate } from "../origins/git-mirror.ts"; |
| 36 | 36 | import type { WriteThroughError } from "../origins/write-through.ts"; |
| 37 | 37 | import { requireWriter, type AccessError } from "./access.ts"; |
| 38 | import { MUST_NOT_ALREADY_EXIST } from "./branch_words.ts"; | |
| 38 | 39 | |
| 39 | 40 | const ZERO = "0".repeat(40); |
| 40 | 41 | |
| @@ -57,8 +58,8 @@ export interface CreateBranchInput { | ||
| 57 | 58 | |
| 58 | 59 | /** |
| 59 | 60 | * Create a branch. On a mirror the ref is created upstream first, with an |
| 60 | * empty lease — git's own "must not already exist" — so two forges racing to | |
| 61 | * create the same branch cannot both believe they won. | |
| 61 | * empty lease — git's own {@link MUST_NOT_ALREADY_EXIST} — so two forges | |
| 62 | * racing to create the same branch cannot both believe they won. | |
| 62 | 63 | */ |
| 63 | 64 | export async function createBranch( |
| 64 | 65 | app: App, |
src/http/mcp.ts +3 −2
| @@ -160,6 +160,7 @@ import { runGit } from "../git/env.ts"; | ||
| 160 | 160 | import { isSafeRef, isSafeTreePath } from "../git/guard.ts"; |
| 161 | 161 | import { blobForRender, listTree } from "../git/read.ts"; |
| 162 | 162 | import { CREATE_ERRORS, DELETE_ERRORS } from "../web/pages/branches.ts"; |
| 163 | import { BRANCH_NOT_CREATED, BRANCH_NOT_DELETED } from "../actions/branch_words.ts"; | |
| 163 | 164 | import { askToLiveFor } from "../web/ask_to_live.ts"; |
| 164 | 165 | import { getAppByRepo } from "../core/apps.ts"; |
| 165 | 166 | import { |
| @@ -2249,12 +2250,12 @@ const TOOLS: readonly Tool[] = [ | ||
| 2249 | 2250 | ...(from === undefined || from === "" ? {} : { from }), |
| 2250 | 2251 | }); |
| 2251 | 2252 | if (!made.ok) |
| 2252 | return bad(made.error, CREATE_ERRORS[made.error] ?? "That branch was not created."); | |
| 2253 | return bad(made.error, CREATE_ERRORS[made.error] ?? BRANCH_NOT_CREATED); | |
| 2253 | 2254 | return good({ says: `Created ${made.value.name} at ${made.value.oid.slice(0, 7)}.`, url: base }); |
| 2254 | 2255 | } |
| 2255 | 2256 | const gone = await deleteBranch(ctx.app, user, repo, name); |
| 2256 | 2257 | if (!gone.ok) |
| 2257 | return bad(gone.error, DELETE_ERRORS[gone.error] ?? "That branch was not deleted."); | |
| 2258 | return bad(gone.error, DELETE_ERRORS[gone.error] ?? BRANCH_NOT_DELETED); | |
| 2258 | 2259 | return good({ says: `Deleted ${gone.value.name}.`, url: base }); |
| 2259 | 2260 | }, |
| 2260 | 2261 | }, |
src/origins/git-mirror.ts +3 −2
| @@ -28,6 +28,7 @@ import { trustedHosts } from "./trusted-hosts.ts"; | ||
| 28 | 28 | import { vetHost, type OutboundOptions } from "../kernel/outbound.ts"; |
| 29 | 29 | import { pinFor, remoteGitEnv } from "../git/remote-env.ts"; |
| 30 | 30 | import { redact, safeRemoteUrl, secretsIn } from "./credentials.ts"; |
| 31 | import { MUST_NOT_ALREADY_EXIST } from "../actions/branch_words.ts"; | |
| 31 | 32 | |
| 32 | 33 | /** A mirror fetch can be genuinely slow; Forgejo allows its own 300s. */ |
| 33 | 34 | const FETCH_TIMEOUT_MS = 300_000; |
| @@ -286,7 +287,7 @@ export interface PushUpdate { | ||
| 286 | 287 | * The lease is the three-part `--force-with-lease=<ref>:<expect>` form — the |
| 287 | 288 | * only one git considers non-experimental, and the only one immune to our |
| 288 | 289 | * own background fetcher. An empty `<expect>` (a ref being created) means |
| 289 | * "must not already exist", which is exactly the check we want. | |
| 290 | * {@link MUST_NOT_ALREADY_EXIST}, which is exactly the check we want. | |
| 290 | 291 | */ |
| 291 | 292 | export async function pushToOrigin( |
| 292 | 293 | dir: string, |
| @@ -317,7 +318,7 @@ export async function pushToOrigin( | ||
| 317 | 318 | if (!ZERO_OID.test(update.oldOid) && !isSafeOid(update.oldOid)) |
| 318 | 319 | return err({ message: `refusing an unsafe object id` }); |
| 319 | 320 | |
| 320 | // Creating a ref leases the empty string: "must not already exist". | |
| 321 | // Creating a ref leases the empty string: MUST_NOT_ALREADY_EXIST. | |
| 321 | 322 | const expect = ZERO_OID.test(update.oldOid) ? "" : update.oldOid; |
| 322 | 323 | args.push(`--force-with-lease=${update.ref}:${expect}`); |
| 323 | 324 | refspecs.push( |
src/tui/screens/refs.ts +2 −1
| @@ -31,6 +31,7 @@ import { | ||
| 31 | 31 | type PickItem, |
| 32 | 32 | } from "./common.ts"; |
| 33 | 33 | import { localRepo, sourceName, whereRows, whereStrip } from "./where.ts"; |
| 34 | import { GIVE_THE_BRANCH_A_NAME } from "../../actions/branch_words.ts"; | |
| 34 | 35 | |
| 35 | 36 | export interface RefList { |
| 36 | 37 | readonly branches: string[]; |
| @@ -209,7 +210,7 @@ export function branchesScreen( | ||
| 209 | 210 | ? "created here" |
| 210 | 211 | : `created on ${mirrored.fullName} first, then here`, |
| 211 | 212 | onSubmit: (name) => { |
| 212 | if (name === "") return "give the branch a name"; | |
| 213 | if (name === "") return GIVE_THE_BRANCH_A_NAME; | |
| 213 | 214 | return (async () => { |
| 214 | 215 | const made = await createBranch(f.app, f.user, repo, { |
| 215 | 216 | name, |
src/tui/screens/workspace.ts +2 −1
| @@ -142,6 +142,7 @@ import { | ||
| 142 | 142 | whereSpans, |
| 143 | 143 | whereStrip, |
| 144 | 144 | } from "./where.ts"; |
| 145 | import { GIVE_THE_BRANCH_A_NAME } from "../../actions/branch_words.ts"; | |
| 145 | 146 | |
| 146 | 147 | /** |
| 147 | 148 | * The mark a rendered markdown tab wears — `README.md ¶`. From the safe set |
| @@ -997,7 +998,7 @@ export function workspaceScreen( | ||
| 997 | 998 | ? "created here, and the workspace switches to it" |
| 998 | 999 | : `created on ${repo.origin.fullName} first, then here`, |
| 999 | 1000 | onSubmit: (name) => { |
| 1000 | if (name === "") return "give the branch a name"; | |
| 1001 | if (name === "") return GIVE_THE_BRANCH_A_NAME; | |
| 1001 | 1002 | return (async () => { |
| 1002 | 1003 | const made = await createBranch(f.app, f.user, repo, { |
| 1003 | 1004 | name, |
src/web/pages/branches.ts +3 −2
| @@ -43,6 +43,7 @@ import { | ||
| 43 | 43 | } from "../ui.ts"; |
| 44 | 44 | import { frozenBlock } from "./frozen.ts"; |
| 45 | 45 | import { refSetFrom } from "./refs.ts"; |
| 46 | import { BRANCH_NOT_CREATED, BRANCH_NOT_DELETED } from "../../actions/branch_words.ts"; | |
| 46 | 47 | |
| 47 | 48 | /** |
| 48 | 49 | * How many rows pay for a `git log -1`. `listBranches` already spends one |
| @@ -337,7 +338,7 @@ export async function branchCreate(ctx: Ctx): Promise<Response> { | ||
| 337 | 338 | if (!created.ok) { |
| 338 | 339 | return flashTo(ctx, base, { |
| 339 | 340 | tone: "danger", |
| 340 | message: CREATE_ERRORS[created.error] ?? "That branch was not created.", | |
| 341 | message: CREATE_ERRORS[created.error] ?? BRANCH_NOT_CREATED, | |
| 341 | 342 | }); |
| 342 | 343 | } |
| 343 | 344 | return flashTo(ctx, base, { |
| @@ -368,7 +369,7 @@ export async function branchDelete(ctx: Ctx): Promise<Response> { | ||
| 368 | 369 | if (!deleted.ok) { |
| 369 | 370 | return flashTo(ctx, base, { |
| 370 | 371 | tone: "danger", |
| 371 | message: DELETE_ERRORS[deleted.error] ?? "That branch was not deleted.", | |
| 372 | message: DELETE_ERRORS[deleted.error] ?? BRANCH_NOT_DELETED, | |
| 372 | 373 | }); |
| 373 | 374 | } |
| 374 | 375 | return flashTo(ctx, base, { message: `Deleted ${deleted.value.name}.` }); |
test/branch-words.test.ts added +51
| @@ -0,0 +1,51 @@ | ||
| 1 | /** | |
| 2 | * A branch's sentences live in one module (#5651): the branches page, MCP, | |
| 3 | * the mirror and the terminal import them from src/actions/branch_words.ts, | |
| 4 | * and none of them types a copy back. | |
| 5 | */ | |
| 6 | ||
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { readFileSync } from "node:fs"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { | |
| 11 | BRANCH_NOT_CREATED, | |
| 12 | BRANCH_NOT_DELETED, | |
| 13 | GIVE_THE_BRANCH_A_NAME, | |
| 14 | MUST_NOT_ALREADY_EXIST, | |
| 15 | } from "../src/actions/branch_words.ts"; | |
| 16 | ||
| 17 | const SENTENCES = [ | |
| 18 | BRANCH_NOT_CREATED, | |
| 19 | BRANCH_NOT_DELETED, | |
| 20 | MUST_NOT_ALREADY_EXIST, | |
| 21 | GIVE_THE_BRANCH_A_NAME, | |
| 22 | ]; | |
| 23 | ||
| 24 | const READERS = [ | |
| 25 | "src/web/pages/branches.ts", | |
| 26 | "src/http/mcp.ts", | |
| 27 | "src/actions/branches.ts", | |
| 28 | "src/origins/git-mirror.ts", | |
| 29 | "src/tui/screens/refs.ts", | |
| 30 | "src/tui/screens/workspace.ts", | |
| 31 | ]; | |
| 32 | ||
| 33 | const source = (path: string): string => | |
| 34 | readFileSync(join(import.meta.dir, "..", path), "utf8"); | |
| 35 | ||
| 36 | test("the module says each sentence as it always read", () => { | |
| 37 | expect(SENTENCES).toEqual([ | |
| 38 | "That branch was not created.", | |
| 39 | "That branch was not deleted.", | |
| 40 | "must not already exist", | |
| 41 | "give the branch a name", | |
| 42 | ]); | |
| 43 | }); | |
| 44 | ||
| 45 | for (const path of READERS) { | |
| 46 | test(`${path} imports the branch's words and types none of them`, () => { | |
| 47 | const text = source(path); | |
| 48 | expect(text).toMatch(/from "[./]+(actions\/)?branch_words\.ts"/); | |
| 49 | for (const sentence of SENTENCES) expect(text).not.toContain(sentence); | |
| 50 | }); | |
| 51 | } |
new above
offline