Skip to content

Commit fbf245f

Browse files
authored
Merge pull request #193 from Yelp/support-scanning-multiple-git-repos
Support scanning multiple git repositories in one invocation
2 parents 0825875 + 8aa90ee commit fbf245f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Diff for: detect_secrets/core/baseline.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import re
66
import subprocess
77

8+
from detect_secrets import util
89
from detect_secrets.core.log import get_logger
910
from detect_secrets.core.secrets_collection import SecretsCollection
1011

12+
1113
log = get_logger(format_string='%(message)s')
1214

1315

@@ -37,13 +39,15 @@ def initialize(
3739
exclude_lines=exclude_lines_regex,
3840
)
3941

40-
files_to_scan = list()
42+
files_to_scan = []
4143
for element in path:
4244
if os.path.isdir(element):
4345
if scan_all_files:
4446
files_to_scan.extend(_get_files_recursively(element))
4547
else:
46-
files_to_scan.extend(_get_git_tracked_files(element))
48+
files = _get_git_tracked_files(element)
49+
if files:
50+
files_to_scan.extend(files)
4751
elif os.path.isfile(element):
4852
files_to_scan.append(element)
4953
else:
@@ -268,13 +272,16 @@ def _get_git_tracked_files(rootdir='.'):
268272
git_files = subprocess.check_output(
269273
[
270274
'git',
275+
'-C', rootdir,
271276
'ls-files',
272-
rootdir,
273277
],
274278
stderr=fnull,
275279
)
276280

277-
return set(git_files.decode('utf-8').split())
281+
return set([
282+
util.get_relative_path(rootdir, filename)
283+
for filename in git_files.decode('utf-8').split()
284+
])
278285
except subprocess.CalledProcessError:
279286
return None
280287

@@ -284,8 +291,8 @@ def _get_files_recursively(rootdir):
284291
This function allows us to do so.
285292
"""
286293
output = []
287-
for root, dirs, files in os.walk(rootdir):
294+
for root, _, files in os.walk(rootdir):
288295
for filename in files:
289-
output.append(os.path.join(root, filename))
296+
output.append(util.get_relative_path(root, filename))
290297

291298
return output

Diff for: detect_secrets/util.py

+7
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ def get_root_directory():
88
'../',
99
),
1010
)
11+
12+
13+
def get_relative_path(root, path):
14+
"""Returns relative path, after following symlinks."""
15+
return os.path.realpath(
16+
os.path.join(root, path),
17+
)[len(os.getcwd() + '/'):]

0 commit comments

Comments
 (0)