Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pre-push hook to lint/test

Tags:

git

hook

I was wondering what is the best way to create a pre-push hook (in a git repo) which does the following:

  • Run JSHint / JSLint
  • Run Unit and functional tests
  • If everything is ok then accept the update
  • Otherwise refuse the update
like image 848
Lt. Avatar asked Sep 04 '13 15:09

Lt.


People also ask

How do I make a pre received hook?

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.

How do you force a pre-commit hook?

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> .

What is pre Push hook?

A pre-push hook is a client-side git hook that runs right before a reference is pushed to a remote ( git push ).


1 Answers

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.

like image 128
RyanWilcox Avatar answered Oct 19 '22 03:10

RyanWilcox