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/diff.ts?version=281&format=json
178 linesFirst import 100%Last change First import in version 0, 1 version changed it

You're reading version 281. The newest is 318, and no version since then changed this file. Go to the newestCompare with 318

1// Line diffs in git's unified format, from a file's text before and after.
2// The server sends whole texts (null where a file doesn't exist); the client
3// turns them into hunks, because agents already read git's format.
4
5import type { FileChange } from "./api.ts";
6import { editScript, linesOf } from "./core.ts";
7
8/** git's default: a deleted and an added file are one renamed file when at least half of it is the same. */
9export const RENAME_SIMILARITY = 50;
10/** Past this many deleted or added files, renames are not looked for (git has diff.renameLimit for the same reason). */
11const RENAME_LIMIT = 400;
13/** How much of `a` and `b` is the same, 0-100: the lines they share over the longer one's lines. */
14function similarity(a: string, b: string): number {
15 if (a === b) return 100;
16 const la = linesOf(a);
17 const lb = linesOf(b);
18 const longer = Math.max(la.length, lb.length);
19 // Too different in length to reach the threshold: skip the diff.
20 if (longer === 0 || Math.min(la.length, lb.length) * 100 < longer * RENAME_SIMILARITY) return 0;
21 const same = editScript(la, lb).filter((o) => o.kind === " ").length;
22 return Math.floor((same * 100) / longer);
25/**
26 * Pairs each deleted file with the added file most like it, as git does, so a
27 * rename reads as one change (with its edits) instead of a delete and an add.
28 * The server keeps each line's identity across a rename already (blame
29 * follows it); this is how the change is shown.
30 */
31export function pairRenames(files: readonly FileChange[]): FileChange[] {
32 const gone = files.filter(
33 (f) => (f.before !== null && f.after === null) || (f.blob?.before && f.blob.after === null),
34 );
35 const added = files.filter(
36 (f) => (f.before === null && f.after !== null) || (f.blob?.after && f.blob.before === null),
37 );
38 if (gone.length === 0 || added.length === 0 || gone.length > RENAME_LIMIT || added.length > RENAME_LIMIT)
39 return [...files];
40 const scored: { g: FileChange; a: FileChange; score: number }[] = [];
41 for (const g of gone) {
42 for (const a of added) {
43 const score =
44 g.blob?.before || a.blob?.after
45 ? g.blob?.before && g.blob.before === a.blob?.after
46 ? 100
47 : 0
48 : similarity(g.before!, a.after!);
49 if (score >= RENAME_SIMILARITY) scored.push({ g, a, score });
50 }
51 }
52 // Best matches first; ties go to the closer name, as a reader would guess.
53 const base = (p: string) => p.slice(p.lastIndexOf("/") + 1);
54 scored.sort(
55 (x, y) =>
56 y.score - x.score || Number(base(y.g.path) === base(y.a.path)) - Number(base(x.g.path) === base(x.a.path)),
57 );
58 const used = new Set<FileChange>();
59 const renamed = new Map<FileChange, FileChange>();
60 for (const { g, a, score } of scored) {
61 if (used.has(g) || used.has(a)) continue;
62 used.add(g).add(a);
63 const mode = g.mode || a.mode ? { mode: { before: g.mode?.before ?? {}, after: a.mode?.after ?? {} } } : {};
64 const blob = g.blob || a.blob ? { blob: { before: g.blob?.before ?? null, after: a.blob?.after ?? null } } : {};
65 renamed.set(a, {
66 path: a.path,
67 from: g.path,
68 similarity: score,
69 before: g.before,
70 after: a.after,
71 ...mode,
72 ...blob,
73 });
74 }
75 return files.filter((f) => !used.has(f) || renamed.has(f)).map((f) => renamed.get(f) ?? f);
78/** One file's change as git prints it: headers, then hunks with `context` lines around each change. */
79export function unifiedDiff(change: FileChange, context = 3): string {
80 const rename =
81 change.from === undefined
82 ? []
83 : [`similarity index ${change.similarity ?? 100}%`, `rename from ${change.from}`, `rename to ${change.path}`];
84 if (change.blob !== undefined) {
85 // As git says it: no lines to show, only that the bytes differ (here, which bytes, by hash).
86 const name = (sha: string | null) => (sha === null ? "none" : sha.slice(0, 12));
87 if (change.from !== undefined && change.blob.before === change.blob.after)
88 return [...rename, ...modeLines(change)].join("\n");
89 return [
90 ...rename,
91 ...modeLines(change),
92 `Binary file ${change.path} changed: ${name(change.blob.before)} -> ${name(change.blob.after)}`,
93 ].join("\n");
94 }
95 const ops = editScript(linesOf(change.before), linesOf(change.after));
96 // A rename with no edits is its header alone, as in git.
97 if (change.from !== undefined && ops.every((o) => o.kind === " "))
98 return [...rename, ...modeLines(change)].join("\n");
99 const out = [
100 ...rename,
101 ...modeLines(change),
102 `--- ${change.before === null ? "/dev/null" : `a/${change.from ?? change.path}`}`,
103 `+++ ${change.after === null ? "/dev/null" : `b/${change.path}`}`,
104 ];
105 // Positions (1-based) in the old and new file at the start of each op.
106 const at: { a: number; b: number }[] = [];
107 let ai = 1;
108 let bi = 1;
109 for (const op of ops) {
110 at.push({ a: ai, b: bi });
111 if (op.kind !== "+") ai++;
112 if (op.kind !== "-") bi++;
113 }
114 let i = 0;
115 while (i < ops.length) {
116 if (ops[i]!.kind === " ") {
117 i++;
118 continue;
119 }
120 // A hunk: from `context` lines before this change to `context` lines after the last change within reach.
121 const start = Math.max(0, i - context);
122 let end = i;
123 let j = i;
124 while (j < ops.length) {
125 if (ops[j]!.kind !== " ") {
126 end = j;
127 j++;
128 continue;
129 }
130 let run = j;
131 while (run < ops.length && ops[run]!.kind === " ") run++;
132 if (run < ops.length && run - j <= 2 * context) j = run;
133 else break;
134 }
135 const stop = Math.min(ops.length, end + 1 + context);
136 const hunk = ops.slice(start, stop);
137 const oldLen = hunk.filter((o) => o.kind !== "+").length;
138 const newLen = hunk.filter((o) => o.kind !== "-").length;
139 const oldStart = oldLen === 0 ? at[start]!.a - 1 : at[start]!.a;
140 const newStart = newLen === 0 ? at[start]!.b - 1 : at[start]!.b;
141 out.push(`@@ -${range(oldStart, oldLen)} +${range(newStart, newLen)} @@`);
142 for (const o of hunk) out.push(`${o.kind}${o.line}`);
143 i = stop;
144 }
145 return out.join("\n");
148/** A change to the executable bit as git prints it, and a change of link target. */
149function modeLines(change: FileChange): string[] {
150 const m = change.mode;
151 if (m === undefined) return [];
152 const out: string[] = [];
153 const execBefore = m.before.exec === true;
154 const execAfter = m.after.exec === true;
155 if (execBefore !== execAfter)
156 out.push(`old mode ${execBefore ? "100755" : "100644"}`, `new mode ${execAfter ? "100755" : "100644"}`);
157 const linkBefore = m.before.link ?? null;
158 const linkAfter = m.after.link ?? null;
159 if (linkBefore !== linkAfter) out.push(`link: ${linkBefore ?? "(not a link)"} -> ${linkAfter ?? "(not a link)"}`);
160 return out;
163/** A hunk's range as git writes it: the length is left out when it is 1. */
164function range(start: number, length: number): string {
165 return length === 1 ? String(start) : `${start},${length}`;
168/** How many lines a change adds and removes. */
169export function lineCounts(change: FileChange): { added: number; removed: number } {
170 if (change.blob !== undefined) return { added: 0, removed: 0 };
171 let added = 0;
172 let removed = 0;
173 for (const op of editScript(linesOf(change.before), linesOf(change.after))) {
174 if (op.kind === "+") added++;
175 else if (op.kind === "-") removed++;
176 }
177 return { added, removed };