Skip to content
Open
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
7 changes: 7 additions & 0 deletions tests/samples/debdiff.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff -u -N original/added.txt new/added.txt
--- original/added.txt 1970-01-01 01:00:00.000000000 +0100
+++ new/added.txt 2025-03-26 12:42:27.672906377 +0000
@@ -0,0 +1 @@
+new file
Binary files Binary files /t/p1/a.png and /t/p2/a.png differ
Binary files Binary files /t/p1/b.png and /t/p2/b.png differ
19 changes: 19 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ def test_parse_diff_with_new_and_modified_binary_files(self):
self.assertTrue(res[4].is_added_file)
self.assertFalse(res[4].is_binary_file)

def test_parse_diff_with_new_and_modified_binary_files_linenos(self):
"""Parse debdiff output for a source debdiff with binary files."""
utf8_file = os.path.join(self.samples_dir, 'samples/debdiff.diff')
with open(utf8_file, 'r') as diff_file:
res = PatchSet(diff_file)

# three file in the patch
self.assertEqual(len(res), 3)

# first file is new/added.txt
self.assertEqual(res[0].path, 'new/added.txt')

# first file, first patch, first hunk diff_line_no is 5
self.assertEqual(res[0][0][0].diff_line_no, 5)

# second file is /t/p2/a.png
self.assertEqual(res[1].path, '/t/p2/a.png')

def test_parse_round_trip_with_binary_files_in_diff(self):
"""Parse git diff with binary files though round trip"""
utf8_file = os.path.join(self.samples_dir, 'samples/sample8.diff')
Expand Down Expand Up @@ -443,6 +461,7 @@ def do_test_diff_hunk_positions(self, res):
self.assertEqual(hunk_positions, expected_hunk_positions)

def test_binary_patch(self):
# samples/binary.diff is as generated by the debdiff Debian utility
utf8_file = os.path.join(self.samples_dir, 'samples/binary.diff')
with open(utf8_file, 'r') as diff_file:
res = PatchSet(diff_file)
Expand Down
8 changes: 6 additions & 2 deletions unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class PatchedFile(list):

def __init__(
self,
diff_line_no: Optional[int] = None,
patch_info: Optional[PatchInfo] = None,
source: str = '',
target: str = '',
Expand All @@ -217,6 +218,7 @@ def __init__(
is_binary_file: bool = False,
) -> None:
super(PatchedFile, self).__init__()
self.diff_line_no = diff_line_no
self.patch_info = patch_info
self.source_file = source
self.source_timestamp = source_timestamp
Expand Down Expand Up @@ -471,7 +473,7 @@ def _parse(
patch_info = None
enumerated_diff = enumerate(diff, 1)

for _, line in enumerated_diff:
for diff_line_no, line in enumerated_diff:
if encoding is not None and isinstance(line, bytes):
line = line.decode(encoding)

Expand All @@ -484,7 +486,7 @@ def _parse(
source_file = is_diff_git_header.group('source')
target_file = is_diff_git_header.group('target')
current_file = PatchedFile(
patch_info, source_file, target_file, None, None)
diff_line_no, patch_info, source_file, target_file, None, None)
self.append(current_file)
patch_info.append(line)
continue
Expand Down Expand Up @@ -536,6 +538,7 @@ def _parse(
if current_file is None:
# add current file to PatchSet
current_file = PatchedFile(
diff_line_no,
patch_info, source_file, target_file,
source_timestamp, target_timestamp)
self.append(current_file)
Expand Down Expand Up @@ -585,6 +588,7 @@ def _parse(
current_file.is_binary_file = True
else:
current_file = PatchedFile(
diff_line_no,
patch_info,
source_file,
target_file,
Expand Down