56
56
# File types that need a terminating newline
57
57
TERMINATING_NEWLINE_EXTS = ['.c' , '.cpp' , '.h' , '.inl' ]
58
58
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
+
59
67
60
68
def _get_output (command , cwd = '.' ):
61
69
return subprocess .check_output (command , shell = True , cwd = cwd ).decode (errors = 'replace' )
@@ -121,7 +129,7 @@ def get_file_content_as_binary(filename):
121
129
_skip (filename , 'File is not UTF-8 encoded' )
122
130
data = None
123
131
else :
124
- data = _get_output (f'git show :{ filename } ' )
132
+ data = _get_output (f'git show :{ _escape_filename ( filename ) } ' )
125
133
return data
126
134
127
135
@@ -134,7 +142,7 @@ def get_text_file_content(filename):
134
142
if _is_github_event () or 'pytest' in sys .modules :
135
143
data = Path (filename ).read_text ()
136
144
else :
137
- data = _get_output (f'git show :{ filename } ' )
145
+ data = _get_output (f'git show :{ _escape_filename ( filename ) } ' )
138
146
return data
139
147
140
148
@@ -166,7 +174,7 @@ def get_branch_files():
166
174
167
175
def add_file_to_index (filename ):
168
176
'''Add file to current commit'''
169
- return _get_output (f'git add { filename } ' )
177
+ return _get_output (f'git add { _escape_filename ( filename ) } ' )
170
178
171
179
172
180
def get_commit_files ():
@@ -244,13 +252,13 @@ def get_changed_lines(modified_file):
244
252
if _is_github_event ():
245
253
if _is_pull_request ():
246
254
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 ) } ' )
248
256
else :
249
257
output = _get_output (
250
- f'git diff --unified=0 HEAD~ { modified_file } ' )
258
+ f'git diff --unified=0 HEAD~ { _escape_filename ( modified_file ) } ' )
251
259
else :
252
260
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 ) } ' )
254
262
255
263
lines = []
256
264
for line in output .splitlines ():
0 commit comments