Skip to content
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