Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-receive hook does not behave as expected in the event of a push force #437

Closed
pierrelalanne opened this issue Dec 1, 2022 · 1 comment · Fixed by #444
Closed

Pre-receive hook does not behave as expected in the event of a push force #437

pierrelalanne opened this issue Dec 1, 2022 · 1 comment · Fixed by #444
Labels
status:confirmed This issue has been reviewed and confirmed type:bug Something isn't working

Comments

@pierrelalanne
Copy link
Collaborator

pierrelalanne commented Dec 1, 2022

Environment

  • ggshield version: >= 1.13.6 (to have the latest pre-receive scan implementation version)
  • Operating system (Linux, macOS, Windows): Any
  • Operating system version: Any
  • Python version: Any

Describe the bug

When a user reworks a local branch (typically after rebasing the target branch) and makes a push force, the pre-receive hook will list all commits on the remote branch + all commits on the local branch being pushed force. This is not the expected behavior as the commits on the remote branch are already known to the remote, and hence should have already been handled by the pre-receive hook.

Steps to reproduce:

  1. Chose a git repository
  2. Make two separate clones : one will be called L (local) for convenience, and the other one R (remote).
  3. Mark R as being a remote in L : git remote add my_remote PATH_TO_R/.git
  4. Add a ggshield pre-receive hook in R : Create a PATH_TO_R/.git/hooks/pre-receive file containing :
#!/bin/sh
set -e
ggshield --verbose --debug secret scan pre-receive
  1. Create a branch pre-receive-test in L, add a commit (it has a sha : sha-first-commit), and push to R. GGShield should run and scan only the commit you added.
  2. Now, go in R, and add a new commit on the main branch, we'll call its sha sha-commit-main-branch. Go in L, pull changed from main, and rebase pre-receive-test on main. You now have one commit on the pre-receive-test branch, the same as before, but its sha changed because of the rebase. Let's call it sha-first-commit-after-rebase.
  3. In L, push force the changes to R : git push -f my_remote
  4. GGshield pre-receive hook will do the following (in the logs) : git rev-list --reverse sha-first-commit...sha-first-commit-after-rebase --max-count 51
  5. This results in scanning three commits (sha-first-commit, sha-commit-main-branch, sha-first-commit-after-rebase) instead of scanning only one : sha-first-commit-after-rebase.

Actual result:

Commits from both the remote branch and the local branch being pushed force are scanned

Expected result:

Only the commits from the branch being pushed are scanned.

@pierrelalanne pierrelalanne added type:bug Something isn't working status:new This issue needs to be reviewed labels Dec 1, 2022
@pierrelalanne
Copy link
Collaborator Author

Comments

This looks a lot like the issue we had a few weeks ago on pre-receive hook scanning all commits when pushing a new branch.
I believe we could apply the same solution : looking up the tip of the branch being pushed.

Also one consideration while having a look at the documentation :
Back when we fixed this pre-receive issue, did we consider leveraging the ^ notation in git rev-list command ? For instance : git rev-list ^sha-commit-a...sha-commit-b will list all commits reachable from sha-commit-b but will exclude all commit reachable from sha-commit-a.

@agateau-gg agateau-gg added status:confirmed This issue has been reviewed and confirmed and removed status:new This issue needs to be reviewed labels Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:confirmed This issue has been reviewed and confirmed type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants