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

Parked opened this
Closed without merging

mason/forge:mason/5651-a-branch-s-sentences-live-in-one-module → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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";
3535 import { pushToOrigin, type PushUpdate } from "../origins/git-mirror.ts";
3636 import type { WriteThroughError } from "../origins/write-through.ts";
3737 import { requireWriter, type AccessError } from "./access.ts";
38 import { MUST_NOT_ALREADY_EXIST } from "./branch_words.ts";
3839
3940 const ZERO = "0".repeat(40);
4041
@@ -57,8 +58,8 @@ export interface CreateBranchInput {
5758
5859 /**
5960 * 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.
6263 */
6364 export async function createBranch(
6465 app: App,
src/http/mcp.ts +3 −2
@@ -160,6 +160,7 @@ import { runGit } from "../git/env.ts";
160160 import { isSafeRef, isSafeTreePath } from "../git/guard.ts";
161161 import { blobForRender, listTree } from "../git/read.ts";
162162 import { CREATE_ERRORS, DELETE_ERRORS } from "../web/pages/branches.ts";
163 import { BRANCH_NOT_CREATED, BRANCH_NOT_DELETED } from "../actions/branch_words.ts";
163164 import { askToLiveFor } from "../web/ask_to_live.ts";
164165 import { getAppByRepo } from "../core/apps.ts";
165166 import {
@@ -2249,12 +2250,12 @@ const TOOLS: readonly Tool[] = [
22492250 ...(from === undefined || from === "" ? {} : { from }),
22502251 });
22512252 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);
22532254 return good({ says: `Created ${made.value.name} at ${made.value.oid.slice(0, 7)}.`, url: base });
22542255 }
22552256 const gone = await deleteBranch(ctx.app, user, repo, name);
22562257 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);
22582259 return good({ says: `Deleted ${gone.value.name}.`, url: base });
22592260 },
22602261 },
src/origins/git-mirror.ts +3 −2
@@ -28,6 +28,7 @@ import { trustedHosts } from "./trusted-hosts.ts";
2828 import { vetHost, type OutboundOptions } from "../kernel/outbound.ts";
2929 import { pinFor, remoteGitEnv } from "../git/remote-env.ts";
3030 import { redact, safeRemoteUrl, secretsIn } from "./credentials.ts";
31 import { MUST_NOT_ALREADY_EXIST } from "../actions/branch_words.ts";
3132
3233 /** A mirror fetch can be genuinely slow; Forgejo allows its own 300s. */
3334 const FETCH_TIMEOUT_MS = 300_000;
@@ -286,7 +287,7 @@ export interface PushUpdate {
286287 * The lease is the three-part `--force-with-lease=<ref>:<expect>` form — the
287288 * only one git considers non-experimental, and the only one immune to our
288289 * 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.
290291 */
291292 export async function pushToOrigin(
292293 dir: string,
@@ -317,7 +318,7 @@ export async function pushToOrigin(
317318 if (!ZERO_OID.test(update.oldOid) && !isSafeOid(update.oldOid))
318319 return err({ message: `refusing an unsafe object id` });
319320
320 // Creating a ref leases the empty string: "must not already exist".
321 // Creating a ref leases the empty string: MUST_NOT_ALREADY_EXIST.
321322 const expect = ZERO_OID.test(update.oldOid) ? "" : update.oldOid;
322323 args.push(`--force-with-lease=${update.ref}:${expect}`);
323324 refspecs.push(
src/tui/screens/refs.ts +2 −1
@@ -31,6 +31,7 @@ import {
3131 type PickItem,
3232 } from "./common.ts";
3333 import { localRepo, sourceName, whereRows, whereStrip } from "./where.ts";
34 import { GIVE_THE_BRANCH_A_NAME } from "../../actions/branch_words.ts";
3435
3536 export interface RefList {
3637 readonly branches: string[];
@@ -209,7 +210,7 @@ export function branchesScreen(
209210 ? "created here"
210211 : `created on ${mirrored.fullName} first, then here`,
211212 onSubmit: (name) => {
212 if (name === "") return "give the branch a name";
213 if (name === "") return GIVE_THE_BRANCH_A_NAME;
213214 return (async () => {
214215 const made = await createBranch(f.app, f.user, repo, {
215216 name,
src/tui/screens/workspace.ts +2 −1
@@ -142,6 +142,7 @@ import {
142142 whereSpans,
143143 whereStrip,
144144 } from "./where.ts";
145 import { GIVE_THE_BRANCH_A_NAME } from "../../actions/branch_words.ts";
145146
146147 /**
147148 * The mark a rendered markdown tab wears — `README.md ¶`. From the safe set
@@ -997,7 +998,7 @@ export function workspaceScreen(
997998 ? "created here, and the workspace switches to it"
998999 : `created on ${repo.origin.fullName} first, then here`,
9991000 onSubmit: (name) => {
1000 if (name === "") return "give the branch a name";
1001 if (name === "") return GIVE_THE_BRANCH_A_NAME;
10011002 return (async () => {
10021003 const made = await createBranch(f.app, f.user, repo, {
10031004 name,
src/web/pages/branches.ts +3 −2
@@ -43,6 +43,7 @@ import {
4343 } from "../ui.ts";
4444 import { frozenBlock } from "./frozen.ts";
4545 import { refSetFrom } from "./refs.ts";
46 import { BRANCH_NOT_CREATED, BRANCH_NOT_DELETED } from "../../actions/branch_words.ts";
4647
4748 /**
4849 * 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> {
337338 if (!created.ok) {
338339 return flashTo(ctx, base, {
339340 tone: "danger",
340 message: CREATE_ERRORS[created.error] ?? "That branch was not created.",
341 message: CREATE_ERRORS[created.error] ?? BRANCH_NOT_CREATED,
341342 });
342343 }
343344 return flashTo(ctx, base, {
@@ -368,7 +369,7 @@ export async function branchDelete(ctx: Ctx): Promise<Response> {
368369 if (!deleted.ok) {
369370 return flashTo(ctx, base, {
370371 tone: "danger",
371 message: DELETE_ERRORS[deleted.error] ?? "That branch was not deleted.",
372 message: DELETE_ERRORS[deleted.error] ?? BRANCH_NOT_DELETED,
372373 });
373374 }
374375 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 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs