From eec4c8d2775f4338268a03338d3f2903b8ccaa98 Mon Sep 17 00:00:00 2001 From: Carles Pina i Estany Date: Thu, 27 Nov 2025 16:20:27 +0100 Subject: [PATCH 1/2] PatchSet._parse(): pass diff_line_no to PatchedFile --- unidiff/patch.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/unidiff/patch.py b/unidiff/patch.py index bfe80e4..8c2c820 100644 --- a/unidiff/patch.py +++ b/unidiff/patch.py @@ -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 = '', @@ -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 @@ -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) @@ -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 @@ -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) @@ -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, From 74756f3b42f7ff5a5bfc588d6ec7a17a300620ed Mon Sep 17 00:00:00 2001 From: Carles Pina i Estany Date: Thu, 27 Nov 2025 16:22:35 +0100 Subject: [PATCH 2/2] Add unit test for diffs generated by debdiff utility --- tests/samples/debdiff.diff | 7 +++++++ tests/test_parser.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/samples/debdiff.diff diff --git a/tests/samples/debdiff.diff b/tests/samples/debdiff.diff new file mode 100644 index 0000000..a53efe9 --- /dev/null +++ b/tests/samples/debdiff.diff @@ -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 diff --git a/tests/test_parser.py b/tests/test_parser.py index fc2fdcb..4a7362b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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') @@ -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)