Skip to content

Commit dcd736a

Browse files
authored
Merge pull request #27 from ccdc-opensource/no_jira_sanitise_filenames
This commit switches from using Shell=True in running commands inside the githooks to avoid problems with filenames containing certain characters that would need escaping otherwise.
2 parents 52b3022 + 6a5aa0e commit dcd736a

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

.github/workflows/quality_check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
- uses: actions/checkout@v3
88
- uses: actions/setup-python@v4
99
with:
10-
python-version: "3.7"
10+
python-version: "3.11"
1111
- name: Install dependencies
1212
run: |
1313
python -m pip install --upgrade pip

.github/workflows/status_check.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
fetch-depth: 0
1414
- uses: actions/setup-python@v4
1515
with:
16-
python-version: "3.7"
16+
python-version: "3.11"
1717
- name: Get the commit message
1818
run: |
1919
echo 'commit_message<<EOF' >> $GITHUB_ENV

main/githooks.py

+32-28
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@
5656
# File types that need a terminating newline
5757
TERMINATING_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

6463
def _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

110109
def 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

153152
def get_event():
@@ -160,13 +159,12 @@ def get_event():
160159

161160
def 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

167165
def 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

172170
def 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):
283283
def 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\nsecond line \nthird line '
462462
trimmed_content = 'first line\nsecond line\nthird 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

Comments
 (0)