* Parses a unified diff string into an array of hunks. * It skips the file header lines (starting with "---" or "+++") and hunk header lines (starting with "@@"), * then collects the remaining lines (which may start with '+' or '-' or have no prefix).
(diffText: string)
| 102 | * then collects the remaining lines (which may start with '+' or '-' or have no prefix). |
| 103 | */ |
| 104 | function parseUnifiedDiff(diffText: string): Hunk[] { |
| 105 | const lines = diffText.split(/\r?\n/); |
| 106 | const hunks: Hunk[] = []; |
| 107 | let currentHunk: Hunk | null = null; |
| 108 | |
| 109 | for (const line of lines) { |
| 110 | if (line.startsWith("---") || line.startsWith("+++")) { |
| 111 | // Skip file header lines. |
| 112 | continue; |
| 113 | } |
| 114 | if (line.startsWith("@@")) { |
| 115 | if (currentHunk) { |
| 116 | hunks.push(currentHunk); |
| 117 | } |
| 118 | currentHunk = { lines: [] }; |
| 119 | continue; |
| 120 | } |
| 121 | currentHunk?.lines.push(line); |
| 122 | } |
| 123 | if (currentHunk) { |
| 124 | hunks.push(currentHunk); |
| 125 | } |
| 126 | return hunks; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Searches for an occurrence of the block of lines (the “before” block) in sourceLines, |
no test coverage detected