- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/versions.ts?format=json
180 linesFirst import 89% · pinch 8% · rename 3%Last change pinch in version 280, 3 versions changed it
1// abso drop, pluck, amend, label, labels and follow: changing which published
2// versions shared holds, naming versions, and following a named one (#10, #27).
4import type { VersionRef } from "./api.ts";
5import { bool, type Parsed, str, UsageError } from "./args.ts";
6import {
7 applyAnswer,
8 collisionText,
9 type Env,
10 EXIT,
11 type Output,
12 open,
13 plural,
14 pullAnswer,
15 pullWritten,
16 recordFolder,
17} from "./commands.ts";
18import { writeConfig } from "./config.ts";
19import { refusedText } from "./safety.ts";
21function versionList(words: readonly string[], what: string): number[] {
22 if (words.length === 0) throw new UsageError(`name the ${what} by number`);
23 return words.map((w) => {
24 if (!/^\d+$/.test(w)) throw new UsageError(`${what} are version numbers (got ${JSON.stringify(w)})`);
25 return Number(w);
26 });
27}
29function ref(v: VersionRef): string {
30 return ` version ${v.version} by ${v.agent}: ${v.message}`;
31}
33function labelName(raw: string | undefined): string {
34 if (!raw || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(raw))
35 throw new UsageError(
36 `label names are letters, digits, dot, dash and underscore (got ${JSON.stringify(raw ?? "")})`,
37 );
38 return raw;
39}
41export async function runDrop(args: Parsed, env: Env): Promise<Output> {
42 const versions = versionList(args.positionals, "versions to drop");
43 const message = str(args, "message");
44 if (!message) throw new UsageError('say why with -m "<message>"');
45 const { api } = open(env);
46 const answer = await api.drop(versions, message, bool(args, "with-dependents"));
47 if (answer.ok) {
48 return {
49 text: [
50 `Dropped from shared, as version ${answer.version}:`,
51 ...answer.dropped.map(ref),
52 "Other agents hear it as news; their next pull takes it out of their folders. History keeps it: abso log shows the drop.",
53 ].join("\n"),
54 json: { ...answer },
55 };
56 }
57 const lines = ["Not dropped."];
58 if (answer.dependents && answer.dependents.length > 0) {
59 lines.push(
60 `Later pushed versions build on ${versions.length === 1 ? "it" : "them"}:`,
61 ...answer.dependents.map(ref),
62 "Pass --with-dependents to drop those too.",
63 );
64 }
65 if (answer.blocked && answer.blocked.length > 0) {
66 lines.push(
67 "Unpushed work builds on it, and a drop never touches unpushed work:",
68 ...answer.blocked.map(
69 (b) => ` ${b.agent !== undefined ? `agent ${b.agent}` : `draft ${b.draft}`}: ${b.files.join(", ")}`,
70 ),
71 );
72 }
73 return { exit: EXIT.refused, text: lines.join("\n"), json: { ...answer } };
74}
76export async function runPluck(args: Parsed, env: Env): Promise<Output> {
77 const [draft, ...rest] = args.positionals;
78 if (draft === undefined)
79 throw new UsageError('pluck takes a draft and its versions: abso pluck <draft> <version...> -m "why"');
80 const versions = versionList(rest, `versions of draft ${draft} to pluck`);
81 const message = str(args, "message");
82 if (!message) throw new UsageError('say why with -m "<message>"');
83 const { api } = open(env);
84 const answer = await api.pluck(draft, versions, message, bool(args, "with-dependencies"));
85 if (answer.ok) {
86 return {
87 text: `Plucked ${plural(versions.length, "version")} of draft ${draft} into shared as version ${answer.version} (${plural(answer.changes, "change")}). The draft stays as it is; landing it later brings in only the rest.`,
88 json: { ...answer },
89 };
90 }
91 const lines = ["Not plucked."];
92 if (answer.needs && answer.needs.length > 0)
93 lines.push(
94 "They build on these versions of the draft:",
95 ...answer.needs.map(ref),
96 "Pass --with-dependencies to take those too.",
97 );
98 if (answer.collisions && answer.collisions.length > 0)
99 lines.push(
100 `${plural(answer.collisions.length, "collision")} with work in shared:`,
101 ...answer.collisions.map(collisionText),
102 );
103 return { exit: EXIT.refused, text: lines.join("\n"), json: { ...answer } };
104}
106export async function runAmend(args: Parsed, env: Env): Promise<Output> {
107 const message = str(args, "message");
108 if (!message) throw new UsageError('give the new message with -m "<message>"');
109 const { api } = open(env);
110 const a = await api.amend(message);
111 const where = a.draft === null ? "shared" : `draft ${a.draft}`;
112 return {
113 text: `Version ${a.version} of ${where} now says: ${a.message}\n(It said: ${a.was}.) Only the message changed; to change what it did, push again or abso drop it.`,
114 json: { ...a },
115 };
116}
118export async function runLabel(args: Parsed, env: Env): Promise<Output> {
119 if (args.positionals.length !== 1)
120 throw new UsageError("label takes one name: abso label <name> [--version <n>] [--remove]");
121 const name = labelName(args.positionals[0]);
122 const raw = str(args, "version");
123 if (raw !== undefined && !/^\d+$/.test(raw))
124 throw new UsageError(`--version is a version number (got ${JSON.stringify(raw)})`);
125 const remove = bool(args, "remove");
126 const { api } = open(env);
127 const a = await api.label(name, raw === undefined ? undefined : Number(raw), remove);
128 const now =
129 a.versions.length === 0 ? "no version now" : `${plural(a.versions.length, "version")}: ${a.versions.join(", ")}`;
130 return {
131 text: `${remove ? "Took" : "Put"} label ${a.name} ${remove ? "off" : "on"} version ${a.version}. It marks ${now}.`,
132 json: { ...a },
133 };
134}
136export async function runLabels(_args: Parsed, env: Env): Promise<Output> {
137 const { config, api } = open(env);
138 const { labels } = await api.labels();
139 const names = Object.keys(labels).sort();
140 const lines =
141 names.length === 0
142 ? ["No labels yet. abso label <name> puts one on shared's newest version."]
143 : names.map((n) => ` ${n}: ${labels[n]!.join(", ")}${config.follow === n ? " (this folder follows it)" : ""}`);
144 return { text: lines.join("\n"), json: { labels, follow: config.follow ?? null } };
145}
147export async function runFollow(args: Parsed, env: Env): Promise<Output> {
148 const none = bool(args, "none");
149 if (none === (args.positionals.length === 1) || args.positionals.length > 1)
150 throw new UsageError("follow takes one label, or --none: abso follow <label> | abso follow --none");
151 const follow = none ? undefined : labelName(args.positionals[0]);
152 const { root, config } = open(env);
153 const { token: _token, follow: _old, ...settings } = config;
154 writeConfig(root, { ...settings, ...(follow ? { follow } : {}) });
155 // Sync now (recording first, as sync does), so the folder is where it now says it is.
156 const { api } = open(env);
157 await recordFolder(root, api);
158 const s = await applyAnswer(root, api, await pullAnswer(root, api));
159 pullWritten(root);
160 const changes = s.written.length + s.deleted.length;
161 const head = follow
162 ? `This folder now follows label ${follow}: every pull takes it to the newest version with that label. It is on version ${s.version}.`
163 : `This folder follows no label now: pulls take it to shared's newest version, ${s.version}.`;
164 return {
165 text: [
166 head,
167 changes === 0 ? "No files changed." : `${plural(s.written.length, "file")} updated, ${s.deleted.length} deleted.`,
168 ...refusedText(s.refused),
169 ...(s.kept.length > 0 ? [`Left alone because they have unrecorded changes: ${s.kept.join(", ")}`] : []),
170 ].join("\n"),
171 json: {
172 follow: follow ?? null,
173 version: s.version,
174 written: s.written,
175 deleted: s.deleted,
176 kept: s.kept,
177 refused: s.refused,
178 },
179 };
180}