MCPcopy Create free account
hub / github.com/anomalyco/opencode / parse

Function parse

packages/core/src/patch.ts:25–69  ·  view source on GitHub ↗
(patchText: string)

Source from the content-addressed store, hash-verified

23}
24
25export function parse(patchText: string): ReadonlyArray<Hunk> {
26 const lines = stripHeredoc(patchText.trim()).split("\n")
27 const begin = lines.findIndex((line) => line.trim() === "*** Begin Patch")
28 const end = lines.findIndex((line) => line.trim() === "*** End Patch")
29 if (begin === -1 || end === -1 || begin >= end) throw new Error("Invalid patch format: missing Begin/End markers")
30
31 const hunks: Hunk[] = []
32 let index = begin + 1
33 while (index < end) {
34 const line = lines[index]!
35 if (line.startsWith("*** Add File:")) {
36 const path = line.slice("*** Add File:".length).trim()
37 if (!path) throw new Error("Invalid add file path")
38 const parsed = parseAdd(lines, index + 1)
39 hunks.push({ type: "add", path, contents: parsed.content })
40 index = parsed.next
41 continue
42 }
43 if (line.startsWith("*** Delete File:")) {
44 const path = line.slice("*** Delete File:".length).trim()
45 if (!path) throw new Error("Invalid delete file path")
46 hunks.push({ type: "delete", path })
47 index++
48 continue
49 }
50 if (line.startsWith("*** Update File:")) {
51 const path = line.slice("*** Update File:".length).trim()
52 if (!path) throw new Error("Invalid update file path")
53 let next = index + 1
54 let movePath: string | undefined
55 if (lines[next]?.startsWith("*** Move to:")) {
56 movePath = lines[next]!.slice("*** Move to:".length).trim()
57 if (!movePath) throw new Error("Invalid move file path")
58 next++
59 }
60 const parsed = parseUpdate(lines, next)
61 if (parsed.chunks.length === 0) throw new Error(`Invalid update hunk for ${path}: expected at least one @@ chunk`)
62 hunks.push({ type: "update", path, movePath, chunks: parsed.chunks })
63 index = parsed.next
64 continue
65 }
66 throw new Error(`Invalid patch line: ${line}`)
67 }
68 return hunks
69}
70
71export function derive(path: string, chunks: ReadonlyArray<UpdateFileChunk>, original: string): FileUpdate {
72 const source = splitBom(original)

Callers 1

config.tsFile · 0.70

Calls 4

parseAddFunction · 0.85
parseUpdateFunction · 0.85
pushMethod · 0.80
stripHeredocFunction · 0.70

Tested by

no test coverage detected