|
28 | 28 | import textwrap
|
29 | 29 | from collections.abc import Iterator
|
30 | 30 | from itertools import chain
|
| 31 | +from pathlib import Path |
31 | 32 |
|
32 | 33 | import pytest
|
33 | 34 |
|
@@ -459,3 +460,33 @@ def test_diff_blobs(emptyrepo: Repository) -> None:
|
459 | 460 | assert diff_one_context_line.text == PATCH_BLOBS_ONE_CONTEXT_LINE
|
460 | 461 | diff_all_together = repo.diff(blob1, blob2, context_lines=1, interhunk_lines=1)
|
461 | 462 | assert diff_all_together.text == PATCH_BLOBS_DEFAULT
|
| 463 | + |
| 464 | + |
| 465 | +def test_diff_unchanged_file_no_patch(testrepo) -> None: |
| 466 | + repo = testrepo |
| 467 | + |
| 468 | + # Convert hello.txt line endings to CRLF |
| 469 | + path = Path(repo.workdir) / 'hello.txt' |
| 470 | + data = path.read_bytes() |
| 471 | + data = data.replace(b'\n', b'\r\n') |
| 472 | + path.write_bytes(data) |
| 473 | + |
| 474 | + # Enable CRLF filter |
| 475 | + repo.config['core.autocrlf'] = 'input' |
| 476 | + |
| 477 | + diff = repo.diff() |
| 478 | + assert len(diff) == 1 |
| 479 | + |
| 480 | + # Get patch #0 in the same diff several times. |
| 481 | + # git_patch_from_diff eventually decides that the file is "unchanged"; |
| 482 | + # it returns a NULL patch in this case. |
| 483 | + # https://libgit2.org/docs/reference/main/patch/git_patch_from_diff |
| 484 | + for i in range(10): # loop typically exits in the third iteration |
| 485 | + patch = diff[0] |
| 486 | + if patch is None: # libgit2 decides the file is unchanged |
| 487 | + break |
| 488 | + assert patch.delta.new_file.path == path.name |
| 489 | + assert patch.text == '' # no content change (just line endings) |
| 490 | + else: |
| 491 | + # Didn't find the edge case that this test is supposed to exercise. |
| 492 | + assert False, 'libgit2 rebuilt a new patch every time' |
0 commit comments