Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with spaces in filename when using custom prefix #100

Merged
merged 1 commit into from
Aug 31, 2022
Merged
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
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