Skip to content

Commit 5ffee6c

Browse files
committed
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
1 parent fe74a6f commit 5ffee6c

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎git/diff.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ class Diff:
431431
(?:^similarity[ ]index[ ]\d+%\n
432432
^copy[ ]from[ ](?P<copied_from_name>.*)\n
433433
^copy[ ]to[ ](?P<copied_file_name>.*)(?:\n|$))?
434+
(?:^dissimilarity[ ]index[ ]\d+%(?:\n|$))?
434435
(?:^index[ ](?P<a_blob_id>[0-9A-Fa-f]+)
435436
\.\.(?P<b_blob_id>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
436437
(?:^---[ ](?P<a_path>[^\t\n\r\f\v]*)[\t\r\f\v]*(?:\n|$))?
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
diff --git a/changed-mode.txt b/changed-mode.txt
2+
old mode 100644
3+
new mode 100755
4+
dissimilarity index 100%
5+
index 6d50bddc95c032358a4d93f18a5efcc04eec2d4b..ec4c9338a05077382eaf246acce3c01704b0e7b7
6+
--- a/changed-mode.txt
7+
+++ b/changed-mode.txt
8+
@@ -1,5 +1,5 @@
9+
-old line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10+
-old line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11+
-old line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
12+
-old line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
13+
-old line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
14+
+new line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
15+
+new line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
16+
+new line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
17+
+new line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
18+
+new line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
19+
diff --git a/same-mode.txt b/same-mode.txt
20+
dissimilarity index 100%
21+
index 6d50bddc95c032358a4d93f18a5efcc04eec2d4b..ec4c9338a05077382eaf246acce3c01704b0e7b7 100644
22+
--- a/same-mode.txt
23+
+++ b/same-mode.txt
24+
@@ -1,5 +1,5 @@
25+
-old line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
26+
-old line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
27+
-old line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
28+
-old line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
29+
-old line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
30+
+new line 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
31+
+new line 2 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
32+
+new line 3 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
33+
+new line 4 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
34+
+new line 5 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

‎test/test_diff.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ def test_diff_with_copied_file(self):
151151
self.assertEqual(diff.b_path, "test2.txt")
152152
self.assertEqual(len(list(diffs.iter_change_type("C"))), 1)
153153

154+
def test_diff_with_dissimilarity_index(self):
155+
# `git diff -B` writes "dissimilarity index" for a rewritten file, between the
156+
# mode lines and the index line.
157+
output = StringProcessAdapter(fixture("diff_dissimilarity_mode"))
158+
diffs = Diff._index_from_patch_format(self.rorepo, output)
159+
self._assert_diff_format(diffs)
160+
161+
self.assertEqual(2, len(diffs))
162+
for diff, (a_mode, b_mode) in zip(diffs, ((0o100644, 0o100755), (0o100644, 0o100644))):
163+
self.assertEqual(diff.a_mode, a_mode)
164+
self.assertEqual(diff.b_mode, b_mode)
165+
self.assertEqual(diff.a_blob.hexsha, "6d50bddc95c032358a4d93f18a5efcc04eec2d4b")
166+
self.assertEqual(diff.b_blob.hexsha, "ec4c9338a05077382eaf246acce3c01704b0e7b7")
167+
self.assertTrue(diff.diff.startswith(b"@@ -1,5 +1,5 @@"))
168+
self.assertEqual(diffs[0].a_path, "changed-mode.txt")
169+
self.assertEqual(diffs[1].a_path, "same-mode.txt")
170+
154171
def test_diff_with_change_in_type(self):
155172
output = StringProcessAdapter(fixture("diff_change_in_type"))
156173
diffs = Diff._index_from_patch_format(self.rorepo, output)

0 commit comments

Comments
 (0)