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

Function applyHunksToFiles

packages/arctic/src/patch/index.ts:460–514  ·  view source on GitHub ↗
(hunks: Hunk[])

Source from the content-addressed store, hash-verified

458
459 // Apply hunks to filesystem
460 export async function applyHunksToFiles(hunks: Hunk[]): Promise<AffectedPaths> {
461 if (hunks.length === 0) {
462 throw new Error("No files were modified.")
463 }
464
465 const added: string[] = []
466 const modified: string[] = []
467 const deleted: string[] = []
468
469 for (const hunk of hunks) {
470 switch (hunk.type) {
471 case "add":
472 // Create parent directories
473 const addDir = path.dirname(hunk.path)
474 if (addDir !== "." && addDir !== "/") {
475 await fs.mkdir(addDir, { recursive: true })
476 }
477
478 await fs.writeFile(hunk.path, hunk.contents, "utf-8")
479 added.push(hunk.path)
480 log.info(`Added file: ${hunk.path}`)
481 break
482
483 case "delete":
484 await fs.unlink(hunk.path)
485 deleted.push(hunk.path)
486 log.info(`Deleted file: ${hunk.path}`)
487 break
488
489 case "update":
490 const fileUpdate = deriveNewContentsFromChunks(hunk.path, hunk.chunks)
491
492 if (hunk.move_path) {
493 // Handle file move
494 const moveDir = path.dirname(hunk.move_path)
495 if (moveDir !== "." && moveDir !== "/") {
496 await fs.mkdir(moveDir, { recursive: true })
497 }
498
499 await fs.writeFile(hunk.move_path, fileUpdate.content, "utf-8")
500 await fs.unlink(hunk.path)
501 modified.push(hunk.move_path)
502 log.info(`Moved file: ${hunk.path} -> ${hunk.move_path}`)
503 } else {
504 // Regular update
505 await fs.writeFile(hunk.path, fileUpdate.content, "utf-8")
506 modified.push(hunk.path)
507 log.info(`Updated file: ${hunk.path}`)
508 }
509 break
510 }
511 }
512
513 return { added, modified, deleted }
514 }
515
516 // Main patch application function
517 export async function applyPatch(patchText: string): Promise<AffectedPaths> {

Callers 1

applyPatchFunction · 0.85

Calls 2

pushMethod · 0.80

Tested by

no test coverage detected