Skip to content

Commit db24842

Browse files
fix(diff): request a/ and b/ header prefixes for patch diffs
With diff.noprefix, diff.srcPrefix or diff.dstPrefix set, git diff emits a patch header without the a/ and b/ prefixes, which Diff.re_header and decode_path both require. The result is an empty DiffIndex, with no error, for index and worktree changes; the diff-tree path is unaffected, so the two documented shapes disagree. The same function already injects -c diff.mnemonicPrefix=false four lines below, because that key rewrites the prefixes the same way. Rather than adding three more config overrides, request the prefixes on the command line, which takes precedence over all four keys and needs no change to the _config allowlist in git/cmd.py.
1 parent 1c9ef7d commit db24842

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎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)