Skip to content

Commit c15b62f

Browse files
committed
Adding feature to scan a particular branch
1 parent 1760c0a commit c15b62f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

test_all.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unittest
22
import os
33
from truffleHog import truffleHog
4+
from mock import patch
5+
from mock import MagicMock
46

57

68
class TestStringMethods(unittest.TestCase):
@@ -22,5 +24,13 @@ def test_unicode_expection(self):
2224
except UnicodeEncodeError:
2325
self.fail("Unicode print error")
2426

27+
@patch('truffleHog.truffleHog.clone_git_repo')
28+
@patch('truffleHog.truffleHog.Repo')
29+
def test_branch(self, repo_const_mock, clone_git_repo):
30+
repo = MagicMock()
31+
repo_const_mock.return_value = repo
32+
truffleHog.find_strings("test_repo", branch="testbranch")
33+
repo.remotes.origin.fetch.assert_called_once_with("testbranch")
34+
2535
if __name__ == '__main__':
2636
unittest.main()

truffleHog/truffleHog.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def main():
2727
parser.add_argument("--entropy", dest="do_entropy", help="Enable entropy checks")
2828
parser.add_argument("--since_commit", dest="since_commit", help="Only scan from a given commit hash")
2929
parser.add_argument("--max_depth", dest="max_depth", help="The max commit depth to go back when searching for secrets")
30+
parser.add_argument("--branch", dest="branch", help="Name of the branch to be scanned")
3031
parser.add_argument('git_url', type=str, help='URL for secret searching')
3132
parser.set_defaults(regex=False)
3233
parser.set_defaults(rules={})
3334
parser.set_defaults(max_depth=1000000)
3435
parser.set_defaults(since_commit=None)
3536
parser.set_defaults(entropy=True)
37+
parser.set_defaults(branch=None)
3638
args = parser.parse_args()
3739
rules = {}
3840
if args.rules:
@@ -48,7 +50,7 @@ def main():
4850
for regex in rules:
4951
regexes[regex] = rules[regex]
5052
do_entropy = str2bool(args.do_entropy)
51-
output = find_strings(args.git_url, args.since_commit, args.max_depth, args.output_json, args.do_regex, do_entropy, surpress_output=False)
53+
output = find_strings(args.git_url, args.since_commit, args.max_depth, args.output_json, args.do_regex, do_entropy, surpress_output=False, branch=args.branch)
5254
project_path = output["project_path"]
5355
shutil.rmtree(project_path, onerror=del_rw)
5456
if output["foundIssues"]:
@@ -240,14 +242,19 @@ def handle_results(output, output_dir, foundIssues):
240242
output["foundIssues"].append(result_path)
241243
return output
242244

243-
def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False, do_regex=False, do_entropy=True, surpress_output=True, custom_regexes={}):
245+
def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False, do_regex=False, do_entropy=True, surpress_output=True, custom_regexes={}, branch=None):
244246
output = {"foundIssues": []}
245247
project_path = clone_git_repo(git_url)
246248
repo = Repo(project_path)
247249
already_searched = set()
248250
output_dir = tempfile.mkdtemp()
249251

250-
for remote_branch in repo.remotes.origin.fetch():
252+
if branch:
253+
branches = repo.remotes.origin.fetch(branch)
254+
else:
255+
branches = repo.remotes.origin.fetch()
256+
257+
for remote_branch in branches:
251258
since_commit_reached = False
252259
branch_name = remote_branch.name
253260
prev_commit = None

0 commit comments

Comments
 (0)