Parse unified diff output into file -> line-range mappings. Handles the ``@@ -old,count +new,count @@`` hunk header format.
(diff_text: str)
| 134 | |
| 135 | |
| 136 | def _parse_unified_diff(diff_text: str) -> dict[str, list[tuple[int, int]]]: |
| 137 | """Parse unified diff output into file -> line-range mappings. |
| 138 | |
| 139 | Handles the ``@@ -old,count +new,count @@`` hunk header format. |
| 140 | """ |
| 141 | ranges: dict[str, list[tuple[int, int]]] = {} |
| 142 | current_file: str | None = None |
| 143 | |
| 144 | # Match "+++ b/path/to/file" |
| 145 | file_pattern = re.compile(r"^\+\+\+ b/(.+)$") |
| 146 | # Match "@@ ... +start,count @@" or "@@ ... +start @@" |
| 147 | hunk_pattern = re.compile(r"^@@ .+? \+(\d+)(?:,(\d+))? @@") |
| 148 | |
| 149 | for line in diff_text.splitlines(): |
| 150 | file_match = file_pattern.match(line) |
| 151 | if file_match: |
| 152 | current_file = file_match.group(1) |
| 153 | continue |
| 154 | |
| 155 | hunk_match = hunk_pattern.match(line) |
| 156 | if hunk_match and current_file is not None: |
| 157 | start = int(hunk_match.group(1)) |
| 158 | count = int(hunk_match.group(2)) if hunk_match.group(2) else 1 |
| 159 | if count == 0: |
| 160 | # Pure deletion hunk (no lines added); still note the position. |
| 161 | end = start |
| 162 | else: |
| 163 | end = start + count - 1 |
| 164 | ranges.setdefault(current_file, []).append((start, end)) |
| 165 | |
| 166 | return ranges |
| 167 | |
| 168 | |
| 169 | # --------------------------------------------------------------------------- |
no outgoing calls