Skip to content

Commit

Permalink
Merge pull request #100 from dleen/master
Browse files Browse the repository at this point in the history
Fix issue with spaces in filename when using custom prefix
  • Loading branch information
matiasb authored Aug 31, 2022
2 parents 3de381b + 9679b10 commit a840305
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tests/samples/git_filenames_with_spaces_prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git src://foo bar/baz dst://foo bar/baz
new file mode 100644
index 00000000000..0a72e5064c8
--- /dev/null
+++ dst://foo bar/baz
@@ -0,0 +1,1 @@
+blah
12 changes: 12 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ def test_parse_filename_with_spaces(self):
self.assertTrue(res[0].is_added_file)
self.assertEqual(res[0].path, 'has spaces/t.sql')

def test_parse_filename_prefix_with_spaces(self):
filename = os.path.join(self.samples_dir, 'samples/git_filenames_with_spaces_prefix.diff')
with open(filename) as f:
res = PatchSet(f)

self.assertEqual(len(res), 1)

self.assertEqual(res[0].source_file, '/dev/null')
self.assertEqual(res[0].target_file, 'dst://foo bar/baz')
self.assertTrue(res[0].is_added_file)
self.assertEqual(res[0].path, 'dst://foo bar/baz')

def test_deleted_file(self):
filename = os.path.join(self.samples_dir, 'samples/git_delete.diff')
with open(filename) as f:
Expand Down
2 changes: 2 additions & 0 deletions unidiff/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
# check diff git line for git renamed files support
RE_DIFF_GIT_HEADER = re.compile(
r'^diff --git (?P<source>a/[^\t\n]+) (?P<target>b/[^\t\n]+)')
RE_DIFF_GIT_HEADER_URI_LIKE = re.compile(
r'^diff --git (?P<source>.*://[^\t\n]+) (?P<target>.*://[^\t\n]+)')
RE_DIFF_GIT_HEADER_NO_PREFIX = re.compile(
r'^diff --git (?P<source>[^\t\n]+) (?P<target>[^\t\n]+)')

Expand Down
5 changes: 4 additions & 1 deletion unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
LINE_VALUE_NO_NEWLINE,
RE_DIFF_GIT_DELETED_FILE,
RE_DIFF_GIT_HEADER,
RE_DIFF_GIT_HEADER_URI_LIKE,
RE_DIFF_GIT_HEADER_NO_PREFIX,
RE_DIFF_GIT_NEW_FILE,
RE_HUNK_BODY_LINE,
Expand Down Expand Up @@ -479,7 +480,9 @@ def _parse(self, diff, encoding, metadata_only):
line = line.decode(encoding)

# check for a git file rename
is_diff_git_header = RE_DIFF_GIT_HEADER.match(line) or RE_DIFF_GIT_HEADER_NO_PREFIX.match(line)
is_diff_git_header = RE_DIFF_GIT_HEADER.match(line) or \
RE_DIFF_GIT_HEADER_URI_LIKE.match(line) or \
RE_DIFF_GIT_HEADER_NO_PREFIX.match(line)
if is_diff_git_header:
patch_info = PatchInfo()
source_file = is_diff_git_header.group('source')
Expand Down

0 comments on commit a840305

Please sign in to comment.