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
12 changes: 12 additions & 0 deletions tests/test_patchedfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
2 changes: 2 additions & 0 deletions unidiff/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
r'(?P<source_filename>[^\t]+?)(?:\t(?P<source_timestamp>[\s0-9:\+-]+))?'
r'(?: and (?P<target_filename>[^\t]+?)(?:\t(?P<target_timestamp>[\s0-9:\+-]+))?)? (differ|has changed)')

RE_PATCH_FILE_PREFIX = re.compile(r"^[abciow12]/.*$")

DEFAULT_ENCODING = 'UTF-8'

DEV_NULL = '/dev/null'
Expand Down
3 changes: 2 additions & 1 deletion unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
RE_TARGET_FILENAME,
RE_NO_NEWLINE_MARKER,
RE_BINARY_DIFF,
RE_PATCH_FILE_PREFIX,
)
from unidiff.errors import UnidiffParseError

Expand Down Expand Up @@ -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:
Expand Down