Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: File enumeration fails on older git binary #150

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,22 +361,30 @@ def _get_git_tracked_files(rootdir='.'):
:returns: filepaths to files which git currently tracks (locally)
"""
output = []

# git <1.8.5 https://github.com/git/git/commit/44e1e4d67d5148c245db362cc48c3cc6c2ec82ca
# doesn't support -C <path> and we can achieve the same using cwd arg to subproc
cmd = ['git', 'ls-files']
if not os.path.exists(rootdir) or not os.path.isdir(rootdir):
log.debug(f'Skipping {rootdir} bc dir doesn\'t exist or isn\'t a directory')
return []

try:
with open(os.devnull, 'w') as fnull:
git_files = subprocess.check_output(
[
'git',
'-C', rootdir,
'ls-files',
],
stderr=fnull,
)
git_files = subprocess.check_output(cmd, cwd=rootdir, 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
2 changes: 1 addition & 1 deletion tests/core/baseline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_error_when_getting_git_tracked_files(self, path):
'detect_secrets.core.baseline.subprocess.check_output',
(
SubprocessMock(
expected_input='git -C ./test_data/files ls-files',
expected_input='git ls-files',
should_throw_exception=True,
mocked_output='',
),
Expand Down
Loading