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

Function parseUpdate

packages/core/src/patch.ts:99–130  ·  view source on GitHub ↗
(lines: ReadonlyArray<string>, start: number)

Source from the content-addressed store, hash-verified

97}
98
99function parseUpdate(lines: ReadonlyArray<string>, start: number) {
100 const chunks: UpdateFileChunk[] = []
101 let index = start
102 while (index < lines.length && !lines[index]!.startsWith("***")) {
103 if (!lines[index]!.startsWith("@@")) {
104 throw new Error(`Invalid update file line: ${lines[index]}`)
105 }
106 const changeContext = lines[index]!.slice(2).trim() || undefined
107 const oldLines: string[] = []
108 const newLines: string[] = []
109 let endOfFile = false
110 index++
111 while (index < lines.length && !lines[index]!.startsWith("@@")) {
112 const line = lines[index]!
113 if (line === "*** End of File") {
114 endOfFile = true
115 index++
116 break
117 }
118 if (line.startsWith("***")) break
119 if (line.startsWith(" ")) {
120 oldLines.push(line.slice(1))
121 newLines.push(line.slice(1))
122 } else if (line.startsWith("-")) oldLines.push(line.slice(1))
123 else if (line.startsWith("+")) newLines.push(line.slice(1))
124 else throw new Error(`Invalid update chunk line: ${line}`)
125 index++
126 }
127 chunks.push({ oldLines, newLines, changeContext, endOfFile: endOfFile || undefined })
128 }
129 return { chunks, next: index }
130}
131
132function computeReplacements(lines: ReadonlyArray<string>, path: string, chunks: ReadonlyArray<UpdateFileChunk>) {
133 const replacements: Array<readonly [start: number, remove: number, insert: ReadonlyArray<string>]> = []

Callers 1

parseFunction · 0.85

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected