Skip to content

Commit e6c0cbb

Browse files
committed
deal with () in filenames by escaping them (NO_JIRA_
1 parent 52b3022 commit e6c0cbb

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

main/githooks.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
# File types that need a terminating newline
5757
TERMINATING_NEWLINE_EXTS = ['.c', '.cpp', '.h', '.inl']
5858

59+
esc_re = re.compile(r'\s|[]()[]')
60+
def _esc_char(match):
61+
return '\\' + match.group(0)
62+
63+
64+
def _escape_filename(filename):
65+
return esc_re.sub(_esc_char, filename)
66+
5967

6068
def _get_output(command, cwd='.'):
6169
return subprocess.check_output(command, shell=True, cwd=cwd).decode(errors='replace')
@@ -121,7 +129,7 @@ def get_file_content_as_binary(filename):
121129
_skip(filename, 'File is not UTF-8 encoded')
122130
data = None
123131
else:
124-
data = _get_output(f'git show :{filename}')
132+
data = _get_output(f'git show :{_escape_filename(filename)}')
125133
return data
126134

127135

@@ -134,7 +142,7 @@ def get_text_file_content(filename):
134142
if _is_github_event() or 'pytest' in sys.modules:
135143
data = Path(filename).read_text()
136144
else:
137-
data = _get_output(f'git show :{filename}')
145+
data = _get_output(f'git show :{_escape_filename(filename)}')
138146
return data
139147

140148

@@ -166,7 +174,7 @@ def get_branch_files():
166174

167175
def add_file_to_index(filename):
168176
'''Add file to current commit'''
169-
return _get_output(f'git add {filename}')
177+
return _get_output(f'git add {_escape_filename(filename)}')
170178

171179

172180
def get_commit_files():
@@ -244,13 +252,13 @@ def get_changed_lines(modified_file):
244252
if _is_github_event():
245253
if _is_pull_request():
246254
output = _get_output(
247-
f'git diff --unified=0 remotes/origin/{os.environ["GITHUB_BASE_REF"]}..remotes/origin/{os.environ["GITHUB_HEAD_REF"]} -- {modified_file}')
255+
f'git diff --unified=0 remotes/origin/{os.environ["GITHUB_BASE_REF"]}..remotes/origin/{os.environ["GITHUB_HEAD_REF"]} -- {_escape_filename(modified_file)}')
248256
else:
249257
output = _get_output(
250-
f'git diff --unified=0 HEAD~ {modified_file}')
258+
f'git diff --unified=0 HEAD~ {_escape_filename(modified_file)}')
251259
else:
252260
output = _get_output(
253-
f'git diff-index HEAD --unified=0 {modified_file}')
261+
f'git diff-index HEAD --unified=0 {_escape_filename(modified_file)}')
254262

255263
lines = []
256264
for line in output.splitlines():

0 commit comments

Comments
 (0)