Apply a V4A diff to the provided text. This parser understands both the create-file syntax (only "+" prefixed lines) and the default update syntax that includes context hunks.
(input: str, diff: str, mode: ApplyDiffMode = "default")
| 50 | |
| 51 | |
| 52 | def apply_diff(input: str, diff: str, mode: ApplyDiffMode = "default") -> str: |
| 53 | """Apply a V4A diff to the provided text. |
| 54 | |
| 55 | This parser understands both the create-file syntax (only "+" prefixed |
| 56 | lines) and the default update syntax that includes context hunks. |
| 57 | """ |
| 58 | newline = _detect_newline(input, diff, mode) |
| 59 | diff_lines = _normalize_diff_lines(diff) |
| 60 | if mode == "create": |
| 61 | return _parse_create_diff(diff_lines, newline=newline) |
| 62 | |
| 63 | normalized_input = _normalize_text_newlines(input) |
| 64 | parsed = _parse_update_diff(diff_lines, normalized_input) |
| 65 | return _apply_chunks(normalized_input, parsed.chunks, newline=newline) |
| 66 | |
| 67 | |
| 68 | def _normalize_diff_lines(diff: str) -> list[str]: |