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
- ''' Lambda function to add in back-slashes to escape special chars as compiled in esc_re above
62
- which makes filenames work with subprocess commands
63
- '''
64
- return '\\ ' + match .group (0 )
65
-
66
- def _escape_filename (filename ):
67
- ''' Return an escaped filename - for example fi(1)le.txt would be changed to fi\\ (1\\ )le.txt
68
- '''
69
- return _esc_re .sub (_esc_char , filename )
70
-
71
-
72
- def _get_output (command , cwd = '.' ):
73
- return subprocess .check_output (command , shell = True , cwd = cwd ).decode (errors = 'replace' )
59
+ def _get_output (command_list , cwd = '.' ):
60
+ return subprocess .check_output (command_list , cwd = cwd ).decode (errors = 'replace' )
74
61
75
62
76
63
def _is_github_event ():
@@ -103,7 +90,7 @@ def get_user():
103
90
if _is_github_event ():
104
91
return os .environ ['GITHUB_ACTOR' ]
105
92
else :
106
- output = _get_output ('git var GIT_AUTHOR_IDENT' )
93
+ output = _get_output ([ 'git' , ' var' , ' GIT_AUTHOR_IDENT'] )
107
94
match = re .match (r'^(.+) <' , output )
108
95
return match .group (1 )
109
96
@@ -116,7 +103,7 @@ def get_branch():
116
103
else :
117
104
return os .environ ['GITHUB_REF' ].split ('/' )[- 1 ]
118
105
else :
119
- return _get_output ('git branch' ).split ()[- 1 ]
106
+ return _get_output ([ 'git' , ' branch'] ).split ()[- 1 ]
120
107
121
108
122
109
def get_file_content_as_binary (filename ):
@@ -133,7 +120,7 @@ def get_file_content_as_binary(filename):
133
120
_skip (filename , 'File is not UTF-8 encoded' )
134
121
data = None
135
122
else :
136
- data = _get_output (f 'git show : { _escape_filename ( filename ) } ' )
123
+ data = _get_output ([ 'git' , ' show' , f': { filename } ' ] )
137
124
return data
138
125
139
126
@@ -146,7 +133,7 @@ def get_text_file_content(filename):
146
133
if _is_github_event () or 'pytest' in sys .modules :
147
134
data = Path (filename ).read_text ()
148
135
else :
149
- data = _get_output (f 'git show : { _escape_filename ( filename ) } ' )
136
+ data = _get_output ('git' , ' show' , f': { filename } ' )
150
137
return data
151
138
152
139
@@ -159,7 +146,7 @@ def get_sha():
159
146
GITHUB_SHA cannot be used because in a pull request it gives the sha of the
160
147
fake merge commit.
161
148
'''
162
- return _get_output (f 'git rev-parse { get_branch ()} ' )
149
+ return _get_output ([ 'git' , ' rev-parse' , f' { get_branch ()} '] )
163
150
164
151
165
152
def get_event ():
@@ -173,12 +160,12 @@ def get_event():
173
160
def get_branch_files ():
174
161
'''Get all files in branch'''
175
162
branch = get_branch ()
176
- return _get_output (f 'git ls-tree -r { branch } --name-only' ).splitlines ()
163
+ return _get_output ([ 'git' , ' ls-tree' , '-r' , f' { branch } ' , ' --name-only'] ).splitlines ()
177
164
178
165
179
166
def add_file_to_index (filename ):
180
167
'''Add file to current commit'''
181
- return _get_output (f 'git add { _escape_filename ( filename ) } ' )
168
+ return _get_output ([ 'git' , ' add' , f' { filename } ' ] )
182
169
183
170
184
171
def get_commit_files ():
@@ -190,12 +177,15 @@ def get_commit_files():
190
177
191
178
'''
192
179
if _is_github_event ():
180
+ commands = ['git' ,'diff' ,'--ignore-submodules' ,'--name-status' ]
193
181
if _is_pull_request ():
194
- output = _get_output ( f'git diff --ignore-submodules --name-status remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} --')
182
+ commands += [ f' remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} ' , ' --']
195
183
else :
196
- output = _get_output ( 'git diff --ignore-submodules --name-status HEAD~.. --')
184
+ commands += [ ' HEAD~..' , ' --']
197
185
else :
198
- output = _get_output ('git diff-index --ignore-submodules HEAD --cached' )
186
+ commands = ['git' , 'diff-index' , '--ignore-submodules' , 'HEAD' , '--cached' ]
187
+
188
+ output = _get_output (commands )
199
189
result = defaultdict (list )
200
190
for line in output .splitlines ():
201
191
parts = line .split ()
@@ -254,15 +244,14 @@ def get_changed_lines(modified_file):
254
244
:returns: A list of line number (integers and ranges) of changed lines
255
245
'''
256
246
if _is_github_event ():
247
+ commands = ['git' ,'diff' ,'--unified=0' ]
257
248
if _is_pull_request ():
258
- output = _get_output (
259
- f'git diff --unified=0 remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} -- { _escape_filename (modified_file )} ' )
249
+ commands += [f'remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} ' , '--' ,f'{ modified_file } ' ]
260
250
else :
261
- output = _get_output (
262
- f'git diff --unified=0 HEAD~ { _escape_filename (modified_file )} ' )
251
+ commands += ['HEAD~' , f'{ modified_file } ' ]
263
252
else :
264
- output = _get_output (
265
- f'git diff-index HEAD --unified=0 { _escape_filename ( modified_file ) } ' )
253
+ commands = [ f'git' , 'diff-index' , 'HEAD' , '--unified=0' , f' { modified_file } ' ]
254
+ output = _get_output ( commands )
266
255
267
256
lines = []
268
257
for line in output .splitlines ():
@@ -295,7 +284,7 @@ def _test(input, output):
295
284
def get_config_setting (setting ):
296
285
'''Get the value of a config setting'''
297
286
try :
298
- return _get_output (f 'git config --get { setting } ' ).strip ()
287
+ return _get_output ([ 'git' , ' config' , ' --get' , f' { setting } '] ).strip ()
299
288
except subprocess .CalledProcessError :
300
289
return None
301
290
@@ -472,20 +461,24 @@ class TestTrimTrailingWhitespace(unittest.TestCase):
472
461
def test_trim_trailing_whitespace (self ):
473
462
content = 'first line\n second line \n third line '
474
463
trimmed_content = 'first line\n second line\n third line'
475
- with NamedTemporaryFile () as tmp :
476
- Path (tmp .name ).write_text (content )
477
-
478
- # Trailing whitespace found
479
- retval = trim_trailing_whitespace_in_file (tmp .name , True , True )
480
- self .assertEqual (retval , 1 )
481
- self .assertEqual (Path (tmp .name ).read_text (), content )
482
-
483
- # Now remove the trailing whitespace
484
- trim_trailing_whitespace_in_file (tmp .name , True , False , False )
485
- # Trailing whitespace no longer found
486
- self .assertEqual (Path (tmp .name ).read_text (), trimmed_content )
487
- retval = trim_trailing_whitespace_in_file (tmp .name , True , True )
488
- self .assertEqual (retval , 0 )
464
+ name = NamedTemporaryFile ().name
465
+ Path (name ).write_text (content )
466
+ # Trailing whitespace found
467
+ retval = trim_trailing_whitespace_in_file (name , True , True )
468
+ self .assertEqual (retval , 1 )
469
+ self .assertEqual (Path (name ).read_text (), content )
470
+
471
+ # Now remove the trailing whitespace
472
+ trim_trailing_whitespace_in_file (name , True , False , False )
473
+ # Trailing whitespace no longer found
474
+ self .assertEqual (Path (name ).read_text (), trimmed_content )
475
+ retval = trim_trailing_whitespace_in_file (name , True , True )
476
+ self .assertEqual (retval , 0 )
477
+
478
+ try :
479
+ Path (name ).unlink (name )
480
+ except Exception as e :
481
+ pass
489
482
490
483
def test_decodeerror (self ):
491
484
# A text file that is not utf-8 encoded - report and skip
0 commit comments