MCPcopy Create free account
hub / github.com/arctic-cli/interface / parsePatch

Function parsePatch

packages/arctic/src/patch/index.ts:180–235  ·  view source on GitHub ↗
(patchText: string)

Source from the content-addressed store, hash-verified

178 }
179
180 export function parsePatch(patchText: string): { hunks: Hunk[] } {
181 const lines = patchText.split("\n")
182 const hunks: Hunk[] = []
183 let i = 0
184
185 // Look for Begin/End patch markers
186 const beginMarker = "*** Begin Patch"
187 const endMarker = "*** End Patch"
188
189 const beginIdx = lines.findIndex((line) => line.trim() === beginMarker)
190 const endIdx = lines.findIndex((line) => line.trim() === endMarker)
191
192 if (beginIdx === -1 || endIdx === -1 || beginIdx >= endIdx) {
193 throw new Error("Invalid patch format: missing Begin/End markers")
194 }
195
196 // Parse content between markers
197 i = beginIdx + 1
198
199 while (i < endIdx) {
200 const header = parsePatchHeader(lines, i)
201 if (!header) {
202 i++
203 continue
204 }
205
206 if (lines[i].startsWith("*** Add File:")) {
207 const { content, nextIdx } = parseAddFileContent(lines, header.nextIdx)
208 hunks.push({
209 type: "add",
210 path: header.filePath,
211 contents: content,
212 })
213 i = nextIdx
214 } else if (lines[i].startsWith("*** Delete File:")) {
215 hunks.push({
216 type: "delete",
217 path: header.filePath,
218 })
219 i = header.nextIdx
220 } else if (lines[i].startsWith("*** Update File:")) {
221 const { chunks, nextIdx } = parseUpdateFileChunks(lines, header.nextIdx)
222 hunks.push({
223 type: "update",
224 path: header.filePath,
225 move_path: header.movePath,
226 chunks,
227 })
228 i = nextIdx
229 } else {
230 i++
231 }
232 }
233
234 return { hunks }
235 }
236
237 // Apply patch functionality

Callers 4

SessionFunction · 0.85
maybeParseApplyPatchFunction · 0.85
applyPatchFunction · 0.85

Calls 4

parsePatchHeaderFunction · 0.85
parseAddFileContentFunction · 0.85
parseUpdateFileChunksFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected