Skip to content

Commit

Permalink
fix(prepush): fix stdin input single line and new tree scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Jun 23, 2021
1 parent c147551 commit b0416a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 7 additions & 3 deletions ggshield/hook_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def collect_from_stdin() -> Tuple[str, str]:
"""
Collect pre-commit variables from stdin.
"""
prepush_input = [line for line in sys.stdin.readlines()]
prepush_input = sys.stdin.read().split()
if len(prepush_input) < 4:
# Then it's either a tag or a deletion event
local_commit = EMPTY_SHA
Expand Down Expand Up @@ -89,6 +89,7 @@ def prepush_cmd(ctx: click.Context, prepush_args: List[str]) -> int: # pragma:
scan as a pre-push git hook.
"""
config = ctx.obj["config"]

local_commit, remote_commit = collect_from_precommit_env()
if local_commit is None or remote_commit is None:
local_commit, remote_commit = collect_from_stdin()
Expand All @@ -101,14 +102,17 @@ def prepush_cmd(ctx: click.Context, prepush_args: List[str]) -> int: # pragma:
click.echo("New tree event. Scanning all changes.")
before = EMPTY_TREE
after = local_commit
cmd_range = f"--max-count={MAX_PREPUSH_COMMITS+1} {EMPTY_TREE} {local_commit}"
else:
before = remote_commit
after = local_commit
cmd_range = f"{remote_commit}...{local_commit}"

commit_list = get_list_commit_SHA(cmd_range)

commit_list = get_list_commit_SHA(f"{before}...{after}")
if not commit_list:
click.echo(
"Unable to get commit range."
"Unable to get commit range.\n"
f" before: {before}\n"
f" after: {after}\n"
"Skipping pre-push hook\n"
Expand Down
34 changes: 33 additions & 1 deletion tests/test_hook_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def test_prepush_new_branch(
["-v", "scan", "pre-push"],
env={"PRE_COMMIT_FROM_REF": "a" * 40, "PRE_COMMIT_TO_REF": EMPTY_SHA},
)
get_list_mock.assert_called_once_with(EMPTY_TREE + "..." + "a" * 40)
get_list_mock.assert_called_once_with(
"--max-count=101 " + EMPTY_TREE + " " + "a" * 40
)
scan_commit_range_mock.assert_called_once()

assert "New tree event. Scanning all changes" in result.output
Expand Down Expand Up @@ -208,3 +210,33 @@ def test_prepush_deletion(
)
assert "Deletion event or nothing to scan.\n" in result.output
assert result.exit_code == 0


@patch("ggshield.hook_cmd.get_list_commit_SHA")
@patch("ggshield.hook_cmd.scan_commit_range")
@patch("ggshield.hook_cmd.check_git_dir")
def test_prepush_stdin_input_no_newline(
check_dir_mock: Mock,
scan_commit_range_mock: Mock,
get_list_mock: Mock,
cli_fs_runner: CliRunner,
):
"""
GIVEN 20 commits through stdin input
WHEN the command is run
THEN it should pass onto scan and return 0
"""
scan_commit_range_mock.return_value = 0
get_list_mock.return_value = ["a" for _ in range(20)]

result = cli_fs_runner.invoke(
cli,
["-v", "scan", "pre-push"],
input="refs/heads/main bfffbd925b1ce9298e6c56eb525b8d7211603c09 refs/heads/main 649061dcda8bff94e02adbaac70ca64cfb84bc78", # noqa: E501
)
get_list_mock.assert_called_once_with(
"649061dcda8bff94e02adbaac70ca64cfb84bc78...bfffbd925b1ce9298e6c56eb525b8d7211603c09" # noqa: E501
) # noqa: E501
scan_commit_range_mock.assert_called_once()
assert "Commits to scan: 20" in result.output
assert result.exit_code == 0

0 comments on commit b0416a5

Please sign in to comment.