From 3238bd4401bdec28805bbdfa382a860336e8a8b2 Mon Sep 17 00:00:00 2001 From: George Pickering <9803299+bigpick@users.noreply.github.com> Date: Mon, 6 May 2024 10:20:17 -0400 Subject: [PATCH] fix: File enumeration fails on older git binary Signed-off-by: George Pickering <9803299+bigpick@users.noreply.github.com> --- detect_secrets/core/baseline.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/detect_secrets/core/baseline.py b/detect_secrets/core/baseline.py index 41692f266..f56ee9fe1 100644 --- a/detect_secrets/core/baseline.py +++ b/detect_secrets/core/baseline.py @@ -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 + 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