Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ def test_parse_diff_with_new_and_modified_binary_files(self):
self.assertFalse(res[4].is_binary_file)
self.assertEqual(res[4].diff_line_no, 15)

def test_parse_binary_diff_without_target_filename(self):
# hg emits "Binary file X has changed", with no target filename;
# both sides should then refer to the same file
diff = (
'diff -r a1b2c3d4 image.png\n'
'Binary file image.png has changed\n'
)
res = PatchSet(diff)

self.assertEqual(len(res), 1)
self.assertTrue(res[0].is_binary_file)
self.assertEqual(res[0].source_file, 'image.png')
self.assertEqual(res[0].target_file, 'image.png')
self.assertFalse(res[0].is_rename)
self.assertEqual(res[0].path, 'image.png')
# the diff still round-trips
self.assertEqual(str(res), diff)

def test_parse_debdiff_binary_file_line_numbers(self):
# issue #122 / PR #123: a binary change without hunks should still
# expose the diff line number where its entry appears.
Expand Down
5 changes: 4 additions & 1 deletion unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ def _parse(self, diff: Iterable, encoding: Optional[str],
is_binary_diff = RE_BINARY_DIFF.match(line)
if is_binary_diff:
source_file = is_binary_diff.group('source_filename')
target_file = is_binary_diff.group('target_filename')
# formats like hg's "Binary file X has changed" carry no
# target filename; both sides refer to the same file
target_file = (is_binary_diff.group('target_filename')
or source_file)
patch_info.append(line)
if current_file is not None:
current_file.is_binary_file = True
Expand Down
Loading