From 494a64c745d36384604802b58c6651fe0ae758c8 Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Thu, 19 Nov 2026 02:46:47 +0530 Subject: [PATCH] fix(gix): rename empty files in native tree diffs like Git On the Gix backend, `Diffable.diff()` between two trees or commits builds its result in `_backend.tree_diff()`. When the added and deleted blob IDs pair up unambiguously, the adapter asks Gix for exact rename tracking with `gix.Rewrites(percentage=None)`. GixPython's `Rewrites` defaults to `track_empty=False`, the merge-style setting that never pairs empty blobs, while `git diff -M` (and the CLI backend) defaults to `--rename-empty` and reports an empty file moved from `a` to `b` as `R100`. So for a commit that only renames an empty file (an `__init__.py`, a `.gitkeep`, a placeholder), the CLI backend returned one `R` diff with `renamed_file` set, and the Gix backend returned an `A` diff for the new path and a `D` diff for the old one. The same happened with `R=True`. Code that follows renames through `rename_from`/`rename_to`, or that treats additions and deletions differently from renames, saw a different history depending on the backend. The adapter now passes `track_empty=True`, which is the Gix mode that matches Git's default for this diff. No pairing is done in Python; the existing ambiguity guard still sends duplicate blob IDs to Git, so several empty files added or deleted at once keep using the CLI path. This is adapter configuration rather than a Gix bug, so no new ledger entry is added. Validation on Windows with CPython 3.11, Git 2.56.0 and GixPython 0.3.0 built from source: - `test_rename_of_empty_file`, using the new `diff.sh empty-rename` fixture, fails on the Gix backend before the change (two diffs instead of one) and passes on both backends after it. - A sweep of 18 tree pairs compared against the CLI backend (empty and non-empty renames, mode changes, symlink/file and gitlink cases, swaps, directory moves, both directions with `R=True`) differed only in the empty-rename case before the change and matches in every case after it; all pairs without duplicate blob IDs stayed native. - `test/test_diff.py` passes on the CLI backend (50 passed). On the Gix backend, `test_diff.py`, `test_gix_backend.py`, `test_cli_process_counts.py` and `test_commit.py` give 193 passed and 2 failed; the two failures (`test_native_repository_lifetime_and_refresh[sha1|sha256]`) fail identically without this change because the test machine has no global Git identity for its `git commit`. - `ruff check`, `ruff format --check`, `shellcheck`, `mypy` and `git diff --check` pass. The full suite was not run. Agent-Model: claude-opus-5-5 --- git/_backend.py | 2 +- test/test_diff.py | 12 ++++++++++++ tests/fixtures/diff.sh | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/git/_backend.py b/git/_backend.py index 51d25c436..f3884a51f 100644 --- a/git/_backend.py +++ b/git/_backend.py @@ -930,7 +930,7 @@ def tree_diff(repository: Any, left: Any, right: Any, paths: Any, patch: bool, o ): raise _Unsupported("inexact or ambiguous rename detection (GIX-11)") # Exact rewrites need no blob filters, textconv, or external diff drivers. - diff_options = diff_options.track_rewrites(gix.Rewrites(percentage=None)) + diff_options = diff_options.track_rewrites(gix.Rewrites(percentage=None, track_empty=True)) changes = repo.diff_tree_to_tree(before, after, diff_options) result: DiffIndex[Diff] = DiffIndex() for change in changes: diff --git a/test/test_diff.py b/test/test_diff.py index de87e081e..4339ad9d2 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -624,6 +624,18 @@ def test_rename_override(self): self.assertEqual("file_a.txt", diff.rename_from) self.assertEqual("file_b.txt", diff.rename_to) + def test_rename_of_empty_file(self): + # `git diff -M` pairs empty files like any other exact rename. + with fixture_repo("diff.sh", "empty-rename") as repo: + before, after = repo.commit("HEAD~1"), repo.commit("HEAD") + for diffs, paths in ( + (before.diff(after), ("empty_a", "empty_b")), + (before.diff(after, R=True), ("empty_b", "empty_a")), + ): + (diff,) = diffs + self.assertTrue(diff.renamed_file) + self.assertEqual(paths, (diff.rename_from, diff.rename_to)) + def test_diff_patch_with_external_engine(self): with fixture_repo("diff.sh", "external") as repo: head_against_head = repo.head.commit.diff("HEAD^", create_patch=True) diff --git a/tests/fixtures/diff.sh b/tests/fixtures/diff.sh index b036f1c83..2605d144e 100644 --- a/tests/fixtures/diff.sh +++ b/tests/fixtures/diff.sh @@ -81,6 +81,14 @@ case "$1" in git add file_b.txt git commit -m 'Removed file_a.txt. Added file_b.txt' ;; + empty-rename) + empty=$(printf '' | git hash-object -w --stdin) + before=$(printf '100644 blob %s\tempty_a\0' "$empty" | git mktree -z) + after=$(printf '100644 blob %s\tempty_b\0' "$empty" | git mktree -z) + parent=$(git commit-tree "$before" -m 'Added empty_a') + commit=$(git commit-tree "$after" -p "$parent" -m 'Renamed empty_a to empty_b') + git update-ref refs/heads/master "$commit" + ;; external) printf 'first_line\n' > .gitignore git add .gitignore