5656# File types that need a terminating newline
5757TERMINATING_NEWLINE_EXTS = ['.c' , '.cpp' , '.h' , '.inl' ]
5858
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' )
6261
6362
6463def _is_github_event ():
@@ -91,7 +90,7 @@ def get_user():
9190 if _is_github_event ():
9291 return os .environ ['GITHUB_ACTOR' ]
9392 else :
94- output = _get_output ('git var GIT_AUTHOR_IDENT' )
93+ output = _get_output ([ 'git' , ' var' , ' GIT_AUTHOR_IDENT'] )
9594 match = re .match (r'^(.+) <' , output )
9695 return match .group (1 )
9796
@@ -104,7 +103,7 @@ def get_branch():
104103 else :
105104 return os .environ ['GITHUB_REF' ].split ('/' )[- 1 ]
106105 else :
107- return _get_output ('git branch' ).split ()[- 1 ]
106+ return _get_output ([ 'git' , ' branch'] ).split ()[- 1 ]
108107
109108
110109def get_file_content_as_binary (filename ):
@@ -121,7 +120,7 @@ def get_file_content_as_binary(filename):
121120 _skip (filename , 'File is not UTF-8 encoded' )
122121 data = None
123122 else :
124- data = _get_output (f 'git show :{ filename } ' )
123+ data = _get_output ([ 'git' , ' show' , f' :{ filename } '] )
125124 return data
126125
127126
@@ -134,7 +133,7 @@ def get_text_file_content(filename):
134133 if _is_github_event () or 'pytest' in sys .modules :
135134 data = Path (filename ).read_text ()
136135 else :
137- data = _get_output (f 'git show :{ filename } ' )
136+ data = _get_output ([ 'git' , ' show' , f' :{ filename } '] )
138137 return data
139138
140139
@@ -147,7 +146,7 @@ def get_sha():
147146 GITHUB_SHA cannot be used because in a pull request it gives the sha of the
148147 fake merge commit.
149148 '''
150- return _get_output (f 'git rev-parse { get_branch ()} ' )
149+ return _get_output ([ 'git' , ' rev-parse' , get_branch ()] )
151150
152151
153152def get_event ():
@@ -160,13 +159,12 @@ def get_event():
160159
161160def get_branch_files ():
162161 '''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 ()
165163
166164
167165def add_file_to_index (filename ):
168166 '''Add file to current commit'''
169- return _get_output (f 'git add { filename } ' )
167+ return _get_output ([ 'git' , ' add' , filename ] )
170168
171169
172170def get_commit_files ():
@@ -178,12 +176,15 @@ def get_commit_files():
178176
179177 '''
180178 if _is_github_event ():
179+ commands = ['git' ,'diff' ,'--ignore-submodules' ,'--name-status' ]
181180 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" ]} ' , ' --']
183182 else :
184- output = _get_output ( 'git diff --ignore-submodules --name-status HEAD~.. --')
183+ commands += [ ' HEAD~..' , ' --']
185184 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 )
187188 result = defaultdict (list )
188189 for line in output .splitlines ():
189190 parts = line .split ()
@@ -242,15 +243,14 @@ def get_changed_lines(modified_file):
242243 :returns: A list of line number (integers and ranges) of changed lines
243244 '''
244245 if _is_github_event ():
246+ commands = ['git' ,'diff' ,'--unified=0' ]
245247 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 } ' ]
248249 else :
249- output = _get_output (
250- f'git diff --unified=0 HEAD~ { modified_file } ' )
250+ commands += ['HEAD~' , f'{ modified_file } ' ]
251251 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 )
254254
255255 lines = []
256256 for line in output .splitlines ():
@@ -283,7 +283,7 @@ def _test(input, output):
283283def get_config_setting (setting ):
284284 '''Get the value of a config setting'''
285285 try :
286- return _get_output (f 'git config --get { setting } ' ).strip ()
286+ return _get_output ([ 'git' , ' config' , ' --get' , setting ] ).strip ()
287287 except subprocess .CalledProcessError :
288288 return None
289289
@@ -460,20 +460,24 @@ class TestTrimTrailingWhitespace(unittest.TestCase):
460460 def test_trim_trailing_whitespace (self ):
461461 content = 'first line\n second line \n third line '
462462 trimmed_content = 'first line\n second line\n third line'
463- with NamedTemporaryFile () as tmp :
464- Path (tmp .name ).write_text (content )
465463
464+ name = NamedTemporaryFile ().name
465+ try :
466+ Path (name ).write_text (content )
466467 # Trailing whitespace found
467- retval = trim_trailing_whitespace_in_file (tmp . name , True , True )
468+ retval = trim_trailing_whitespace_in_file (name , True , True )
468469 self .assertEqual (retval , 1 )
469- self .assertEqual (Path (tmp . name ).read_text (), content )
470+ self .assertEqual (Path (name ).read_text (), content )
470471
471472 # 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 )
473474 # 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 )
476477 self .assertEqual (retval , 0 )
478+ finally :
479+ Path (name ).unlink ()
480+
477481
478482 def test_decodeerror (self ):
479483 # A text file that is not utf-8 encoded - report and skip
0 commit comments