Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/src/collision-text.ts?format=json
124 linesFirst import 93% · rename 6% · infra-split 1%Last change rename in version 100, 3 versions changed it
1// How a collision, and the lines a sync merged, read to an agent.
2
3import type { Collision, MergedLine } from "./api.ts";
4import { plural } from "./command-env.ts";
5
6/** `[first, last]`, 1-based; `[n, n-1]` means that side left no lines at line n. */
7function describeLines(lines: unknown): string {
8 if (Array.isArray(lines) && lines.length === 2 && lines.every((n) => typeof n === "number")) {
9 const [first, last] = lines as [number, number];
10 if (last < first) return `no lines, at line ${first}`;
11 return first === last ? `line ${first}` : `lines ${first}-${last}`;
12 }
13 return `lines ${JSON.stringify(lines)}`;
16/** Lines a sync merged item by item: not collisions and no publish waits on them, but the agent should read them (#37). */
17export function mergedLinesText(merged: readonly MergedLine[]): string[] {
18 if (merged.length === 0) return [];
19 const one = (m: MergedLine) =>
20 `${m.file} line ${m.line}, with ${m.theirs.agent}'s version ${m.theirs.version} ("${m.theirs.message}"): ${m.text}`;
21 const who = new Set(merged.map((m) => m.theirs.agent)).size === 1 ? merged[0]!.theirs.agent : "others";
22 return [
23 `Merged ${merged.length === 1 ? "1 line" : `${merged.length} lines`} that both you and ${who} changed, item by item. Check ${merged.length === 1 ? "it reads" : "they read"} right, then push:`,
24 ...merged.map((m) => ` ${one(m)}`),
25 ];
28/** The line above a sync's or switch's collisions: what the folder now holds, said only for the kinds that apply. */
29export function collisionsHeading(collisions: readonly Collision[]): string {
30 // Lines changed on both sides: publish refuses them untouched (#37), so say to edit them, or --as-is to keep both.
31 const marked = collisions.some((c) => c.marked);
32 const lines = !marked && collisions.some((c) => c.kind === undefined || c.kind === "lines");
33 const twice = collisions.some((c) => c.kind === "duplicate");
34 return [
35 `${plural(collisions.length, "collision")}:`,
36 ...(marked
37 ? [
38 "where lines changed on both sides, the file now holds a marker block: yours, the lines you both started from, and theirs. Settle each before you push: keep yours, theirs or a mix and delete the marker lines, or run abso settle <file> --yours|--theirs|--both. Box runs fail while markers are in the folder.",
39 ]
40 : []),
41 ...(lines
42 ? [
43 "where lines changed on both sides, the files now hold both, with no markers. Edit them to what they should be before you push; if both really belong, abso push --as-is keeps both.",
44 ]
45 : []),
46 ...(twice ? ["a name defined twice needs one definition kept before you push."] : []),
47 ...(!marked && !lines && !twice ? ["read each one, edit what needs it, then push."] : []),
48 ].join(" ");
51/** Others' versions a publish went on top of: said plainly, since "version 4" alone read as overwriting version 3 (#37). */
52export function mergedText(
53 merged: readonly { readonly version: number; readonly agent: string; readonly message: string }[],
54): string {
55 const which = merged.map((m) => `version ${m.version} (${m.agent}: "${m.message}")`);
56 const list = which.length === 1 ? which[0]! : `${which.slice(0, -1).join(", ")} and ${which[which.length - 1]}`;
57 return `It went on top of ${list}, which your folder doesn't have yet. Nothing of theirs was overwritten; run abso pull to see both.`;
60export function collisionText(c: Collision): string {
61 // Theirs may still be waiting in the merge queue (#39): not a version yet.
62 const v =
63 c.theirs.queued !== undefined ? `queued push ${c.theirs.queued} (not landed yet)` : `version ${c.theirs.version}`;
64 const indent = (text: string): string =>
65 text
66 .replace(/\n$/, "")
67 .split("\n")
68 .map((l) => ` ${l}`)
69 .join("\n");
70 if (c.marked) {
71 // The block's range in the file (yours.lines, as the engine sends it); a stray marker line has no other author.
72 const range = (c.lines ?? c.yours.lines) as readonly [number, number] | undefined;
73 const at = Array.isArray(range)
74 ? range[0] === range[1]
75 ? `line ${range[0]}`
76 : `lines ${range[0]}-${range[1]}`
77 : "a marker block";
78 if (c.theirs.agent === "") return ` ${c.file} ${at}: a marker line left from a collision; delete it.`;
79 return ` ${c.file} ${at}: yours against ${c.theirs.agent}'s ${v} ("${c.theirs.message}").`;
80 }
81 if (c.kind === "attribute") {
82 // The value as text: "1" for executable, a link's target; "" for neither.
83 const value = (text: string): string =>
84 c.name === "exec"
85 ? text === ""
86 ? "not executable"
87 : "executable"
88 : text === ""
89 ? "a plain file, not a link"
90 : `a link to ${text}`;
91 return [
92 ` ${c.file}: you and ${c.theirs.agent} both changed ${c.name === "exec" ? "whether it is executable" : "where it links"}.`,
93 ` yours: ${value(c.yours.text)}`,
94 ` theirs, by ${c.theirs.agent} in ${v} ("${c.theirs.message}"): ${value(c.theirs.text)}`,
95 ].join("\n");
96 }
97 if (c.kind === "duplicate") {
98 return [
99 ` ${c.file}: ${c.name ?? "a name"} is defined twice, yours (${describeLines(c.yours.lines)}) and ${c.theirs.agent}'s from ${v} ("${c.theirs.message}") (${describeLines(c.theirs.lines)}). Keep one, then push.`,
100 ` yours:`,
101 indent(c.yours.text),
102 ` theirs:`,
103 indent(c.theirs.text),
104 ].join("\n");
105 }
106 if (c.kind === "definition") {
107 const where = c.theirs.file ?? c.file;
108 return [
109 ` ${c.file}: your draft uses ${c.name ?? "a name"}, whose definition ${c.theirs.agent} changed in ${v} ("${c.theirs.message}").`,
110 ` where you use it (${describeLines(c.yours.lines)}):`,
111 indent(c.yours.text),
112 c.theirs.text === ""
113 ? ` their change removed it from ${where}.`
114 : ` the definition now, in ${where} (${describeLines(c.theirs.lines)}):\n${indent(c.theirs.text)}`,
115 ].join("\n");
116 }
117 return [
118 ` ${c.file}:`,
119 ` yours (${describeLines(c.yours.lines)}):`,
120 indent(c.yours.text),
121 ` theirs, by ${c.theirs.agent} in ${v} ("${c.theirs.message}") (${describeLines(c.theirs.lines)}):`,
122 indent(c.theirs.text),
123 ].join("\n");