@leolastforce/chalice-hashline-edit 0.0.0-stage → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +153 -2
- package/index.ts +282 -0
- package/package.json +79 -4
- package/prompts/find-guidelines.md +2 -0
- package/prompts/find-snippet.md +1 -0
- package/prompts/find.md +1 -0
- package/prompts/grep-guidelines.md +2 -0
- package/prompts/grep-snippet.md +1 -0
- package/prompts/grep.md +1 -0
- package/prompts/insert-guidelines.md +1 -0
- package/prompts/insert-snippet.md +1 -0
- package/prompts/insert.md +1 -0
- package/prompts/read-guidelines.md +2 -0
- package/prompts/read-snippet.md +1 -0
- package/prompts/read.md +1 -0
- package/prompts/replace-guidelines.md +4 -0
- package/prompts/replace-snippet.md +1 -0
- package/prompts/replace.md +1 -0
- package/prompts/undo-last-change-guidelines.md +1 -0
- package/prompts/undo-last-change-snippet.md +1 -0
- package/prompts/undo-last-change.md +1 -0
- package/src/anchor-registry.ts +977 -0
- package/src/auto-read-all-state.ts +27 -0
- package/src/auto-read-all.ts +477 -0
- package/src/batch.ts +767 -0
- package/src/commit.ts +137 -0
- package/src/config-ui.ts +212 -0
- package/src/config.ts +235 -0
- package/src/constants.ts +26 -0
- package/src/edit-common.ts +206 -0
- package/src/file-kind.ts +191 -0
- package/src/file-reader.ts +135 -0
- package/src/find.ts +244 -0
- package/src/fs-write.ts +231 -0
- package/src/glob.ts +186 -0
- package/src/grep.ts +690 -0
- package/src/hash-store/cache.ts +18 -0
- package/src/hash-store/retry.ts +44 -0
- package/src/hash-store/validation.ts +62 -0
- package/src/hash-store.ts +609 -0
- package/src/hashline/alphabet.ts +21 -0
- package/src/hashline/anchor-table.json +1 -0
- package/src/hashline/apply.ts +297 -0
- package/src/hashline/hash.ts +119 -0
- package/src/hashline/hasher.ts +47 -0
- package/src/hashline/index.ts +50 -0
- package/src/hashline/parse.ts +84 -0
- package/src/hashline/resolve.ts +373 -0
- package/src/insert.ts +258 -0
- package/src/normalize.ts +27 -0
- package/src/paths.ts +56 -0
- package/src/payload-contract.ts +133 -0
- package/src/prompts.ts +13 -0
- package/src/read.ts +286 -0
- package/src/replace-diff.ts +610 -0
- package/src/replace-render.ts +390 -0
- package/src/replace-response.ts +166 -0
- package/src/replace-undo.ts +270 -0
- package/src/replace.ts +318 -0
- package/src/served.ts +36 -0
- package/src/utils.ts +387 -0
- package/src/validation.ts +45 -0
- package/src/write-hook.ts +53 -0
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import * as Diff from "hashline-diff";
|
|
2
|
+
import { formatSize, DEFAULT_MAX_BYTES } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import {
|
|
4
|
+
_lineHashesPure,
|
|
5
|
+
ANCHOR_LEN,
|
|
6
|
+
HASH_SEP,
|
|
7
|
+
HASH_CLASS,
|
|
8
|
+
changedRange,
|
|
9
|
+
} from "./hashline";
|
|
10
|
+
import { MAX_DIFF_INPUT_BYTES } from "./constants";
|
|
11
|
+
import { splitLines } from "./utils";
|
|
12
|
+
import {
|
|
13
|
+
detectEnding,
|
|
14
|
+
toLF,
|
|
15
|
+
restoreEndings,
|
|
16
|
+
stripBOM,
|
|
17
|
+
type LineEnding,
|
|
18
|
+
} from "./normalize";
|
|
19
|
+
|
|
20
|
+
export { detectEnding, toLF, restoreEndings, stripBOM, type LineEnding };
|
|
21
|
+
|
|
22
|
+
export interface DiffSpan {
|
|
23
|
+
start: number;
|
|
24
|
+
end: number;
|
|
25
|
+
replacementCount: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PlacedSpan {
|
|
29
|
+
oldStart: number;
|
|
30
|
+
oldEnd: number;
|
|
31
|
+
newStart: number;
|
|
32
|
+
newEnd: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fmtDiffLine(
|
|
36
|
+
prefix: " " | "+" | "-",
|
|
37
|
+
line: string,
|
|
38
|
+
hash: string | undefined,
|
|
39
|
+
): string {
|
|
40
|
+
if (hash === undefined) {
|
|
41
|
+
return `${prefix}${" ".repeat(ANCHOR_LEN)}${HASH_SEP}${line}`;
|
|
42
|
+
}
|
|
43
|
+
return `${prefix}${hash}${HASH_SEP}${line}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isBlankLine(line: string): boolean {
|
|
47
|
+
return line.trim().length === 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ELLIPSIS_MARKER: unique symbol = Symbol("ellipsis");
|
|
51
|
+
const isEllipsisMarker = (line: string | symbol): line is symbol =>
|
|
52
|
+
line === ELLIPSIS_MARKER;
|
|
53
|
+
|
|
54
|
+
export interface DiffLimits {
|
|
55
|
+
maxLineBytes?: number;
|
|
56
|
+
maxBytes?: number;
|
|
57
|
+
unlimited?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function spansFromHashes(oldHashes: string[], newHashes: string[]): DiffSpan[] {
|
|
61
|
+
const parts = Diff.diffArrays(oldHashes, newHashes) as unknown as Array<{ added?: boolean; removed?: boolean; count?: number; value?: string[] }>;
|
|
62
|
+
const spans: DiffSpan[] = [];
|
|
63
|
+
let oldIdx = 0;
|
|
64
|
+
let i = 0;
|
|
65
|
+
while (i < parts.length) {
|
|
66
|
+
const part = parts[i]!;
|
|
67
|
+
const isChange = part.added === true || part.removed === true;
|
|
68
|
+
if (!isChange) {
|
|
69
|
+
const count = part.count ?? part.value?.length ?? 0;
|
|
70
|
+
oldIdx += count;
|
|
71
|
+
i += 1;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const runStart = oldIdx;
|
|
75
|
+
let removedTotal = 0;
|
|
76
|
+
let addedTotal = 0;
|
|
77
|
+
while (i < parts.length) {
|
|
78
|
+
const runPart = parts[i]!;
|
|
79
|
+
const runChange = runPart.added === true || runPart.removed === true;
|
|
80
|
+
if (!runChange) break;
|
|
81
|
+
const count = runPart.count ?? runPart.value?.length ?? 0;
|
|
82
|
+
if (runPart.removed === true && runPart.added !== true) {
|
|
83
|
+
removedTotal += count;
|
|
84
|
+
oldIdx += count;
|
|
85
|
+
} else if (runPart.added === true && runPart.removed !== true) {
|
|
86
|
+
addedTotal += count;
|
|
87
|
+
} else {
|
|
88
|
+
removedTotal += count;
|
|
89
|
+
addedTotal += count;
|
|
90
|
+
oldIdx += count;
|
|
91
|
+
}
|
|
92
|
+
i += 1;
|
|
93
|
+
}
|
|
94
|
+
spans.push({
|
|
95
|
+
start: runStart,
|
|
96
|
+
end: runStart + removedTotal - 1,
|
|
97
|
+
replacementCount: addedTotal,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return spans;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const ANCHORED_DIFF_ROW_RE = new RegExp(`^([+ -])(${HASH_CLASS})│`);
|
|
104
|
+
|
|
105
|
+
export function disambiguateDuplicateAnchors(diff: string): string {
|
|
106
|
+
if (!diff.includes("│")) return diff;
|
|
107
|
+
const live = new Set<string>();
|
|
108
|
+
const lines = diff.split("\n");
|
|
109
|
+
for (const line of lines) {
|
|
110
|
+
const match = ANCHORED_DIFF_ROW_RE.exec(line);
|
|
111
|
+
if (!match) continue;
|
|
112
|
+
const prefix = match[1]!;
|
|
113
|
+
const anchor = match[2]!;
|
|
114
|
+
if (prefix === "+" || prefix === " ") live.add(anchor);
|
|
115
|
+
}
|
|
116
|
+
if (live.size === 0) return diff;
|
|
117
|
+
let changed = false;
|
|
118
|
+
const out = lines.map((line) => {
|
|
119
|
+
const match = ANCHORED_DIFF_ROW_RE.exec(line);
|
|
120
|
+
if (!match || match[1] !== "-") return line;
|
|
121
|
+
if (!live.has(match[2]!)) return line;
|
|
122
|
+
changed = true;
|
|
123
|
+
return `- │${line.slice(match[0].length)}`;
|
|
124
|
+
});
|
|
125
|
+
return changed ? out.join("\n") : diff;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function placeSpans(
|
|
129
|
+
oldLen: number,
|
|
130
|
+
newLen: number,
|
|
131
|
+
spans: DiffSpan[],
|
|
132
|
+
): PlacedSpan[] | undefined {
|
|
133
|
+
const sorted = [...spans].sort((a, b) => a.start - b.start || a.end - b.end);
|
|
134
|
+
for (const span of sorted) {
|
|
135
|
+
if (!Number.isInteger(span.start) || !Number.isInteger(span.end) || !Number.isInteger(span.replacementCount)) return undefined;
|
|
136
|
+
if (span.replacementCount < 0) return undefined;
|
|
137
|
+
if (span.start > span.end) {
|
|
138
|
+
if (span.end !== span.start - 1) return undefined;
|
|
139
|
+
if (span.start < 0 || span.start > oldLen) return undefined;
|
|
140
|
+
} else {
|
|
141
|
+
if (span.start < 0 || span.end >= oldLen) return undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
145
|
+
if (sorted[i]!.start <= sorted[i - 1]!.end) return undefined;
|
|
146
|
+
}
|
|
147
|
+
const placed: PlacedSpan[] = [];
|
|
148
|
+
let offset = 0;
|
|
149
|
+
let removedSum = 0;
|
|
150
|
+
let addedSum = 0;
|
|
151
|
+
for (const span of sorted) {
|
|
152
|
+
const spanLen = Math.max(0, span.end - span.start + 1);
|
|
153
|
+
const newStart = span.start + offset;
|
|
154
|
+
const newEnd = newStart + span.replacementCount - 1;
|
|
155
|
+
if (newStart < 0 || newStart > newLen) return undefined;
|
|
156
|
+
if (span.replacementCount === 0) {
|
|
157
|
+
if (newEnd !== newStart - 1) return undefined;
|
|
158
|
+
} else if (newEnd >= newLen) return undefined;
|
|
159
|
+
placed.push({ oldStart: span.start, oldEnd: span.end, newStart, newEnd });
|
|
160
|
+
offset += span.replacementCount - spanLen;
|
|
161
|
+
removedSum += spanLen;
|
|
162
|
+
addedSum += span.replacementCount;
|
|
163
|
+
}
|
|
164
|
+
if (oldLen - removedSum + addedSum !== newLen) return undefined;
|
|
165
|
+
return placed;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function trimPlacedSpans(
|
|
169
|
+
placed: PlacedSpan[],
|
|
170
|
+
oldLines: string[],
|
|
171
|
+
newLines: string[],
|
|
172
|
+
): PlacedSpan[] {
|
|
173
|
+
const out: PlacedSpan[] = [];
|
|
174
|
+
for (const span of placed) {
|
|
175
|
+
let oldStart = span.oldStart;
|
|
176
|
+
let oldEnd = span.oldEnd;
|
|
177
|
+
let newStart = span.newStart;
|
|
178
|
+
let newEnd = span.newEnd;
|
|
179
|
+
while (oldStart <= oldEnd && newStart <= newEnd && oldLines[oldStart] === newLines[newStart]) {
|
|
180
|
+
oldStart += 1;
|
|
181
|
+
newStart += 1;
|
|
182
|
+
}
|
|
183
|
+
while (oldStart <= oldEnd && newStart <= newEnd && oldLines[oldEnd] === newLines[newEnd]) {
|
|
184
|
+
oldEnd -= 1;
|
|
185
|
+
newEnd -= 1;
|
|
186
|
+
}
|
|
187
|
+
if (oldStart > oldEnd && newStart > newEnd) continue;
|
|
188
|
+
out.push({ oldStart, oldEnd, newStart, newEnd });
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function createRowEmitter(
|
|
194
|
+
output: string[],
|
|
195
|
+
lineNumbers: (number | undefined)[],
|
|
196
|
+
maxLineBytes: number,
|
|
197
|
+
maxBytes: number,
|
|
198
|
+
): {
|
|
199
|
+
readonly stopped: boolean;
|
|
200
|
+
readonly truncated: boolean;
|
|
201
|
+
emitPlain(line: string, num?: number): void;
|
|
202
|
+
emitRow(prefix: " " | "+" | "-", line: string, hash: string | undefined, num?: number): void;
|
|
203
|
+
} {
|
|
204
|
+
let outBytes = 0;
|
|
205
|
+
let stopped = false;
|
|
206
|
+
let truncated = false;
|
|
207
|
+
const emitPlain = (line: string, num?: number): void => {
|
|
208
|
+
if (stopped) return;
|
|
209
|
+
const lineBytes = Buffer.byteLength(line, "utf-8") + 1;
|
|
210
|
+
if (outBytes + lineBytes > maxBytes) {
|
|
211
|
+
stopped = true;
|
|
212
|
+
truncated = true;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
outBytes += lineBytes;
|
|
216
|
+
output.push(line);
|
|
217
|
+
lineNumbers.push(num);
|
|
218
|
+
};
|
|
219
|
+
const emitRow = (prefix: " " | "+" | "-", line: string, hash: string | undefined, num?: number): void => {
|
|
220
|
+
if (stopped) return;
|
|
221
|
+
const full = fmtDiffLine(prefix, line, hash);
|
|
222
|
+
const rowBytes = Buffer.byteLength(full, "utf-8");
|
|
223
|
+
if (rowBytes > maxLineBytes) {
|
|
224
|
+
const marker = `[Row is ${formatSize(rowBytes)}, exceeds ${formatSize(maxLineBytes)}; content not shown. Use read to see the full line.]`;
|
|
225
|
+
emitPlain(fmtDiffLine(prefix, marker, hash), num);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (outBytes + rowBytes + 1 > maxBytes) {
|
|
229
|
+
stopped = true;
|
|
230
|
+
truncated = true;
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
outBytes += rowBytes + 1;
|
|
234
|
+
output.push(full);
|
|
235
|
+
lineNumbers.push(num);
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
get stopped() { return stopped; },
|
|
239
|
+
get truncated() { return truncated; },
|
|
240
|
+
emitPlain,
|
|
241
|
+
emitRow,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function genSpanDiff(
|
|
246
|
+
oldContent: string,
|
|
247
|
+
newContent: string,
|
|
248
|
+
contextLines: number,
|
|
249
|
+
newHashes: string[],
|
|
250
|
+
oldHashes: string[],
|
|
251
|
+
maxLineBytes: number,
|
|
252
|
+
maxBytes: number,
|
|
253
|
+
spans: DiffSpan[],
|
|
254
|
+
): { diff: string; firstChangedLine: number | undefined; lineNumbers: (number | undefined)[] } | undefined {
|
|
255
|
+
const oldLines = splitLines(oldContent);
|
|
256
|
+
const newLines = splitLines(newContent);
|
|
257
|
+
if (oldHashes.length !== oldLines.length || newHashes.length !== newLines.length) return undefined;
|
|
258
|
+
const placed = placeSpans(oldLines.length, newLines.length, spans);
|
|
259
|
+
if (!placed) return undefined;
|
|
260
|
+
const trimmed = trimPlacedSpans(placed, oldLines, newLines);
|
|
261
|
+
const ordered = [...trimmed].sort((a, b) => a.oldStart - b.oldStart || a.newStart - b.newStart);
|
|
262
|
+
let oldPos = 0;
|
|
263
|
+
let newPos = 0;
|
|
264
|
+
for (const span of ordered) {
|
|
265
|
+
if (span.oldStart < oldPos || span.newStart < newPos) return undefined;
|
|
266
|
+
const gapOld = span.oldStart - oldPos;
|
|
267
|
+
const gapNew = span.newStart - newPos;
|
|
268
|
+
if (gapOld !== gapNew || gapOld < 0) return undefined;
|
|
269
|
+
oldPos = span.oldEnd + 1;
|
|
270
|
+
newPos = span.newEnd + 1;
|
|
271
|
+
}
|
|
272
|
+
if (oldLines.length - ordered.reduce((sum, span) => sum + Math.max(0, span.oldEnd - span.oldStart + 1), 0) + ordered.reduce((sum, span) => sum + Math.max(0, span.newEnd - span.newStart + 1), 0) !== newLines.length) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
const output: string[] = [];
|
|
276
|
+
const lineNumbers: (number | undefined)[] = [];
|
|
277
|
+
let firstChangedLine: number | undefined;
|
|
278
|
+
const em = createRowEmitter(output, lineNumbers, maxLineBytes, maxBytes);
|
|
279
|
+
const emitGap = (gapStartNew: number, gapLen: number, position: "leading" | "middle" | "trailing"): void => {
|
|
280
|
+
if (em.stopped || gapLen <= 0) return;
|
|
281
|
+
if (position === "leading") {
|
|
282
|
+
let count = contextLines;
|
|
283
|
+
if (contextLines > 0 && gapLen > count && isBlankLine(newLines[gapStartNew + gapLen - 1]!)) count += 1;
|
|
284
|
+
count = Math.min(count, gapLen);
|
|
285
|
+
const skipStart = gapLen - count;
|
|
286
|
+
if (skipStart > 0) em.emitPlain(" ...", undefined);
|
|
287
|
+
for (let k = gapLen - count; k < gapLen && !em.stopped; k++) {
|
|
288
|
+
const newIdx = gapStartNew + k;
|
|
289
|
+
em.emitRow(" ", newLines[newIdx]!, newHashes[newIdx], newIdx + 1);
|
|
290
|
+
}
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (position === "trailing") {
|
|
294
|
+
let count = contextLines;
|
|
295
|
+
if (contextLines > 0 && gapLen > count && isBlankLine(newLines[gapStartNew]!)) count += 1;
|
|
296
|
+
count = Math.min(count, gapLen);
|
|
297
|
+
for (let k = 0; k < count && !em.stopped; k++) {
|
|
298
|
+
const newIdx = gapStartNew + k;
|
|
299
|
+
em.emitRow(" ", newLines[newIdx]!, newHashes[newIdx], newIdx + 1);
|
|
300
|
+
}
|
|
301
|
+
if (gapLen > count) em.emitPlain(" ...", undefined);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
let headCount = contextLines;
|
|
305
|
+
let tailCount = contextLines;
|
|
306
|
+
if (contextLines > 0 && gapLen - headCount > tailCount && isBlankLine(newLines[gapStartNew + headCount - 1]!)) headCount += 1;
|
|
307
|
+
if (contextLines > 0 && gapLen - tailCount > headCount && isBlankLine(newLines[gapStartNew + gapLen - tailCount]!)) tailCount += 1;
|
|
308
|
+
const middleLen = gapLen - headCount - tailCount;
|
|
309
|
+
if (middleLen <= 0) {
|
|
310
|
+
for (let k = 0; k < gapLen && !em.stopped; k++) {
|
|
311
|
+
const newIdx = gapStartNew + k;
|
|
312
|
+
em.emitRow(" ", newLines[newIdx]!, newHashes[newIdx], newIdx + 1);
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
for (let k = 0; k < headCount && !em.stopped; k++) {
|
|
317
|
+
const newIdx = gapStartNew + k;
|
|
318
|
+
em.emitRow(" ", newLines[newIdx]!, newHashes[newIdx], newIdx + 1);
|
|
319
|
+
}
|
|
320
|
+
if (!em.stopped) em.emitPlain(" ...", undefined);
|
|
321
|
+
for (let k = gapLen - tailCount; k < gapLen && !em.stopped; k++) {
|
|
322
|
+
const newIdx = gapStartNew + k;
|
|
323
|
+
em.emitRow(" ", newLines[newIdx]!, newHashes[newIdx], newIdx + 1);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
oldPos = 0;
|
|
327
|
+
newPos = 0;
|
|
328
|
+
for (let spanIdx = 0; spanIdx < ordered.length && !em.stopped; spanIdx++) {
|
|
329
|
+
const span = ordered[spanIdx]!;
|
|
330
|
+
const gapLen = span.oldStart - oldPos;
|
|
331
|
+
emitGap(newPos, gapLen, spanIdx === 0 ? "leading" : "middle");
|
|
332
|
+
if (em.stopped) break;
|
|
333
|
+
if (firstChangedLine === undefined) firstChangedLine = span.newStart + 1;
|
|
334
|
+
for (let oldIdx = span.oldStart; oldIdx <= span.oldEnd && !em.stopped; oldIdx++) {
|
|
335
|
+
em.emitRow("-", oldLines[oldIdx]!, oldHashes[oldIdx], oldIdx + 1);
|
|
336
|
+
}
|
|
337
|
+
for (let newIdx = span.newStart; newIdx <= span.newEnd && !em.stopped; newIdx++) {
|
|
338
|
+
em.emitRow("+", newLines[newIdx]!, newHashes[newIdx], newIdx + 1);
|
|
339
|
+
}
|
|
340
|
+
oldPos = span.oldEnd + 1;
|
|
341
|
+
newPos = span.newEnd + 1;
|
|
342
|
+
}
|
|
343
|
+
if (!em.stopped) {
|
|
344
|
+
const trailingLen = oldLines.length - oldPos;
|
|
345
|
+
emitGap(newPos, trailingLen, "trailing");
|
|
346
|
+
}
|
|
347
|
+
if (em.truncated) {
|
|
348
|
+
output.push(" ...");
|
|
349
|
+
lineNumbers.push(undefined);
|
|
350
|
+
output.push(`[diff truncated at ${formatSize(maxBytes)}; use read to see the rest.]`);
|
|
351
|
+
lineNumbers.push(undefined);
|
|
352
|
+
}
|
|
353
|
+
return { diff: output.join("\n"), firstChangedLine, lineNumbers };
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function overDiffInputLimit(oldContent: string, newContent: string): boolean {
|
|
357
|
+
return Buffer.byteLength(oldContent, "utf-8") + Buffer.byteLength(newContent, "utf-8") > MAX_DIFF_INPUT_BYTES;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function genDiff(
|
|
361
|
+
oldContent: string,
|
|
362
|
+
newContent: string,
|
|
363
|
+
contextLines = 2,
|
|
364
|
+
newContentHashes?: string[],
|
|
365
|
+
oldContentHashes?: string[],
|
|
366
|
+
limits?: DiffLimits,
|
|
367
|
+
spans?: DiffSpan[],
|
|
368
|
+
): { diff: string; firstChangedLine: number | undefined; lineNumbers: (number|undefined)[] } {
|
|
369
|
+
const maxLineBytes = limits?.unlimited ? Number.POSITIVE_INFINITY : (limits?.maxLineBytes ?? DEFAULT_MAX_BYTES);
|
|
370
|
+
const maxBytes = limits?.unlimited ? Number.POSITIVE_INFINITY : (limits?.maxBytes ?? DEFAULT_MAX_BYTES);
|
|
371
|
+
if (spans && newContentHashes && oldContentHashes) {
|
|
372
|
+
const anchored = genSpanDiff(oldContent, newContent, contextLines, newContentHashes, oldContentHashes, maxLineBytes, maxBytes, spans);
|
|
373
|
+
if (anchored) {
|
|
374
|
+
const diff = disambiguateDuplicateAnchors(anchored.diff);
|
|
375
|
+
return { diff, firstChangedLine: anchored.firstChangedLine, lineNumbers: anchored.lineNumbers };
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (!limits?.unlimited && overDiffInputLimit(oldContent, newContent)) {
|
|
379
|
+
const guardedRange = changedRange(oldContent, newContent);
|
|
380
|
+
const guardNote = `[diff truncated at ${formatSize(maxBytes)}; use read to see the rest.]`;
|
|
381
|
+
if (!guardedRange || !newContentHashes) {
|
|
382
|
+
return { diff: ` ...\n${guardNote}`, firstChangedLine: guardedRange?.firstChangedLine, lineNumbers: [undefined, undefined] };
|
|
383
|
+
}
|
|
384
|
+
const newLines = splitLines(newContent);
|
|
385
|
+
const oldLines = splitLines(oldContent);
|
|
386
|
+
const first = guardedRange.firstChangedLine;
|
|
387
|
+
const last = guardedRange.lastChangedLine;
|
|
388
|
+
const suffixLen = newLines.length - last;
|
|
389
|
+
const oldLast = oldLines.length - suffixLen;
|
|
390
|
+
const beforeStart = Math.max(1, first - contextLines);
|
|
391
|
+
const afterEnd = Math.min(newLines.length, last + contextLines);
|
|
392
|
+
const guarded: string[] = [];
|
|
393
|
+
const guardedNumbers: (number|undefined)[] = [];
|
|
394
|
+
let guardedBytes = 0;
|
|
395
|
+
const pushGuarded = (text: string, num?: number): boolean => {
|
|
396
|
+
const size = Buffer.byteLength(text, "utf-8") + 1;
|
|
397
|
+
if (guardedBytes + size > maxBytes) return false;
|
|
398
|
+
guardedBytes += size;
|
|
399
|
+
guarded.push(text);
|
|
400
|
+
guardedNumbers.push(num);
|
|
401
|
+
return true;
|
|
402
|
+
};
|
|
403
|
+
const pushGuardedRow = (prefix: " " | "+" | "-", line: string, hash: string | undefined, num?: number): boolean => {
|
|
404
|
+
const full = fmtDiffLine(prefix, line, hash);
|
|
405
|
+
if (Buffer.byteLength(full, "utf-8") > maxLineBytes) {
|
|
406
|
+
const marker = `[Row is ${formatSize(Buffer.byteLength(full, "utf-8"))}, exceeds ${formatSize(maxLineBytes)}; content not shown. Use read to see the full line.]`;
|
|
407
|
+
return pushGuarded(fmtDiffLine(prefix, marker, hash), num);
|
|
408
|
+
}
|
|
409
|
+
return pushGuarded(full, num);
|
|
410
|
+
};
|
|
411
|
+
if (beforeStart > 1) pushGuarded(" ...", undefined);
|
|
412
|
+
for (let n = beforeStart; n < first; n++) {
|
|
413
|
+
if (!pushGuardedRow(" ", newLines[n - 1]!, newContentHashes[n - 1], n)) break;
|
|
414
|
+
}
|
|
415
|
+
if (oldContent.length > 0) {
|
|
416
|
+
for (let n = first; n <= Math.min(oldLast, oldLines.length); n++) {
|
|
417
|
+
if (!pushGuardedRow("-", oldLines[n - 1]!, oldContentHashes?.[n - 1], n)) break;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
for (let n = first; n <= last; n++) {
|
|
421
|
+
if (!pushGuardedRow("+", newLines[n - 1]!, newContentHashes[n - 1], n)) break;
|
|
422
|
+
}
|
|
423
|
+
for (let n = last + 1; n <= afterEnd; n++) {
|
|
424
|
+
if (!pushGuardedRow(" ", newLines[n - 1]!, newContentHashes[n - 1], n)) break;
|
|
425
|
+
}
|
|
426
|
+
guarded.push(" ...");
|
|
427
|
+
guardedNumbers.push(undefined);
|
|
428
|
+
guarded.push(guardNote);
|
|
429
|
+
guardedNumbers.push(undefined);
|
|
430
|
+
return { diff: disambiguateDuplicateAnchors(guarded.join("\n")), firstChangedLine: first, lineNumbers: guardedNumbers };
|
|
431
|
+
}
|
|
432
|
+
const effectiveNewHashes = newContentHashes ?? _lineHashesPure(newContent);
|
|
433
|
+
|
|
434
|
+
const parts = Diff.diffLines(oldContent, newContent);
|
|
435
|
+
const output: string[] = [];
|
|
436
|
+
let newLineNum = 1;
|
|
437
|
+
let oldLineNum = 1;
|
|
438
|
+
let lastWasChange = false;
|
|
439
|
+
let firstChangedLine: number | undefined;
|
|
440
|
+
const lineNumbers: (number|undefined)[] = [];
|
|
441
|
+
|
|
442
|
+
const em = createRowEmitter(output, lineNumbers, maxLineBytes, maxBytes);
|
|
443
|
+
|
|
444
|
+
for (let i = 0; i < parts.length; i++) {
|
|
445
|
+
if (em.stopped) break;
|
|
446
|
+
const part = parts[i]!;
|
|
447
|
+
const raw = part.value.split("\n");
|
|
448
|
+
if (raw[raw.length - 1] === "") raw.pop();
|
|
449
|
+
const displayLines = raw;
|
|
450
|
+
|
|
451
|
+
if (part.added || part.removed) {
|
|
452
|
+
if (firstChangedLine === undefined) firstChangedLine = newLineNum;
|
|
453
|
+
for (let k = 0; k < displayLines.length; k++) {
|
|
454
|
+
if (em.stopped) break;
|
|
455
|
+
if (part.added) {
|
|
456
|
+
const hash = effectiveNewHashes[newLineNum - 1];
|
|
457
|
+
em.emitRow("+", displayLines[k]!, hash, newLineNum);
|
|
458
|
+
newLineNum++;
|
|
459
|
+
} else {
|
|
460
|
+
const hash = oldContentHashes?.[oldLineNum - 1];
|
|
461
|
+
em.emitRow("-", displayLines[k]!, hash, oldLineNum);
|
|
462
|
+
oldLineNum++;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (em.stopped) break;
|
|
466
|
+
lastWasChange = true;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const nextPartIsChange =
|
|
471
|
+
i < parts.length - 1 && (parts[i + 1]!.added || parts[i + 1]!.removed);
|
|
472
|
+
if (lastWasChange || nextPartIsChange) {
|
|
473
|
+
let linesToShow: (string | symbol)[] = displayLines;
|
|
474
|
+
let skipStart = 0;
|
|
475
|
+
let skipMiddle = 0;
|
|
476
|
+
let skipTail = 0;
|
|
477
|
+
|
|
478
|
+
if (!lastWasChange) {
|
|
479
|
+
let count = contextLines;
|
|
480
|
+
if (
|
|
481
|
+
contextLines > 0 &&
|
|
482
|
+
displayLines.length > count &&
|
|
483
|
+
isBlankLine(displayLines[displayLines.length - 1]!)
|
|
484
|
+
) {
|
|
485
|
+
count += 1;
|
|
486
|
+
}
|
|
487
|
+
count = Math.min(count, displayLines.length);
|
|
488
|
+
skipStart = displayLines.length - count;
|
|
489
|
+
linesToShow = displayLines.slice(skipStart);
|
|
490
|
+
} else if (nextPartIsChange && displayLines.length > contextLines * 2) {
|
|
491
|
+
let headCount = contextLines;
|
|
492
|
+
let tailCount = contextLines;
|
|
493
|
+
if (
|
|
494
|
+
contextLines > 0 &&
|
|
495
|
+
displayLines.length - headCount > tailCount &&
|
|
496
|
+
isBlankLine(displayLines[headCount - 1]!)
|
|
497
|
+
) {
|
|
498
|
+
headCount += 1;
|
|
499
|
+
}
|
|
500
|
+
if (
|
|
501
|
+
contextLines > 0 &&
|
|
502
|
+
displayLines.length - tailCount > headCount &&
|
|
503
|
+
isBlankLine(displayLines[displayLines.length - tailCount]!)
|
|
504
|
+
) {
|
|
505
|
+
tailCount += 1;
|
|
506
|
+
}
|
|
507
|
+
const middleLen = displayLines.length - headCount - tailCount;
|
|
508
|
+
if (middleLen > 0) {
|
|
509
|
+
linesToShow = [
|
|
510
|
+
...displayLines.slice(0, headCount),
|
|
511
|
+
ELLIPSIS_MARKER,
|
|
512
|
+
...displayLines.slice(displayLines.length - tailCount),
|
|
513
|
+
];
|
|
514
|
+
skipMiddle = middleLen;
|
|
515
|
+
} else {
|
|
516
|
+
linesToShow = displayLines;
|
|
517
|
+
}
|
|
518
|
+
} else if (!nextPartIsChange && linesToShow.length > contextLines) {
|
|
519
|
+
let count = contextLines;
|
|
520
|
+
const firstLine = linesToShow[0];
|
|
521
|
+
if (contextLines > 0 && typeof firstLine === "string" && isBlankLine(firstLine)) count += 1;
|
|
522
|
+
count = Math.min(count, linesToShow.length);
|
|
523
|
+
linesToShow = linesToShow.slice(0, count);
|
|
524
|
+
skipTail = displayLines.length - count;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (skipStart > 0) {
|
|
528
|
+
em.emitPlain(" ...", undefined);
|
|
529
|
+
newLineNum += skipStart;
|
|
530
|
+
oldLineNum += skipStart;
|
|
531
|
+
}
|
|
532
|
+
for (const line of linesToShow) {
|
|
533
|
+
if (em.stopped) break;
|
|
534
|
+
if (isEllipsisMarker(line)) {
|
|
535
|
+
em.emitPlain(" ...", undefined);
|
|
536
|
+
newLineNum += skipMiddle;
|
|
537
|
+
oldLineNum += skipMiddle;
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
const hash = effectiveNewHashes[newLineNum - 1];
|
|
541
|
+
em.emitRow(" ", line, hash, newLineNum);
|
|
542
|
+
newLineNum++;
|
|
543
|
+
oldLineNum++;
|
|
544
|
+
}
|
|
545
|
+
if (skipTail > 0) {
|
|
546
|
+
em.emitPlain(" ...", undefined);
|
|
547
|
+
}
|
|
548
|
+
} else {
|
|
549
|
+
newLineNum += displayLines.length;
|
|
550
|
+
oldLineNum += displayLines.length;
|
|
551
|
+
}
|
|
552
|
+
lastWasChange = false;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (em.truncated) {
|
|
556
|
+
output.push(" ...");
|
|
557
|
+
lineNumbers.push(undefined);
|
|
558
|
+
output.push(`[diff truncated at ${formatSize(maxBytes)}; use read to see the rest.]`);
|
|
559
|
+
lineNumbers.push(undefined);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
const diff = newContentHashes && oldContentHashes ? disambiguateDuplicateAnchors(output.join("\n")) : output.join("\n");
|
|
563
|
+
return { diff, firstChangedLine, lineNumbers };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function genPatch(
|
|
567
|
+
path: string,
|
|
568
|
+
oldContent: string,
|
|
569
|
+
newContent: string,
|
|
570
|
+
limits?: DiffLimits,
|
|
571
|
+
): { patch: string; truncated: boolean } {
|
|
572
|
+
const patchOpts: Record<string, unknown> = { context: 4 };
|
|
573
|
+
const ho = (Diff as unknown as Record<string, unknown>).FILE_HEADERS_ONLY;
|
|
574
|
+
if (ho !== undefined) patchOpts.headerOptions = ho;
|
|
575
|
+
if (!limits?.unlimited && overDiffInputLimit(oldContent, newContent)) {
|
|
576
|
+
return { patch: "", truncated: true };
|
|
577
|
+
}
|
|
578
|
+
const full = (Diff.createTwoFilesPatch(path, path, oldContent, newContent, undefined, undefined, patchOpts as never) as unknown as string) ?? "";
|
|
579
|
+
const maxLineBytes = limits?.unlimited ? Number.POSITIVE_INFINITY : (limits?.maxLineBytes ?? DEFAULT_MAX_BYTES);
|
|
580
|
+
const maxBytes = limits?.unlimited ? Number.POSITIVE_INFINITY : (limits?.maxBytes ?? DEFAULT_MAX_BYTES);
|
|
581
|
+
const out: string[] = [];
|
|
582
|
+
let outBytes = 0;
|
|
583
|
+
let truncated = false;
|
|
584
|
+
for (const line of full.split("\n")) {
|
|
585
|
+
const lineBytes = Buffer.byteLength(line, "utf-8");
|
|
586
|
+
if (lineBytes > maxLineBytes) {
|
|
587
|
+
truncated = true;
|
|
588
|
+
const prefix = /^[ +-]/.test(line) ? line[0]! : "";
|
|
589
|
+
const marker = `${prefix}[Patch line is ${formatSize(lineBytes)}, exceeds ${formatSize(maxLineBytes)}; content not shown. Use read to see the full line.]`;
|
|
590
|
+
const markerBytes = Buffer.byteLength(marker, "utf-8") + 1;
|
|
591
|
+
if (outBytes + markerBytes > maxBytes) {
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
outBytes += markerBytes;
|
|
595
|
+
out.push(marker);
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (outBytes + lineBytes + 1 > maxBytes) {
|
|
599
|
+
truncated = true;
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
outBytes += lineBytes + 1;
|
|
603
|
+
out.push(line);
|
|
604
|
+
}
|
|
605
|
+
if (truncated) {
|
|
606
|
+
out.push("...");
|
|
607
|
+
out.push(`[patch truncated at ${formatSize(maxBytes)}; the patch cannot be applied as-is. Use read to see the full file.]`);
|
|
608
|
+
}
|
|
609
|
+
return { patch: out.join("\n"), truncated };
|
|
610
|
+
}
|