Skip to content

Commit 4aeaf56

Browse files
Address review feedback: improve test coverage for all mnemonicPrefix values
Changes per reviewer (Byron) and Copilot suggestions: - Add h/ (HEAD) to the comment listing supported prefixes - Update test docstring to include h/ prefix - Expand test to cover all prefix combinations using subTest: - c/ (commit) vs w/ (worktree) - c/ (commit) vs i/ (index) - i/ (index) vs w/ (worktree) - o/ (object) vs w/ (worktree) - h/ (HEAD) vs i/ (index) - h/ (HEAD) vs w/ (worktree) This ensures the regex pattern [abciwoh] and decode_path() work correctly with all supported mnemonicPrefix values.
1 parent 6e77e26 commit 4aeaf56

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

git/diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ class Diff:
371371

372372
# Precompiled regex.
373373
# Note: The path prefixes support both default (a/b) and mnemonicPrefix mode
374-
# which can use prefixes like c/ (commit), w/ (worktree), i/ (index), o/ (object)
374+
# which can use prefixes like c/ (commit), w/ (worktree), i/ (index), o/ (object), and h/ (HEAD)
375375
re_header = re.compile(
376376
rb"""
377377
^diff[ ]--git

test/test_diff.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -290,28 +290,42 @@ def test_diff_mnemonic_prefix(self):
290290
- w/ for worktree
291291
- i/ for index
292292
- o/ for object
293+
- h/ for HEAD
293294
294295
This addresses issue #2013 where the regex only matched [ab]/ prefixes.
295296
"""
296-
# Create a diff with mnemonicPrefix-style c/ and w/ prefixes
297-
# Using valid 40-char hex SHAs
298-
diff_mnemonic = b"""diff --git c/.vscode/launch.json w/.vscode/launch.json
299-
index 1234567890abcdef1234567890abcdef12345678..abcdef1234567890abcdef1234567890abcdef12 100644
300-
--- c/.vscode/launch.json
301-
+++ w/.vscode/launch.json
302-
@@ -1,3 +1,3 @@
303-
-old content
304-
+new content
305-
"""
306-
diff_proc = StringProcessAdapter(diff_mnemonic)
307-
diffs = Diff._index_from_patch_format(self.rorepo, diff_proc)
308-
309-
# Should parse successfully (previously would fail or return empty)
310-
self.assertEqual(len(diffs), 1)
311-
diff = diffs[0]
312-
# The path should be extracted correctly (without the c/ or w/ prefix)
313-
self.assertEqual(diff.a_path, ".vscode/launch.json")
314-
self.assertEqual(diff.b_path, ".vscode/launch.json")
297+
# Test all mnemonicPrefix combinations
298+
# Each tuple is (a_prefix, b_prefix) representing different comparison types
299+
prefix_pairs = [
300+
(b"c/", b"w/"), # commit vs worktree
301+
(b"c/", b"i/"), # commit vs index
302+
(b"i/", b"w/"), # index vs worktree
303+
(b"o/", b"w/"), # object vs worktree
304+
(b"h/", b"i/"), # HEAD vs index
305+
(b"h/", b"w/"), # HEAD vs worktree
306+
]
307+
308+
for a_prefix, b_prefix in prefix_pairs:
309+
with self.subTest(a_prefix=a_prefix, b_prefix=b_prefix):
310+
diff_mnemonic = (
311+
b"diff --git " + a_prefix + b".vscode/launch.json " + b_prefix + b".vscode/launch.json\n"
312+
b"index 1234567890abcdef1234567890abcdef12345678.."
313+
b"abcdef1234567890abcdef1234567890abcdef12 100644\n"
314+
b"--- " + a_prefix + b".vscode/launch.json\n"
315+
b"+++ " + b_prefix + b".vscode/launch.json\n"
316+
b"@@ -1,3 +1,3 @@\n"
317+
b"-old content\n"
318+
b"+new content\n"
319+
)
320+
diff_proc = StringProcessAdapter(diff_mnemonic)
321+
diffs = Diff._index_from_patch_format(self.rorepo, diff_proc)
322+
323+
# Should parse successfully (previously would fail or return empty)
324+
self.assertEqual(len(diffs), 1)
325+
diff = diffs[0]
326+
# The path should be extracted correctly (without the prefix)
327+
self.assertEqual(diff.a_path, ".vscode/launch.json")
328+
self.assertEqual(diff.b_path, ".vscode/launch.json")
315329

316330
def test_diff_patch_format(self):
317331
# Test all of the 'old' format diffs for completeness - it should at least be

0 commit comments

Comments
 (0)