From 5ffee6c00aea4faad42622932f6f6e4fc6c22621 Mon Sep 17 00:00:00 2001 From: JROtto5 <222316562+JROtto5@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:08:16 -0500 Subject: [PATCH] fix(diff): read the index line after a dissimilarity index With `-B`, Git writes `dissimilarity index N%` for a rewritten file, between the mode lines and the `index` line. `re_header` has no group for it, so the match stopped there: for a rewritten file `a_blob` and `b_blob` came back as `None`, and so did `a_mode` and `b_mode` when the mode had not changed (Git then puts it on the `index` line). The hunks were still read. Other files in the same `-B` diff were not affected. Accept the line, in the place Git writes it, before the `index` line. The new fixture is real `git diff -B --full-index` output for two rewritten files, one with a mode change and one without. `test_diff_with_dissimilarity_index` fails without this change. Agent-Model: claude-opus-5-5 --- git/diff.py | 1 + test/fixtures/diff_dissimilarity_mode | 34 +++++++++++++++++++++++++++ test/test_diff.py | 17 ++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 test/fixtures/diff_dissimilarity_mode diff --git a/git/diff.py b/git/diff.py index e83b18f1f..7a19ffe15 100644 --- a/git/diff.py +++ b/git/diff.py @@ -431,6 +431,7 @@ class Diff: (?:^similarity[ ]index[ ]\d+%\n ^copy[ ]from[ ](?P.*)\n ^copy[ ]to[ ](?P.*)(?:\n|$))? + (?:^dissimilarity[ ]index[ ]\d+%(?:\n|$))? (?:^index[ ](?P[0-9A-Fa-f]+) \.\.(?P[0-9A-Fa-f]+)[ ]?(?P.+)?(?:\n|$))? (?:^---[ ](?P[^\t\n\r\f\v]*)[\t\r\f\v]*(?:\n|$))? diff --git a/test/fixtures/diff_dissimilarity_mode b/test/fixtures/diff_dissimilarity_mode new file mode 100644 index 000000000..3d0f70c43 --- /dev/null +++ b/test/fixtures/diff_dissimilarity_mode @@ -0,0 +1,34 @@ +diff --git a/changed-mode.txt b/changed-mode.txt +old mode 100644 +new mode 100755 +dissimilarity index 100% +index 6d50bddc95c032358a4d93f18a5efcc04eec2d4b..ec4c9338a05077382eaf246acce3c01704b0e7b7 +--- a/changed-mode.txt ++++ b/changed-mode.txt +@@ -1,5 +1,5 @@ +-old line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ++new line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 +diff --git a/same-mode.txt b/same-mode.txt +dissimilarity index 100% +index 6d50bddc95c032358a4d93f18a5efcc04eec2d4b..ec4c9338a05077382eaf246acce3c01704b0e7b7 100644 +--- a/same-mode.txt ++++ b/same-mode.txt +@@ -1,5 +1,5 @@ +-old line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-old line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ++new line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 ++new line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 diff --git a/test/test_diff.py b/test/test_diff.py index 76f31369b..de87e081e 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -151,6 +151,23 @@ def test_diff_with_copied_file(self): self.assertEqual(diff.b_path, "test2.txt") self.assertEqual(len(list(diffs.iter_change_type("C"))), 1) + def test_diff_with_dissimilarity_index(self): + # `git diff -B` writes "dissimilarity index" for a rewritten file, between the + # mode lines and the index line. + output = StringProcessAdapter(fixture("diff_dissimilarity_mode")) + diffs = Diff._index_from_patch_format(self.rorepo, output) + self._assert_diff_format(diffs) + + self.assertEqual(2, len(diffs)) + for diff, (a_mode, b_mode) in zip(diffs, ((0o100644, 0o100755), (0o100644, 0o100644))): + self.assertEqual(diff.a_mode, a_mode) + self.assertEqual(diff.b_mode, b_mode) + self.assertEqual(diff.a_blob.hexsha, "6d50bddc95c032358a4d93f18a5efcc04eec2d4b") + self.assertEqual(diff.b_blob.hexsha, "ec4c9338a05077382eaf246acce3c01704b0e7b7") + self.assertTrue(diff.diff.startswith(b"@@ -1,5 +1,5 @@")) + self.assertEqual(diffs[0].a_path, "changed-mode.txt") + self.assertEqual(diffs[1].a_path, "same-mode.txt") + def test_diff_with_change_in_type(self): output = StringProcessAdapter(fixture("diff_change_in_type")) diffs = Diff._index_from_patch_format(self.rorepo, output)