Skip to content

Commit

Permalink
Fixed since_commit and tests (#219)
Browse files Browse the repository at this point in the history
* Fixed since_commit and tests

* Updated testing comment

Co-authored-by: Dylan Ayrey <dxa4481@rit.edu>
  • Loading branch information
dmrickert and dxa4481 committed Jan 30, 2021
1 parent cc44ee1 commit 288f35e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/truffleHog.egg-info/
**/__pycache__/
**/*.pyc
env/
13 changes: 7 additions & 6 deletions test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_unicode_expection(self):
self.fail("Unicode print error")

def test_return_correct_commit_hash(self):
# Start at commit d15627104d07846ac2914a976e8e347a663bbd9b, which
# Start at commit 202564cf776b402800a4aab8bb14fa4624888475, which
# is immediately followed by a secret inserting commit:
# https://github.com/dxa4481/truffleHog/commit/9ed54617547cfca783e0f81f8dc5c927e3d1e345
since_commit = 'd15627104d07846ac2914a976e8e347a663bbd9b'
commit_w_secret = '9ed54617547cfca783e0f81f8dc5c927e3d1e345'
cross_valdiating_commit_w_secret_comment = 'OH no a secret'
# https://github.com/dxa4481/truffleHog/commit/d15627104d07846ac2914a976e8e347a663bbd9b
since_commit = '202564cf776b402800a4aab8bb14fa4624888475'
commit_w_secret = 'd15627104d07846ac2914a976e8e347a663bbd9b'
cross_valdiating_commit_w_secret_comment = 'Oh no a secret file'

json_result = ''
if sys.version_info >= (3,):
Expand All @@ -54,7 +54,8 @@ def test_return_correct_commit_hash(self):

json_result_list = tmp_stdout.getvalue().split('\n')
results = [json.loads(r) for r in json_result_list if bool(r.strip())]
filtered_results = list(filter(lambda r: r['commitHash'] == commit_w_secret, results))
filtered_results = list(filter(lambda r: r['commitHash'] == commit_w_secret and r['branch'] == 'origin/master', results))

self.assertEqual(1, len(filtered_results))
self.assertEqual(commit_w_secret, filtered_results[0]['commitHash'])
# Additionally, we cross-validate the commit comment matches the expected comment
Expand Down
16 changes: 11 additions & 5 deletions truffleHog/truffleHog.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,7 @@ def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False,
commitHash = curr_commit.hexsha
if commitHash == since_commit:
since_commit_reached = True
if since_commit and since_commit_reached:
prev_commit = curr_commit
continue
break
# if not prev_commit, then curr_commit is the newest commit. And we have nothing to diff with.
# But we will diff the first commit with NULL_TREE here to check the oldest code.
# In this way, no commit will be missed.
Expand All @@ -364,8 +362,16 @@ def find_strings(git_url, since_commit=None, max_depth=1000000, printJson=False,
foundIssues = diff_worker(diff, curr_commit, prev_commit, branch_name, commitHash, custom_regexes, do_entropy, do_regex, printJson, surpress_output, path_inclusions, path_exclusions, allow)
output = handle_results(output, output_dir, foundIssues)
prev_commit = curr_commit
# Handling the first commit
diff = curr_commit.diff(NULL_TREE, create_patch=True)

# Check if since_commit was used to check which diff should be grabbed
if since_commit_reached:
# Handle when there's no prev_commit (used since_commit on the most recent commit)
if prev_commit is None:
continue
diff = prev_commit.diff(curr_commit, create_patch=True)
else:
diff = curr_commit.diff(NULL_TREE, create_patch=True)

foundIssues = diff_worker(diff, curr_commit, prev_commit, branch_name, commitHash, custom_regexes, do_entropy, do_regex, printJson, surpress_output, path_inclusions, path_exclusions, allow)
output = handle_results(output, output_dir, foundIssues)
output["project_path"] = project_path
Expand Down

0 comments on commit 288f35e

Please sign in to comment.