diff --git a/detect_secrets/core/baseline.py b/detect_secrets/core/baseline.py index 41692f266..f32ce48ca 100644 --- a/detect_secrets/core/baseline.py +++ b/detect_secrets/core/baseline.py @@ -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 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 diff --git a/tests/core/baseline_test.py b/tests/core/baseline_test.py index efd7c7ea2..bb778e0fb 100644 --- a/tests/core/baseline_test.py +++ b/tests/core/baseline_test.py @@ -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='', ),