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