56
56
# File types that need a terminating newline
57
57
TERMINATING_NEWLINE_EXTS = ['.c' , '.cpp' , '.h' , '.inl' ]
58
58
59
-
60
- def _get_output (command , cwd = '.' ):
61
- 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' )
62
61
63
62
64
63
def _is_github_event ():
@@ -91,7 +90,7 @@ def get_user():
91
90
if _is_github_event ():
92
91
return os .environ ['GITHUB_ACTOR' ]
93
92
else :
94
- output = _get_output ('git var GIT_AUTHOR_IDENT' )
93
+ output = _get_output ([ 'git' , ' var' , ' GIT_AUTHOR_IDENT'] )
95
94
match = re .match (r'^(.+) <' , output )
96
95
return match .group (1 )
97
96
@@ -104,7 +103,7 @@ def get_branch():
104
103
else :
105
104
return os .environ ['GITHUB_REF' ].split ('/' )[- 1 ]
106
105
else :
107
- return _get_output ('git branch' ).split ()[- 1 ]
106
+ return _get_output ([ 'git' , ' branch'] ).split ()[- 1 ]
108
107
109
108
110
109
def get_file_content_as_binary (filename ):
@@ -121,7 +120,7 @@ def get_file_content_as_binary(filename):
121
120
_skip (filename , 'File is not UTF-8 encoded' )
122
121
data = None
123
122
else :
124
- data = _get_output (f 'git show :{ filename } ' )
123
+ data = _get_output ([ 'git' , ' show' , f' :{ filename } '] )
125
124
return data
126
125
127
126
@@ -134,7 +133,7 @@ def get_text_file_content(filename):
134
133
if _is_github_event () or 'pytest' in sys .modules :
135
134
data = Path (filename ).read_text ()
136
135
else :
137
- data = _get_output (f 'git show :{ filename } ' )
136
+ data = _get_output ([ 'git' , ' show' , f' :{ filename } '] )
138
137
return data
139
138
140
139
@@ -147,7 +146,7 @@ def get_sha():
147
146
GITHUB_SHA cannot be used because in a pull request it gives the sha of the
148
147
fake merge commit.
149
148
'''
150
- return _get_output (f 'git rev-parse { get_branch ()} ' )
149
+ return _get_output ([ 'git' , ' rev-parse' , get_branch ()] )
151
150
152
151
153
152
def get_event ():
@@ -160,13 +159,12 @@ def get_event():
160
159
161
160
def get_branch_files ():
162
161
'''Get all files in branch'''
163
- branch = get_branch ()
164
- return _get_output (f'git ls-tree -r { branch } --name-only' ).splitlines ()
162
+ return _get_output (['git' ,'ls-tree' , '-r' , get_branch (),'--name-only' ]).splitlines ()
165
163
166
164
167
165
def add_file_to_index (filename ):
168
166
'''Add file to current commit'''
169
- return _get_output (f 'git add { filename } ' )
167
+ return _get_output ([ 'git' , ' add' , filename ] )
170
168
171
169
172
170
def get_commit_files ():
@@ -178,12 +176,15 @@ def get_commit_files():
178
176
179
177
'''
180
178
if _is_github_event ():
179
+ commands = ['git' ,'diff' ,'--ignore-submodules' ,'--name-status' ]
181
180
if _is_pull_request ():
182
- output = _get_output ( f'git diff --ignore-submodules --name-status remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} --')
181
+ commands += [ f' remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} ' , ' --']
183
182
else :
184
- output = _get_output ( 'git diff --ignore-submodules --name-status HEAD~.. --')
183
+ commands += [ ' HEAD~..' , ' --']
185
184
else :
186
- output = _get_output ('git diff-index --ignore-submodules HEAD --cached' )
185
+ commands = ['git' , 'diff-index' , '--ignore-submodules' , 'HEAD' , '--cached' ]
186
+
187
+ output = _get_output (commands )
187
188
result = defaultdict (list )
188
189
for line in output .splitlines ():
189
190
parts = line .split ()
@@ -242,15 +243,14 @@ def get_changed_lines(modified_file):
242
243
:returns: A list of line number (integers and ranges) of changed lines
243
244
'''
244
245
if _is_github_event ():
246
+ commands = ['git' ,'diff' ,'--unified=0' ]
245
247
if _is_pull_request ():
246
- 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 } ' )
248
+ commands += [f'remotes/origin/{ os .environ ["GITHUB_BASE_REF" ]} ..remotes/origin/{ os .environ ["GITHUB_HEAD_REF" ]} ' , '--' ,f'{ modified_file } ' ]
248
249
else :
249
- output = _get_output (
250
- f'git diff --unified=0 HEAD~ { modified_file } ' )
250
+ commands += ['HEAD~' , f'{ modified_file } ' ]
251
251
else :
252
- output = _get_output (
253
- f'git diff-index HEAD --unified=0 { modified_file } ' )
252
+ commands = [ f'git' , 'diff-index' , 'HEAD' , '--unified=0' , f' { modified_file } ' ]
253
+ output = _get_output ( commands )
254
254
255
255
lines = []
256
256
for line in output .splitlines ():
@@ -283,7 +283,7 @@ def _test(input, output):
283
283
def get_config_setting (setting ):
284
284
'''Get the value of a config setting'''
285
285
try :
286
- return _get_output (f 'git config --get { setting } ' ).strip ()
286
+ return _get_output ([ 'git' , ' config' , ' --get' , setting ] ).strip ()
287
287
except subprocess .CalledProcessError :
288
288
return None
289
289
@@ -460,20 +460,24 @@ class TestTrimTrailingWhitespace(unittest.TestCase):
460
460
def test_trim_trailing_whitespace (self ):
461
461
content = 'first line\n second line \n third line '
462
462
trimmed_content = 'first line\n second line\n third line'
463
- with NamedTemporaryFile () as tmp :
464
- Path (tmp .name ).write_text (content )
465
463
464
+ name = NamedTemporaryFile ().name
465
+ try :
466
+ Path (name ).write_text (content )
466
467
# Trailing whitespace found
467
- retval = trim_trailing_whitespace_in_file (tmp . name , True , True )
468
+ retval = trim_trailing_whitespace_in_file (name , True , True )
468
469
self .assertEqual (retval , 1 )
469
- self .assertEqual (Path (tmp . name ).read_text (), content )
470
+ self .assertEqual (Path (name ).read_text (), content )
470
471
471
472
# Now remove the trailing whitespace
472
- trim_trailing_whitespace_in_file (tmp . name , True , False , False )
473
+ trim_trailing_whitespace_in_file (name , True , False , False )
473
474
# Trailing whitespace no longer found
474
- self .assertEqual (Path (tmp . name ).read_text (), trimmed_content )
475
- retval = trim_trailing_whitespace_in_file (tmp . name , True , True )
475
+ self .assertEqual (Path (name ).read_text (), trimmed_content )
476
+ retval = trim_trailing_whitespace_in_file (name , True , True )
476
477
self .assertEqual (retval , 0 )
478
+ finally :
479
+ Path (name ).unlink ()
480
+
477
481
478
482
def test_decodeerror (self ):
479
483
# A text file that is not utf-8 encoded - report and skip
0 commit comments