Skip to content

Commit

Permalink
Support scanning multiple git repositories in one invocation.
Browse files Browse the repository at this point in the history
Also, bug fix to ignore files if no git tracked files are found.
  • Loading branch information
domanchi committed Jun 15, 2019
1 parent 0825875 commit 8aa90ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 13 additions & 6 deletions detect_secrets/core/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import re
import subprocess

from detect_secrets import util
from detect_secrets.core.log import get_logger
from detect_secrets.core.secrets_collection import SecretsCollection


log = get_logger(format_string='%(message)s')


Expand Down Expand Up @@ -37,13 +39,15 @@ def initialize(
exclude_lines=exclude_lines_regex,
)

files_to_scan = list()
files_to_scan = []
for element in path:
if os.path.isdir(element):
if scan_all_files:
files_to_scan.extend(_get_files_recursively(element))
else:
files_to_scan.extend(_get_git_tracked_files(element))
files = _get_git_tracked_files(element)
if files:
files_to_scan.extend(files)
elif os.path.isfile(element):
files_to_scan.append(element)
else:
Expand Down Expand Up @@ -268,13 +272,16 @@ def _get_git_tracked_files(rootdir='.'):
git_files = subprocess.check_output(
[
'git',
'-C', rootdir,
'ls-files',
rootdir,
],
stderr=fnull,
)

return set(git_files.decode('utf-8').split())
return set([
util.get_relative_path(rootdir, filename)
for filename in git_files.decode('utf-8').split()
])
except subprocess.CalledProcessError:
return None

Expand All @@ -284,8 +291,8 @@ def _get_files_recursively(rootdir):
This function allows us to do so.
"""
output = []
for root, dirs, files in os.walk(rootdir):
for root, _, files in os.walk(rootdir):
for filename in files:
output.append(os.path.join(root, filename))
output.append(util.get_relative_path(root, filename))

return output
7 changes: 7 additions & 0 deletions detect_secrets/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ def get_root_directory():
'../',
),
)


def get_relative_path(root, path):
"""Returns relative path, after following symlinks."""
return os.path.realpath(
os.path.join(root, path),
)[len(os.getcwd() + '/'):]

0 comments on commit 8aa90ee

Please sign in to comment.