ggshield output handlers currently do not work with multi-parent diffs, so convert them into single-parent diffs
(content: str)
| 314 | |
| 315 | |
| 316 | def convert_multi_parent_diff(content: str) -> str: |
| 317 | """ |
| 318 | ggshield output handlers currently do not work with multi-parent diffs, so convert |
| 319 | them into single-parent diffs |
| 320 | """ |
| 321 | lines = content.splitlines() |
| 322 | |
| 323 | # Process header |
| 324 | hunk_header, parent_count = process_multi_parent_hunk_header(lines.pop(0)) |
| 325 | out_lines = [hunk_header] |
| 326 | |
| 327 | # Process content |
| 328 | for line in lines: |
| 329 | columns = line[:parent_count] |
| 330 | text = line[parent_count:] |
| 331 | if columns.startswith("-"): |
| 332 | # Removed from first parent, keep it |
| 333 | text = f"-{text}" |
| 334 | elif columns.startswith("+"): |
| 335 | # Added by first parent, keep it |
| 336 | text = f"+{text}" |
| 337 | elif "+" in columns: |
| 338 | # Added by another parent, keep it but consider it unchanged |
| 339 | text = f" {text}" |
| 340 | elif "-" in columns: |
| 341 | # Removed from another parent, skip it |
| 342 | continue |
| 343 | else: |
| 344 | # Unchanged |
| 345 | text = f" {text}" |
| 346 | out_lines.append(text) |
| 347 | |
| 348 | return "\n".join(out_lines) |
| 349 | |
| 350 | |
| 351 | def process_multi_parent_hunk_header(header: str) -> Tuple[str, int]: |