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
blame on this site's /mcp
CLI
abso blame client/src/settle.ts --json
JSON
/christopher/abso-core/blame/client/src/settle.ts?format=json
121 linesFirst import 84% · review 16%Last change review in version 12,

121 lines from 2 changes in 2 versions.

init0Start the project1// abso settle <file...> --yours | --theirs | --both: settle a file's
2// collisions by rewriting the marker blocks a sync wrote into it (#11, round
3// 1). A block is exactly four marker lines, in this order, as the engine
4// writes them:
5//
6// <<<<<<< yours (unpublished)
7// ...yours
8// ||||||| before (version N)
9// ...the lines both started from
10// =======
11// ...theirs
12// >>>>>>> theirs: <agent>, version N "<message>"
13//
14// Anything else that merely starts with <<<<<<< (a doc about git, a test
15// fixture) is not a block and is left alone. The server works out at publish
16// which side survived, and tells the other author.
18import { readFileSync } from "node:fs";
19import { UsageError } from "./args.ts";
21export type Keep = "yours" | "theirs" | "both";
23const OPEN = "<<<<<<< yours (unpublished)";
24const BASE = /^\|\|\|\|\|\|\| before \(version \d+\)$/;
25const MID = "=======";
26const CLOSE = /^>>>>>>> theirs: /;
28export interface Block {
29 /** 0-based index of the opening marker line. */
30 readonly start: number;
31 /** 0-based index of the closing marker line. */
32 readonly end: number;
33 readonly yours: readonly string[];
34 readonly base: readonly string[];
35 readonly theirs: readonly string[];
38/** The marker blocks in a file's lines. A run of lines that doesn't follow the four markers in order is not a block. */
39export function findBlocks(lines: readonly string[]): Block[] {
40 const out: Block[] = [];
41 for (let i = 0; i < lines.length; i++) {
42 if (lines[i] !== OPEN) continue;
43 let base = -1;
44 let mid = -1;
45 let end = -1;
46 for (let j = i + 1; j < lines.length; j++) {
47 const l = lines[j]!;
48 if (l === OPEN) break;
49 if (base === -1 && BASE.test(l)) base = j;
50 else if (base !== -1 && mid === -1 && l === MID) mid = j;
51 else if (mid !== -1 && CLOSE.test(l)) {
52 end = j;
53 break;
54 }
55 }
56 if (end === -1) continue;
57 out.push({
58 start: i,
59 end,
60 yours: lines.slice(i + 1, base),
61 base: lines.slice(base + 1, mid),
62 theirs: lines.slice(mid + 1, end),
63 });
64 i = end;
65 }
66 return out;
69/** The text with each block replaced by the side kept (both: yours, then theirs), keeping the file's line endings. */
70export function settleText(text: string, keep: Keep): { text: string; blocks: number } {
71 const eol = text.includes("\r\n") ? "\r\n" : "\n";
72 const lines = text.split(/\r?\n/);
73 const blocks = findBlocks(lines);
74 if (blocks.length === 0) return { text, blocks: 0 };
75 const out: string[] = [];
76 let at = 0;
77 for (const b of blocks) {
78 out.push(...lines.slice(at, b.start));
79 if (keep !== "theirs") out.push(...b.yours);
80 if (keep !== "yours") out.push(...b.theirs);
81 at = b.end + 1;
82 }
83 out.push(...lines.slice(at));
84 return { text: out.join(eol), blocks: blocks.length };
87/** Which side the flags name: exactly one of --yours, --theirs, --both. */
88export function keepFrom(flags: { yours: boolean; theirs: boolean; both: boolean }): Keep {
89 const picked = (["yours", "theirs", "both"] as const).filter((k) => flags[k]);
90 if (picked.length !== 1)
91 throw new UsageError("say which side to keep, once: abso settle <file...> --yours, --theirs or --both");
92 return picked[0]!;
95/** How many blocks a file on disk holds (0 when it can't be read as text). */
96export function blocksIn(path: string): number {
97 try {
98 return findBlocks(readFileSync(path, "utf8").split(/\r?\n/)).length;
99 } catch {
100 return 0;
101 }
review12record and publish note two things git users know the hard way, without blocking. (1) A file that holds what looks like a git conflict (a '<<<<<<< ' line, a '=======' line, then a '>>>>>>> ' line, other than abso's own blocks) gets: 'Note: <file> has lines that look like a git conflict (from line N)...'. (2) A sent path of 160+ characters inside the folder, or 240+ with the folder's location, gets a note that Windows stops at 260. Both are one line per file, only when the file is sent, and the JSON carries gitMarkers and longPaths. gitMarkerLine is in settle.ts, longPathsOf in commands.ts. Tests: three new ones; the whole suite passes (702) and biome ci is clean.Compare103
104/**
105 * The first line (1-based) of what looks like a git conflict left in a text: a "<<<<<<< " line, then a "=======" line,
106 * then a ">>>>>>> " line. abso's own blocks are not these (their opening line is exact, and `abso settle` finds them);
107 * a merge done by git, or markers typed or pasted by hand, are plain text to abso, which would publish them as they are.
108 */
109export function gitMarkerLine(text: string): number | undefined {
110 const lines = text.split(/\r?\n/);
111 for (let i = 0; i < lines.length; i++) {
112 if (!lines[i]!.startsWith("<<<<<<< ") || lines[i] === OPEN) continue;
113 let mid = false;
114 for (let j = i + 1; j < lines.length; j++) {
115 if (lines[j]!.startsWith("<<<<<<< ")) break;
116 if (lines[j] === MID) mid = true;
117 else if (mid && lines[j]!.startsWith(">>>>>>> ")) return i + 1;
118 }
119 }
120 return undefined;