Skip to content

fix(diff): request a/ and b/ header prefixes for patch diffs - #2371

Closed
qianeric-backup wants to merge 1 commit into
gitpython-developers:mainfrom
qianeric-backup:diff-patch-header-prefixes
Closed

qianeric-backup wants to merge 1 commit into
gitpython-developers:mainfrom
qianeric-backup:diff-patch-header-prefixes

Conversation

@qianeric-backup

Copy link
Copy Markdown

With diff.noprefix=true in the repository config, create_patch=True returns nothing at all for index and worktree changes:

$ git config diff.noprefix true
$ python -c "..."
repo.index.diff(None,          create_patch=False)  -> 2 entries
repo.index.diff(None,          create_patch=True)   -> 0 entries
repo.commit().diff(INDEX,      create_patch=True)   -> 0 entries
repo.commit().diff(None,       create_patch=True)   -> 0 entries

No exception, no warning — an empty DiffIndex for a worktree that has changes. git diff-tree (the commit-to-commit path) is unaffected, so the two documented shapes disagree with each other.

The cause is the header the patch parser requires. diff.noprefix makes git diff emit diff --git foo.txt foo.txt, and Diff.re_header only accepts a/ and b/:

    re_header = re.compile(
        rb"^diff --git " + _header_path_re + b" " + _header_path_re + b"$",
        ...
    )

decode_path states the same requirement directly: assert path.startswith(b"a/") or path.startswith(b"b/").

Four lines below the affected branch, the same function already deals with exactly this class of problem for a different key:

            proc = self.repo.git._call_process_safe(
                diff_cmd, *args,
                _config=["diff.mnemonicPrefix=false"], **kwargs,
            )

diff.mnemonicPrefix rewrites the prefixes to i/, w/, c/, o/ — same breakage, handled. diff.noprefix, diff.srcPrefix and diff.dstPrefix were not, and none of them is in the _config allowlist in git/cmd.py, so they cannot be passed that way.

Changes

Pass the prefixes on the command line rather than adding three more -c overrides:

            args.append("--src-prefix=a/")
            args.append("--dst-prefix=b/")

Command-line options take precedence over all four config keys, and this needs no change to the _config allowlist. A caller who asks for something else explicitly (no_prefix=True, src_prefix=...) still gets it, because those are appended after and win.

doc/source/changes.rst entry.

Verification

Three parametrized cases, one per config key (diff.noprefix, diff.srcPrefix, diff.dstPrefix), each run against all three backends (default, GIT_PYTHON_BACKEND=cli, GIT_PYTHON_BACKEND=gix):

tree test/test_diff.py
main 52 passed
main + new tests, git/diff.py unchanged 3 failed, 52 passed
with this patch 55 passed

The failures are ValueError: not enough values to unpack (expected 1, got 0) — the empty DiffIndex. All three backends fail identically; the gix backend raises _Unsupported for patch=True and always falls back to CLI parsing, so the defect is backend-independent.

Full suite (pytest test -m 'not net' --ignore=test/performance): 2349 passed before and after, comm -3 over the FAILED lines empty, and 2352 with the new cases. The one pre-existing failure, test_shared_fixture_works_without_checkout_history, is an environment artefact — that test filters the child environment down to PATH and friends, so the gix package installed on PYTHONPATH is not visible to it — and fails identically on main.

Twelve legal calls (index.diff(None), index.diff("HEAD"), commit.diff(), commit.diff("HEAD~1"), commit.diff(NULL_TREE), each with and without create_patch) against a two-commit repo containing a rename, a deletion, an addition, a mode change, a binary file, a symlink, a staged change, an untracked file and a subdirectory produce byte-identical output before and after (cmp, 2706 bytes).

ruff 0.17.2 (the pinned rev) check and format --check pass on the changed files.

Note this needs git ≥ 2.52 to run the suite, so I built 2.56.1 locally rather than relying on the system 2.43.

@qianeric-backup
qianeric-backup marked this pull request as ready for review November 27, 2026 15:46
Document that diff.noprefix, diff.srcPrefix and diff.dstPrefix no longer
make Diffable.diff(..., create_patch=True) return an empty DiffIndex.
@qianeric-backup
qianeric-backup force-pushed the diff-patch-header-prefixes branch from db24842 to 27c8c82 Compare November 27, 2026 15:47
@Byron

Byron commented Nov 27, 2026

Copy link
Copy Markdown
Member

I am closing this PR as it violates the contributing guideline: fixes must refer to the downstream issue that motivates the fix.
Thanks for your understanding.

PS: If GitPython is too buggy and annoying to contribute to, consider trying its successor, GixPython. The underlying engine, Gitoxide has no restrictions on contributions either.

@Byron Byron closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants