Repository navigation
Commit c7ce984
committed
fix(diff): read patch header paths the way git apply does
`Diff._index_from_patch_format` takes a file's paths from the
`diff --git a/<path> b/<path>` line whenever a patch header has no
`---` and `+++` lines, which is the case for binary files, empty files
and mode-only changes. It also did so for both names of a copy, because
the `copy from` line was not captured and the `copy to` line only set
`copied_file`. `Diff.re_header` splits that line at the first space
followed by `a/` or `b/`, but git does not quote a path for containing
a space, so a path containing ` a/` or ` b/` is split in the wrong
place:
- A binary file `assets b/logo.png` is reported with `a_path` `assets`
and `b_path` `logo.png b/assets b/logo.png`; an empty file
`docs a/empty` comes back as `docs` and `empty b/docs a/empty`.
- A copy of `assets b/logo.png` to `copy b/logo.png` comes back as
`assets` and `logo.png b/copy b/logo.png`.
- For a quoted name such as `"a/quoted\t a/tab"`, the first name is cut
at the space inside the quotes, `decode_path` fails its prefix
assertion, and the whole `diff(create_patch=True)` call raises
`AssertionError`.
Paths are chosen by whoever authored the commit. A tool that uses
`Commit.diff(create_patch=True)` to learn which files a commit touches
is shown an `a_path` prefix of the author's choosing, does not see the
real path as `b_path`, or gets no diff at all.
`re_header` now captures the `copy from` name, and the copy lines are
used for the paths the way the rename lines already are. When no `---`,
`+++`, rename or copy line names the file, both names on the
`diff --git` line are the same path, and `_same_path_in_header` splits
the line the way `git_header_name` in git's `apply.c` does: at the end
of a quoted first name, or at the space where the two halves agree. A
line that does not split that way keeps the previous result, so the
existing fixtures parse as before.
The new test in `test/test_diff.py` fails before the change and passes
after it. `ruff check`, `ruff format --check` and `mypy` are clean.
In a full clone the suite has 3 failures (`test_diff_with_staged_file`,
`test_empty_repo` and `test_rebasing`), and all 3 also fail without
this change.1 parent 9d3aff0 commit c7ce984
2 files changed
Lines changed: 83 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
410 | 410 | | |
411 | 411 | | |
412 | 412 | | |
413 | | - | |
| 413 | + | |
| 414 | + | |
414 | 415 | | |
415 | 416 | | |
416 | 417 | | |
| |||
419 | 420 | | |
420 | 421 | | |
421 | 422 | | |
422 | | - | |
| 423 | + | |
423 | 424 | | |
424 | 425 | | |
425 | 426 | | |
| |||
626 | 627 | | |
627 | 628 | | |
628 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
629 | 663 | | |
630 | 664 | | |
631 | 665 | | |
| |||
662 | 696 | | |
663 | 697 | | |
664 | 698 | | |
| 699 | + | |
665 | 700 | | |
666 | 701 | | |
667 | 702 | | |
| |||
670 | 705 | | |
671 | 706 | | |
672 | 707 | | |
| 708 | + | |
673 | 709 | | |
674 | 710 | | |
675 | 711 | | |
| |||
684 | 720 | | |
685 | 721 | | |
686 | 722 | | |
687 | | - | |
688 | | - | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
689 | 732 | | |
690 | 733 | | |
691 | 734 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
273 | 309 | | |
274 | 310 | | |
275 | 311 | | |
| |||
0 commit comments