(hunks: Hunk[])
| 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> { |
no test coverage detected