Skip to content

Commit

Permalink
fix: File enumeration fails on older git binary
Browse files Browse the repository at this point in the history
Signed-off-by: George Pickering <[email protected]>
  • Loading branch information
bigpick committed May 6, 2024
1 parent cb803c6 commit 3238bd4
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,22 +361,38 @@ def _get_git_tracked_files(rootdir='.'):
:returns: filepaths to files which git currently tracks (locally)
"""
output = []
git_dir_opts = []
try:
with open(os.devnull, 'w') as fnull:
git_files = subprocess.check_output(
# git <1.8.5 https://github.com/git/git/commit/44e1e4d67d5148c245db362cc48c3cc6c2ec82ca
# doesn't support -C <path>
git_major, git_minor, git_patch, _ = subprocess.check_output(
[
'git',
'-C', rootdir,
'ls-files',
'--version',
],
stderr=fnull,
).decode('utf-8').split()[-1].split(".")

if int(git_major) == 1 and int(git_minor) <= 8 and int(git_patch) < 5:
git_dir_opts = ["--git-dir", os.path.join(rootdir, ".git")]
else:
git_dir_opts = ["-C", rootdir]

git_files = subprocess.check_output(
['git' ] + git_dir_opts + ['ls-files'],
stderr=fnull,
)
for filename in git_files.decode('utf-8').split():
relative_path = util.get_relative_path_if_in_cwd(rootdir, filename)
if relative_path:
output.append(relative_path)
except subprocess.CalledProcessError:

except subprocess.CalledProcessError as err:
log.error('detect-secrets: Encountered error trying to list git tracked '+
f'files for dir "{rootdir}": {str(err)}')
pass

return output


Expand Down

0 comments on commit 3238bd4

Please sign in to comment.