Skip to content

Commit 27c8c82

Browse files
Record the header-prefix fix in the changelog
Document that diff.noprefix, diff.srcPrefix and diff.dstPrefix no longer make Diffable.diff(..., create_patch=True) return an empty DiffIndex.
1 parent 1c9ef7d commit 27c8c82

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎doc/source/changes.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ API changes
8888
undecodable bytes. Diff blob paths can be used for tree lookup, and renames
8989
between distinct byte names are detected correctly. Patch rename fields
9090
contain unquoted filename bytes.
91+
* Patch diffs request the ``a/`` and ``b/`` header prefixes explicitly, so
92+
``diff.noprefix``, ``diff.srcPrefix`` and ``diff.dstPrefix`` no longer make
93+
``Diffable.diff(..., create_patch=True)`` return an empty ``DiffIndex`` for
94+
index and working-tree changes.
9195
* Tree serialization now delegates construction to Git or the supported Gix
9296
backend. ``Tree.cache`` and ``TreeModifier`` edit one tree's in-memory entries;
9397
``set_done()`` orders entries by filename bytes, including undecodable names.

‎git/diff.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ def diff(
281281
if create_patch:
282282
args.append("-p")
283283
args.append("--no-ext-diff")
284+
# The patch parser only recognizes "a/"/"b/"-prefixed header names, and
285+
# `git diff` (unlike `git diff-tree`) lets configuration such as
286+
# diff.noprefix, diff.srcPrefix or diff.dstPrefix replace or drop them,
287+
# which would otherwise yield an empty DiffIndex for a changed worktree.
288+
args.append("--src-prefix=a/")
289+
args.append("--dst-prefix=b/")
284290
else:
285291
args.append("--raw")
286292
args.append("-z")

‎test/test_diff.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import ddt
99
import pytest
1010

11-
from git import NULL_TREE, Diff, DiffIndex, Diffable, Repo
11+
from git import NULL_TREE, Actor, Diff, DiffIndex, Diffable, Repo
1212
from git.compat import safe_encode
1313
from git.diff import decode_path
1414
from git.exc import UnsafeOptionError
@@ -660,3 +660,26 @@ def test_beginning_space(self):
660660
b_path = d.b_path
661661
self.assertEqual(a_path, " file.txt")
662662
self.assertEqual(b_path, " file.txt")
663+
664+
665+
@pytest.mark.parametrize(
666+
("option", "value"),
667+
[("diff.noprefix", "true"), ("diff.srcPrefix", "x/"), ("diff.dstPrefix", "y/")],
668+
)
669+
def test_worktree_patch_diff_ignores_prefix_configuration(tmp_path, option, value):
670+
author = Actor("Test Author", "author@example.invalid")
671+
with Repo.init(tmp_path / "repo") as repo:
672+
repo.git.config(option, value)
673+
tracked = tmp_path / "repo" / "file.txt"
674+
tracked.write_text("before\n")
675+
repo.index.add(["file.txt"])
676+
repo.index.commit("initial", author=author, committer=author)
677+
tracked.write_text("after\n")
678+
679+
# The raw format reports the change regardless of the header prefixes.
680+
assert len(repo.index.diff(None)) == 1
681+
682+
# Patch headers must keep the a/ and b/ names the parser recognizes.
683+
(diff,) = repo.index.diff(None, create_patch=True)
684+
assert diff.a_path == "file.txt"
685+
assert diff.diff == b"@@ -1 +1 @@\n-before\n+after\n"

0 commit comments

Comments
 (0)