I was wondering what is the best way to create a pre-push hook (in a git repo) which does the following:
Adding a Pre-Receive HookNavigate to Admin → Script Pre Hooks. Click a heading to add a handler. Choose Custom script hook to use your own scripts to decide whether to allow the push or not.
pre-commit will now run on every commit. Every time you clone a project using pre-commit running pre-commit install should always be the first thing you do. If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> .
A pre-push hook is a client-side git hook that runs right before a reference is pushed to a remote ( git push ).
You could use a Git pre commit hook to do this. I've set up pre commit hooks to check for debug statements, etc.
Client side hooks like this belong in the .git/hooks folder. But since you can't commit anything in the .git repo into version control you're kind of stuck.
What you need to do then is to keep your shell command that checks correctness in some folder in your git repo, say a top level tools
directory.
Then "just" tell people to install it via:
chmod u+x tools/precommit-checks.sh
ln -s $PWD/tools/precommit-checks.sh .git/hooks/pre-commit
and, assuming that everyone installs it, you can have checking like you ask.
Probably a better way is to just catch this server side: have some kind of continuous integration server pulling the latest commits from your Github repo and checking the codebase.
No, it won't give you the "deny a push" capabilities you'd like.
Assuming you were to host your git repo yourself, there's also another wrinkle: I thought pre-receive hooks on the server would hang the clients for as long as the hook takes. (This is documented to be true for post-recieve hooks, so I'm guessing it's true here too). So if the CI tests take 2 minutes some developer is typing git push
and waiting 2 minutes for their console to do anything again.
So probably better to do post push analysis using a CI server or other quality tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With