Skip to content

Commit

Permalink
feat(pre-receive): try to diff against HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
jguerreiro authored and Jguer committed Nov 2, 2021
1 parent 146d982 commit 53754e0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
35 changes: 19 additions & 16 deletions ggshield/pre_receive_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def prereceive_cmd(ctx: click.Context, web: bool, prereceive_args: List[str]) ->
"""
config = ctx.obj["config"]

breakglass = get_breakglass_option()
if breakglass:
if get_breakglass_option():
click.echo("SKIP: breakglass detected. Skipping GitGuardian pre-receive hook.")

return 0
Expand All @@ -93,28 +92,32 @@ def prereceive_cmd(ctx: click.Context, web: bool, prereceive_args: List[str]) ->
if len(args) < 3:
raise click.ClickException(f"Invalid input arguments: {args}")

oldref, newref, *_ = args
before, after, *_ = args
commit_list = []

if newref == EMPTY_SHA:
if after == EMPTY_SHA:
click.echo("Deletion event or nothing to scan.")
return 0

if oldref == EMPTY_SHA:
click.echo(
f"New tree event. Scanning last {config.max_commits_for_hook} commits."
if before == EMPTY_SHA:
before = "HEAD"
commit_list = get_list_commit_SHA(
f"--max-count={config.max_commits_for_hook+1} {before}...{after}"
)
before = EMPTY_TREE
after = newref
cmd_range = f"--max-count={config.max_commits_for_hook+1} {EMPTY_TREE} {after}"

if not commit_list:
before = EMPTY_TREE
click.echo(
f"New tree event. Scanning last {config.max_commits_for_hook} commits."
)
commit_list = get_list_commit_SHA(
f"--max-count={config.max_commits_for_hook+1} {EMPTY_TREE} {after}"
)
else:
before = oldref
after = newref
cmd_range = (
f"--max-count={config.max_commits_for_hook+1} {before}...{after}" # noqa
commit_list = get_list_commit_SHA(
f"--max-count={config.max_commits_for_hook+1} {before}...{after}"
)

commit_list = get_list_commit_SHA(cmd_range)

if not commit_list:
click.echo(
"Unable to get commit range.\n"
Expand Down
38 changes: 31 additions & 7 deletions tests/test_prereceive_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_changing_max_commit_hooks(
"""

scan_commit_range_mock.return_value = 0
get_list_mock.return_value = ["a" for _ in range(60)]
get_list_mock.side_effect = [[], ["a" for _ in range(60)]]

result = cli_fs_runner.invoke(
cli,
Expand All @@ -194,9 +194,34 @@ def test_changing_max_commit_hooks(
assert "New tree event. Scanning last 20 commits" in result.output
assert "Commits to scan: 20" in result.output
assert result.exit_code == 0
get_list_mock.assert_called_once_with(
f"--max-count=21 {EMPTY_TREE} { 'a' * 40}"
assert get_list_mock.call_count == 2
get_list_mock.assert_called_with(f"--max-count=21 {EMPTY_TREE} { 'a' * 40}")
scan_commit_range_mock.assert_called_once()

@patch("ggshield.pre_receive_cmd.get_list_commit_SHA")
@patch("ggshield.pre_receive_cmd.scan_commit_range")
def test_new_branch_diff_with_head_success(
self,
scan_commit_range_mock: Mock,
get_list_mock: Mock,
cli_fs_runner: CliRunner,
):
"""
GIVEN a ref creation event
WHEN the command is run and its able to diff with HEAD
THEN it should scan the rev-list presented
"""

scan_commit_range_mock.return_value = 0
get_list_mock.side_effect = [["a" for _ in range(60)]]

result = cli_fs_runner.invoke(
cli, ["-v", "scan", "pre-receive"], input=f"{EMPTY_SHA}\n{'a'*40}\nmain"
)

assert result.exit_code == 0
assert get_list_mock.call_count == 1
get_list_mock.assert_called_with(f"--max-count=51 HEAD...{ 'a' * 40}")
scan_commit_range_mock.assert_called_once()

@patch("ggshield.pre_receive_cmd.get_list_commit_SHA")
Expand All @@ -214,7 +239,7 @@ def test_stdin_input_creation(
"""

scan_commit_range_mock.return_value = 0
get_list_mock.return_value = ["a" for _ in range(60)]
get_list_mock.side_effect = [[], ["a" for _ in range(60)]]

result = cli_fs_runner.invoke(
cli, ["-v", "scan", "pre-receive"], input=f"{EMPTY_SHA}\n{'a'*40}\nmain"
Expand All @@ -223,9 +248,8 @@ def test_stdin_input_creation(
assert "New tree event. Scanning last 50 commits" in result.output
assert "Commits to scan: 50" in result.output
assert result.exit_code == 0
get_list_mock.assert_called_once_with(
f"--max-count=51 {EMPTY_TREE} { 'a' * 40}"
)
assert get_list_mock.call_count == 2
get_list_mock.assert_called_with(f"--max-count=51 {EMPTY_TREE} { 'a' * 40}")
scan_commit_range_mock.assert_called_once()

@patch("ggshield.pre_receive_cmd.get_list_commit_SHA")
Expand Down

0 comments on commit 53754e0

Please sign in to comment.