Skip to content

Commit 2771a87

Browse files
committed
Support quoted filenames
1 parent ab8fcd4 commit 2771a87

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ Contributors
3434
* (`@cpackham-atlnz`)
3535
* David Leen (`@dleen`)
3636
* Martin Liška (`@marxin`)
37+
* Tushar Sadhwani (`@tushar-deepsource`)
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
diff --git "a/A \303\242 B.py" "b/A \303\242 B.py"
2+
new file mode 100644
3+
index 0000000..ce01362
4+
--- /dev/null
5+
+++ "b/A \303\242 B.py"
6+
@@ -0,0 +1 @@
7+
+hello

tests/test_parser.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
# The MIT License (MIT)
4-
# Copyright (c) 2014-2021 Matias Bordese
4+
# Copyright (c) 2014-2023 Matias Bordese
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to deal
@@ -337,6 +337,19 @@ def test_parse_filename_prefix_with_spaces(self):
337337
self.assertTrue(res[0].is_added_file)
338338
self.assertEqual(res[0].path, 'dst://foo bar/baz')
339339

340+
def test_parse_quoted_filename(self):
341+
filename = os.path.join(self.samples_dir, 'samples/git_quoted_filename.diff')
342+
with open(filename) as f:
343+
res = PatchSet(f)
344+
345+
self.assertEqual(len(res), 1)
346+
347+
self.assertEqual(res[0].source_file, '/dev/null')
348+
self.assertEqual(res[0].target_file, '"b/A \\303\\242 B.py"')
349+
self.assertTrue(res[0].is_added_file)
350+
self.assertEqual(res[0].path, '"A \\303\\242 B.py"')
351+
352+
340353
def test_deleted_file(self):
341354
filename = os.path.join(self.samples_dir, 'samples/git_delete.diff')
342355
with open(filename) as f:

unidiff/constants.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
# The MIT License (MIT)
4-
# Copyright (c) 2014-2022 Matias Bordese
4+
# Copyright (c) 2014-2023 Matias Bordese
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to deal
@@ -30,14 +30,14 @@
3030

3131

3232
RE_SOURCE_FILENAME = re.compile(
33-
r'^--- (?P<filename>[^\t\n]+)(?:\t(?P<timestamp>[^\n]+))?')
33+
r'^--- (?P<filename>"?[^\t\n]+"?)(?:\t(?P<timestamp>[^\n]+))?')
3434
RE_TARGET_FILENAME = re.compile(
35-
r'^\+\+\+ (?P<filename>[^\t\n]+)(?:\t(?P<timestamp>[^\n]+))?')
35+
r'^\+\+\+ (?P<filename>"?[^\t\n]+"?)(?:\t(?P<timestamp>[^\n]+))?')
3636

3737

3838
# check diff git line for git renamed files support
3939
RE_DIFF_GIT_HEADER = re.compile(
40-
r'^diff --git (?P<source>a/[^\t\n]+) (?P<target>b/[^\t\n]+)')
40+
r'^diff --git (?P<source>"?a/[^\t\n]+"?) (?P<target>"?b/[^\t\n]+"?)')
4141
RE_DIFF_GIT_HEADER_URI_LIKE = re.compile(
4242
r'^diff --git (?P<source>.*://[^\t\n]+) (?P<target>.*://[^\t\n]+)')
4343
RE_DIFF_GIT_HEADER_NO_PREFIX = re.compile(

unidiff/patch.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
# The MIT License (MIT)
4-
# Copyright (c) 2014-2022 Matias Bordese
4+
# Copyright (c) 2014-2023 Matias Bordese
55
#
66
# Permission is hereby granted, free of charge, to any person obtaining a copy
77
# of this software and associated documentation files (the "Software"), to deal
@@ -393,9 +393,16 @@ def path(self):
393393
# if this is a rename, prefer the target filename
394394
filepath = self.target_file
395395

396+
quoted = filepath.startswith('"') and filepath.endswith('"')
397+
if quoted:
398+
filepath = filepath[1:-1]
399+
396400
if filepath.startswith('a/') or filepath.startswith('b/'):
397401
filepath = filepath[2:]
398402

403+
if quoted:
404+
filepath = '"{}"'.format(filepath)
405+
399406
return filepath
400407

401408
@property

0 commit comments

Comments
 (0)