* Parse raw unified diff output into the structured ToolUseDiff format. * Extracts only the hunk content (starting from @@) as the patch, * and counts additions/deletions.
( filename: string, rawDiff: string, status: 'modified' | 'added', )
| 446 | * and counts additions/deletions. |
| 447 | */ |
| 448 | function parseRawDiffToToolUseDiff( |
| 449 | filename: string, |
| 450 | rawDiff: string, |
| 451 | status: 'modified' | 'added', |
| 452 | ): Omit<ToolUseDiff, 'repository'> { |
| 453 | const lines = rawDiff.split('\n') |
| 454 | const patchLines: string[] = [] |
| 455 | let inHunks = false |
| 456 | let additions = 0 |
| 457 | let deletions = 0 |
| 458 | |
| 459 | for (const line of lines) { |
| 460 | if (line.startsWith('@@')) { |
| 461 | inHunks = true |
| 462 | } |
| 463 | if (inHunks) { |
| 464 | patchLines.push(line) |
| 465 | if (line.startsWith('+') && !line.startsWith('+++')) { |
| 466 | additions++ |
| 467 | } else if (line.startsWith('-') && !line.startsWith('---')) { |
| 468 | deletions++ |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return { |
| 474 | filename, |
| 475 | status, |
| 476 | additions, |
| 477 | deletions, |
| 478 | changes: additions + deletions, |
| 479 | patch: patchLines.join('\n'), |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Determine the best ref to diff against for a PR-like diff. |
no test coverage detected