From 566407dd94804f02e84c049121cbb7c9dbf192c8 Mon Sep 17 00:00:00 2001 From: danny crasto Date: Wed, 10 Sep 2025 21:20:47 +0400 Subject: [PATCH] Support extended git PatchFile Prefix Extend support of traditional source/target Patch File prefix' to include git's [mnemonicPrefix](https://git-scm.com/docs/diff-config#Documentation/diff-config.txt-diffmnemonicPrefix). - uses regex for prefix match - adds tests for old and new behavior --- tests/test_patchedfile.py | 12 ++++++++++++ unidiff/constants.py | 2 ++ unidiff/patch.py | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_patchedfile.py b/tests/test_patchedfile.py index cd1342a..962fad2 100644 --- a/tests/test_patchedfile.py +++ b/tests/test_patchedfile.py @@ -52,3 +52,15 @@ def test_is_modified_file(self): hunk = Hunk(src_start=1, src_len=10, tgt_start=1, tgt_len=8) self.patched_file.append(hunk) self.assertTrue(self.patched_file.is_modified_file) + + def test_default_file_prefix(self): + default_prefix = PatchedFile(source="a/foo/", target="b/foo/") + self.assertTrue(default_prefix.path == "foo/") + + def test_git_mnemonic_file_prefix(self): + default_prefix = PatchedFile(source="i/foo/", target="c/foo/") + self.assertTrue(default_prefix.path == "foo/") + + def test_no_file_prefix(self): + default_prefix = PatchedFile(source="/foo/", target="/foo/") + self.assertTrue(default_prefix.path == "/foo/") diff --git a/unidiff/constants.py b/unidiff/constants.py index 266d59c..be63883 100644 --- a/unidiff/constants.py +++ b/unidiff/constants.py @@ -71,6 +71,8 @@ r'(?P[^\t]+?)(?:\t(?P[\s0-9:\+-]+))?' r'(?: and (?P[^\t]+?)(?:\t(?P[\s0-9:\+-]+))?)? (differ|has changed)') +RE_PATCH_FILE_PREFIX = re.compile(r"^[abciow12]/.*$") + DEFAULT_ENCODING = 'UTF-8' DEV_NULL = '/dev/null' diff --git a/unidiff/patch.py b/unidiff/patch.py index bee28fc..86db378 100644 --- a/unidiff/patch.py +++ b/unidiff/patch.py @@ -50,6 +50,7 @@ RE_TARGET_FILENAME, RE_NO_NEWLINE_MARKER, RE_BINARY_DIFF, + RE_PATCH_FILE_PREFIX, ) from unidiff.errors import UnidiffParseError @@ -397,7 +398,7 @@ def path(self): if quoted: filepath = filepath[1:-1] - if filepath.startswith('a/') or filepath.startswith('b/'): + if RE_PATCH_FILE_PREFIX.match(filepath): filepath = filepath[2:] if quoted: