Skip to content

Commit 494a64c

Browse files
committed
fix(gix): rename empty files in native tree diffs like Git
<!-- agent: Claude --> 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
1 parent 029964a commit 494a64c

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎git/_backend.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ def tree_diff(repository: Any, left: Any, right: Any, paths: Any, patch: bool, o
930930
):
931931
raise _Unsupported("inexact or ambiguous rename detection (GIX-11)")
932932
# Exact rewrites need no blob filters, textconv, or external diff drivers.
933-
diff_options = diff_options.track_rewrites(gix.Rewrites(percentage=None))
933+
diff_options = diff_options.track_rewrites(gix.Rewrites(percentage=None, track_empty=True))
934934
changes = repo.diff_tree_to_tree(before, after, diff_options)
935935
result: DiffIndex[Diff] = DiffIndex()
936936
for change in changes:

‎test/test_diff.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,18 @@ def test_rename_override(self):
624624
self.assertEqual("file_a.txt", diff.rename_from)
625625
self.assertEqual("file_b.txt", diff.rename_to)
626626

627+
def test_rename_of_empty_file(self):
628+
# `git diff -M` pairs empty files like any other exact rename.
629+
with fixture_repo("diff.sh", "empty-rename") as repo:
630+
before, after = repo.commit("HEAD~1"), repo.commit("HEAD")
631+
for diffs, paths in (
632+
(before.diff(after), ("empty_a", "empty_b")),
633+
(before.diff(after, R=True), ("empty_b", "empty_a")),
634+
):
635+
(diff,) = diffs
636+
self.assertTrue(diff.renamed_file)
637+
self.assertEqual(paths, (diff.rename_from, diff.rename_to))
638+
627639
def test_diff_patch_with_external_engine(self):
628640
with fixture_repo("diff.sh", "external") as repo:
629641
head_against_head = repo.head.commit.diff("HEAD^", create_patch=True)

‎tests/fixtures/diff.sh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ case "$1" in
8181
git add file_b.txt
8282
git commit -m 'Removed file_a.txt. Added file_b.txt'
8383
;;
84+
empty-rename)
85+
empty=$(printf '' | git hash-object -w --stdin)
86+
before=$(printf '100644 blob %s\tempty_a\0' "$empty" | git mktree -z)
87+
after=$(printf '100644 blob %s\tempty_b\0' "$empty" | git mktree -z)
88+
parent=$(git commit-tree "$before" -m 'Added empty_a')
89+
commit=$(git commit-tree "$after" -p "$parent" -m 'Renamed empty_a to empty_b')
90+
git update-ref refs/heads/master "$commit"
91+
;;
8492
external)
8593
printf 'first_line\n' > .gitignore
8694
git add .gitignore

0 commit comments

Comments
 (0)