Repository navigation
Expand file tree
/
Copy pathpatch.ts
More file actions
382 lines (364 loc) · 13.1 KB
/
Copy pathpatch.ts
File metadata and controls
382 lines (364 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
export * as Patch from "./patch.js"
import { Result, Schema } from "effect"
import { Bom } from "./bom.js"
export class BoundaryError extends Schema.TaggedError<BoundaryError>()("Patch.BoundaryError", {
boundary: Schema.Literals(["first", "last"]),
}) {
override get message() {
return `The ${this.boundary} line of the patch must be '${this.boundary === "first" ? "*** Begin Patch" : "*** End Patch"}'`
}
}
export class InvalidHunkError extends Schema.TaggedError<InvalidHunkError>()("Patch.InvalidHunkError", {
line: Schema.String,
lineNumber: Schema.Number,
reason: Schema.optional(Schema.String),
}) {
override get message() {
if (this.reason) return `Invalid hunk at line ${this.lineNumber}: ${this.reason}`
return `Invalid hunk at line ${this.lineNumber}: '${this.line}' is not a valid hunk header. Valid hunk headers: '*** Add File: {path}', '*** Delete File: {path}', '*** Update File: {path}'`
}
}
export type ParseError = BoundaryError | InvalidHunkError
export type Hunk =
| { readonly type: "add"; readonly path: string; readonly contents: string }
| { readonly type: "delete"; readonly path: string }
| {
readonly type: "update"
readonly path: string
readonly movePath?: string
readonly chunks: ReadonlyArray<UpdateFileChunk>
}
export interface UpdateFileChunk {
readonly oldLines: ReadonlyArray<string>
readonly newLines: ReadonlyArray<string>
readonly changeContext?: string
readonly endOfFile?: boolean
}
export interface FileUpdate {
readonly content: string
readonly bom: boolean
}
export function parse(patchText: string): Result.Result<ReadonlyArray<Hunk>, ParseError> {
const lines = stripHeredoc(patchText.trim())
.split("\n")
.map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line))
const begin = lines[0]?.trim() === "*** Begin Patch" ? 0 : -1
const end = lines.at(-1)?.trim() === "*** End Patch" ? lines.length - 1 : -1
if (begin === -1) return Result.fail(new BoundaryError({ boundary: "first" }))
if (end === -1 || begin >= end) return Result.fail(new BoundaryError({ boundary: "last" }))
const hunks: Hunk[] = []
let index = begin + 1
while (index < end) {
const line = lines[index]!
const header = line.trim()
if (
index === begin + 1 &&
header.startsWith("*** Environment ID:") &&
header.slice("*** Environment ID:".length).trim()
) {
index++
continue
}
if (header.startsWith("*** Add File: ")) {
const path = header.slice("*** Add File: ".length).trim()
const parsed = parseAdd(lines, index + 1, end, path)
if ("error" in parsed) return Result.fail(parsed.error)
hunks.push({ type: "add", path, contents: parsed.content })
index = parsed.next
continue
}
if (header.startsWith("*** Delete File: ")) {
const path = header.slice("*** Delete File: ".length).trim()
const next = lines[index + 1]?.trim()
if (index + 1 < end && next !== undefined && !isBoundary(next)) {
if (next.startsWith("*** ")) {
return Result.fail(new InvalidHunkError({ line: next, lineNumber: index + 2 }))
}
return Result.fail(
new InvalidHunkError({
line: next,
lineNumber: index + 2,
reason: `Unexpected line after Delete File '${path}': '${next}'. Delete hunks do not contain body lines`,
}),
)
}
hunks.push({ type: "delete", path })
index++
continue
}
if (header.startsWith("*** Update File: ")) {
const path = header.slice("*** Update File: ".length).trim()
let next = index + 1
let movePath: string | undefined
while (lines[next]?.trimEnd() === "*** End of File") next++
const move = lines[next]?.trimEnd()
if (move === "*** Move to:" || move?.startsWith("*** Move to: ")) {
movePath = move.slice("*** Move to: ".length).trim()
if (!movePath) {
return Result.fail(
new InvalidHunkError({
line: lines[next]!.trim(),
lineNumber: next + 1,
reason: `Move destination for '${path}' must not be empty`,
}),
)
}
next++
}
const parsed = parseUpdate(lines, next, end, path, index)
if ("error" in parsed) return Result.fail(parsed.error)
hunks.push({ type: "update", path, movePath, chunks: parsed.chunks })
index = parsed.next
continue
}
return Result.fail(new InvalidHunkError({ line: header, lineNumber: index + 1 }))
}
return Result.succeed(hunks)
}
export function derive(path: string, chunks: ReadonlyArray<UpdateFileChunk>, original: string): FileUpdate {
const source = Bom.split(original)
const lines = source.text.split("\n")
if (lines.at(-1) === "") lines.pop()
const replacements = computeReplacements(lines, path, chunks)
for (const [start, remove, insert] of replacements.reverse()) lines.splice(start, remove, ...insert)
if (lines.at(-1) !== "") lines.push("")
const next = Bom.split(lines.join("\n"))
return { content: next.text, bom: source.bom || next.bom }
}
export function joinBom(text: string, bom: boolean) {
return Bom.join(text, bom)
}
function parseAdd(
lines: ReadonlyArray<string>,
start: number,
end: number,
path: string,
): { content: string; next: number } | { error: InvalidHunkError } {
const content: string[] = []
let index = start
while (index < end && !isBoundary(lines[index]!.trim())) {
if (!lines[index]!.startsWith("+")) {
const line = lines[index]!.trim()
return {
error: new InvalidHunkError({
line,
lineNumber: index + 1,
reason: `Invalid Add File line for '${path}': expected a line starting with '+', got '${line}'`,
}),
}
}
// Every + line is a whole line of the file, as in Codex, so a final empty + line is a blank last line.
content.push(`${lines[index]!.slice(1)}\n`)
index++
}
return { content: content.join(""), next: index }
}
function parseUpdate(
lines: ReadonlyArray<string>,
start: number,
end: number,
path: string,
hunk: number,
): { chunks: ReadonlyArray<UpdateFileChunk>; next: number } | { error: InvalidHunkError } {
const chunks: Array<{
oldLines: string[]
newLines: string[]
changeContext?: string
endOfFile?: boolean
}> = []
let index = start
let afterEndOfFile = false
while (index < end) {
const line = lines[index]!
const updateLine = line.trimEnd()
if (afterEndOfFile) {
if (updateLine === "") {
index++
continue
}
if (updateLine === "@@" || updateLine.startsWith("@@ ")) afterEndOfFile = false
else if (isBoundary(updateLine)) break
else {
return {
error: new InvalidHunkError({
line,
lineNumber: index + 1,
reason: `Expected update hunk to start with a @@ context marker, got: '${line}'`,
}),
}
}
}
if (updateLine === "*** End of File") {
const chunk = chunks.at(-1)
if (chunk && chunk.oldLines.length === 0 && chunk.newLines.length === 0) {
return {
error: new InvalidHunkError({
line: updateLine,
lineNumber: index + 1,
reason: "Update hunk does not contain any lines",
}),
}
}
if (chunk) {
chunk.endOfFile = true
afterEndOfFile = true
}
index++
continue
}
if (isBoundary(updateLine)) break
if (updateLine === "@@" || updateLine.startsWith("@@ ")) {
const previous = chunks.at(-1)
if (previous && previous.oldLines.length === 0 && previous.newLines.length === 0) {
return {
error: new InvalidHunkError({
line,
lineNumber: index + 1,
reason: `Unexpected line found in update hunk: '${line}'. Every line should start with ' ' (context line), '+' (added line), or '-' (removed line)`,
}),
}
}
chunks.push({
oldLines: [],
newLines: [],
changeContext: updateLine === "@@" ? undefined : updateLine.slice("@@ ".length),
})
index++
continue
}
if (chunks.length === 0) chunks.push({ oldLines: [], newLines: [] })
const chunk = chunks.at(-1)!
if (line === "") {
chunk.oldLines.push("")
chunk.newLines.push("")
index++
continue
}
if (line.startsWith(" ")) {
chunk.oldLines.push(line.slice(1))
chunk.newLines.push(line.slice(1))
index++
continue
}
if (line.startsWith("-")) {
chunk.oldLines.push(line.slice(1))
index++
continue
}
if (line.startsWith("+")) {
chunk.newLines.push(line.slice(1))
index++
continue
}
const populated = chunk.oldLines.length > 0 || chunk.newLines.length > 0
return {
error: new InvalidHunkError({
line,
lineNumber: index + 1,
reason: populated
? `Expected update hunk to start with a @@ context marker, got: '${line}'`
: `Unexpected line found in update hunk: '${line}'. Every line should start with ' ' (context line), '+' (added line), or '-' (removed line)`,
}),
}
}
if (chunks.length === 0) {
return {
error: new InvalidHunkError({
line: lines[hunk]!.trim(),
lineNumber: hunk + 1,
reason: `Update file hunk for path '${path}' is empty`,
}),
}
}
const last = chunks.at(-1)!
if (last.oldLines.length === 0 && last.newLines.length === 0) {
const line = lines[index]!.trim()
return {
error: new InvalidHunkError({
line,
lineNumber: index + 1,
reason:
line === "*** End Patch"
? "Update hunk does not contain any lines"
: `Unexpected line found in update hunk: '${line}'. Every line should start with ' ' (context line), '+' (added line), or '-' (removed line)`,
}),
}
}
return { chunks, next: index }
}
function isBoundary(line: string) {
return (
line === "*** End Patch" ||
line.startsWith("*** Add File: ") ||
line.startsWith("*** Delete File: ") ||
line.startsWith("*** Update File: ")
)
}
function computeReplacements(lines: ReadonlyArray<string>, path: string, chunks: ReadonlyArray<UpdateFileChunk>) {
const replacements: Array<readonly [start: number, remove: number, insert: ReadonlyArray<string>]> = []
let lineIndex = 0
for (const chunk of chunks) {
if (chunk.changeContext) {
const context = seek(lines, [chunk.changeContext], lineIndex)
if (context === -1) throw new Error(`Failed to find context '${chunk.changeContext}' in ${path}`)
lineIndex = context + 1
}
if (chunk.oldLines.length === 0) {
replacements.push([lines.length, 0, chunk.newLines])
continue
}
let oldLines = chunk.oldLines
let newLines = chunk.newLines
let found = seek(lines, oldLines, lineIndex, chunk.endOfFile)
if (found === -1 && oldLines.at(-1) === "") {
oldLines = oldLines.slice(0, -1)
if (newLines.at(-1) === "") newLines = newLines.slice(0, -1)
found = seek(lines, oldLines, lineIndex, chunk.endOfFile)
}
if (found === -1 && chunk.oldLines.every((line) => line === "")) {
const expected =
chunk.oldLines.length === 1 ? "an expected blank line" : `${chunk.oldLines.length} consecutive blank lines`
throw new Error(`Failed to find ${expected} in ${path}`)
}
if (found === -1) throw new Error(`Failed to find expected lines in ${path}:\n${chunk.oldLines.join("\n")}`)
replacements.push([found, oldLines.length, newLines])
lineIndex = found + oldLines.length
}
return replacements.sort((left, right) => left[0] - right[0])
}
function seek(lines: ReadonlyArray<string>, pattern: ReadonlyArray<string>, start: number, eof = false) {
if (pattern.length === 0) return -1
if (eof) {
const offset = lines.length - pattern.length
if (offset < start) return -1
for (const compare of [exact, rstrip, trim, normalized]) {
if (matches(lines, pattern, offset, compare)) return offset
}
return -1
}
for (const compare of [exact, rstrip, trim, normalized]) {
for (let offset = start; offset <= lines.length - pattern.length; offset++) {
if (matches(lines, pattern, offset, compare)) return offset
}
}
return -1
}
function matches(
lines: ReadonlyArray<string>,
pattern: ReadonlyArray<string>,
offset: number,
compare: (left: string, right: string) => boolean,
) {
return pattern.every((line, index) => compare(lines[offset + index]!, line))
}
const exact = (left: string, right: string) => left === right
const rstrip = (left: string, right: string) => left.trimEnd() === right.trimEnd()
const trim = (left: string, right: string) => left.trim() === right.trim()
const normalized = (left: string, right: string) => normalize(left.trim()) === normalize(right.trim())
const normalize = (value: string) =>
value
.replace(/[‘’‚‛]/g, "'")
.replace(/[“”„‟]/g, '"')
.replace(/[‐‑‒–—―−]/g, "-")
.replace(/[\u00A0\u2002-\u200A\u202F\u205F\u3000]/g, " ")
const stripHeredoc = (input: string) => input.match(/^(?:cat\s+)?<<(['"]?)(\w+)\1\s*\n([\s\S]*?)\n\2\s*$/)?.[3] ?? input