From c639af8ab1f98f986e925ab53cfe8458e055365d Mon Sep 17 00:00:00 2001 From: Jef Roelandt Date: Thu, 30 Oct 2025 12:03:46 +0100 Subject: [PATCH 1/2] Fix error when trying to make a PatchSet with a diff containing empty filenames Seeing this https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html Nothing forbids a header to container empty filenames. In fact, difflib.unified_diff() creates diff with empty filenames in header. (See https://github.com/matiasb/python-unidiff/issues/115) --- tests/samples/sample_no_filename.diff | 6 ++++++ tests/test_parser.py | 8 ++++++++ unidiff/constants.py | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/samples/sample_no_filename.diff diff --git a/tests/samples/sample_no_filename.diff b/tests/samples/sample_no_filename.diff new file mode 100644 index 0000000..dcd7712 --- /dev/null +++ b/tests/samples/sample_no_filename.diff @@ -0,0 +1,6 @@ +--- ++++ +@@ -1,2 +1,2 @@ +-foo + bar ++foobar diff --git a/tests/test_parser.py b/tests/test_parser.py index 1ca3388..fc2fdcb 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -534,3 +534,11 @@ def test_git_renaming(self): # by unidiff are the same with codecs.open(file_path, 'r', encoding='utf-8') as diff_file: self.assertEqual(diff_file.read(), str(res)) + + def test_no_filename_in_header(self): + tests_dir = os.path.dirname(os.path.realpath(__file__)) + file_path = os.path.join(tests_dir, 'samples/sample_no_filename.diff') + with codecs.open(file_path, 'r', encoding='utf-8') as diff_file: + res = PatchSet(diff_file) + + self.assertEqual(' ', res.modified_files[0].source_file) diff --git a/unidiff/constants.py b/unidiff/constants.py index 2166e4b..83c7745 100644 --- a/unidiff/constants.py +++ b/unidiff/constants.py @@ -30,9 +30,9 @@ RE_SOURCE_FILENAME = re.compile( - r'^--- (?P"?[^\t\n]+"?)(?:\t(?P[^\n]+))?') + r'^---( ?)(?P"?[^\t\n]+"?)(?:\t(?P[^\n]+))?') RE_TARGET_FILENAME = re.compile( - r'^\+\+\+ (?P"?[^\t\n]+"?)(?:\t(?P[^\n]+))?') + r'^\+\+\+( ?)(?P"?[^\t\n]+"?)(?:\t(?P[^\n]+))?') # check diff git line for git renamed files support From 1fec8dd40ed086f19013e72649c07207df53962e Mon Sep 17 00:00:00 2001 From: Jef Roelandt Date: Thu, 30 Oct 2025 12:05:34 +0100 Subject: [PATCH 2/2] Fix missing f-string in a raise statement --- unidiff/patch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unidiff/patch.py b/unidiff/patch.py index 48cb93a..bfe80e4 100644 --- a/unidiff/patch.py +++ b/unidiff/patch.py @@ -549,7 +549,7 @@ def _parse( if is_hunk_header: patch_info = None if current_file is None: - raise UnidiffParseError('Unexpected hunk found: {line}') + raise UnidiffParseError(f'Unexpected hunk found: {line}') current_file._parse_hunk( line, enumerated_diff,