Create pre-push hook to lint/test











up vote
7
down vote

favorite
1












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










share|improve this question




















  • 2




    Github (the git hosting service) doesn't let you add arbitrary hooks to the hosted copies of your repos. Setting hooks on the local version of your repositories is certainly possible, but then the question isn't Github-specific :)
    – Gareth
    Sep 4 '13 at 15:19






  • 2




    So, the only way to add a hook is using the pre-defined ones by GitHub?
    – Lt.
    Sep 4 '13 at 15:44










  • Well first, be aware that pre-push is run on the machine doing the pushing, and that will never be Github. The 'push' event that Github offers a Web Hook for is actually triggered as a post-receive, as it says at the top of the page you linked to. So, it can only really be used for notifications
    – Gareth
    Sep 4 '13 at 15:51












  • related: stackoverflow.com/questions/31681746
    – tkruse
    Jul 26 at 10:11















up vote
7
down vote

favorite
1












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










share|improve this question




















  • 2




    Github (the git hosting service) doesn't let you add arbitrary hooks to the hosted copies of your repos. Setting hooks on the local version of your repositories is certainly possible, but then the question isn't Github-specific :)
    – Gareth
    Sep 4 '13 at 15:19






  • 2




    So, the only way to add a hook is using the pre-defined ones by GitHub?
    – Lt.
    Sep 4 '13 at 15:44










  • Well first, be aware that pre-push is run on the machine doing the pushing, and that will never be Github. The 'push' event that Github offers a Web Hook for is actually triggered as a post-receive, as it says at the top of the page you linked to. So, it can only really be used for notifications
    – Gareth
    Sep 4 '13 at 15:51












  • related: stackoverflow.com/questions/31681746
    – tkruse
    Jul 26 at 10:11













up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





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










share|improve this question















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







git hook






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 4 '13 at 15:22









Gareth

88.8k29136148




88.8k29136148










asked Sep 4 '13 at 15:08









Lt.

795826




795826








  • 2




    Github (the git hosting service) doesn't let you add arbitrary hooks to the hosted copies of your repos. Setting hooks on the local version of your repositories is certainly possible, but then the question isn't Github-specific :)
    – Gareth
    Sep 4 '13 at 15:19






  • 2




    So, the only way to add a hook is using the pre-defined ones by GitHub?
    – Lt.
    Sep 4 '13 at 15:44










  • Well first, be aware that pre-push is run on the machine doing the pushing, and that will never be Github. The 'push' event that Github offers a Web Hook for is actually triggered as a post-receive, as it says at the top of the page you linked to. So, it can only really be used for notifications
    – Gareth
    Sep 4 '13 at 15:51












  • related: stackoverflow.com/questions/31681746
    – tkruse
    Jul 26 at 10:11














  • 2




    Github (the git hosting service) doesn't let you add arbitrary hooks to the hosted copies of your repos. Setting hooks on the local version of your repositories is certainly possible, but then the question isn't Github-specific :)
    – Gareth
    Sep 4 '13 at 15:19






  • 2




    So, the only way to add a hook is using the pre-defined ones by GitHub?
    – Lt.
    Sep 4 '13 at 15:44










  • Well first, be aware that pre-push is run on the machine doing the pushing, and that will never be Github. The 'push' event that Github offers a Web Hook for is actually triggered as a post-receive, as it says at the top of the page you linked to. So, it can only really be used for notifications
    – Gareth
    Sep 4 '13 at 15:51












  • related: stackoverflow.com/questions/31681746
    – tkruse
    Jul 26 at 10:11








2




2




Github (the git hosting service) doesn't let you add arbitrary hooks to the hosted copies of your repos. Setting hooks on the local version of your repositories is certainly possible, but then the question isn't Github-specific :)
– Gareth
Sep 4 '13 at 15:19




Github (the git hosting service) doesn't let you add arbitrary hooks to the hosted copies of your repos. Setting hooks on the local version of your repositories is certainly possible, but then the question isn't Github-specific :)
– Gareth
Sep 4 '13 at 15:19




2




2




So, the only way to add a hook is using the pre-defined ones by GitHub?
– Lt.
Sep 4 '13 at 15:44




So, the only way to add a hook is using the pre-defined ones by GitHub?
– Lt.
Sep 4 '13 at 15:44












Well first, be aware that pre-push is run on the machine doing the pushing, and that will never be Github. The 'push' event that Github offers a Web Hook for is actually triggered as a post-receive, as it says at the top of the page you linked to. So, it can only really be used for notifications
– Gareth
Sep 4 '13 at 15:51






Well first, be aware that pre-push is run on the machine doing the pushing, and that will never be Github. The 'push' event that Github offers a Web Hook for is actually triggered as a post-receive, as it says at the top of the page you linked to. So, it can only really be used for notifications
– Gareth
Sep 4 '13 at 15:51














related: stackoverflow.com/questions/31681746
– tkruse
Jul 26 at 10:11




related: stackoverflow.com/questions/31681746
– tkruse
Jul 26 at 10:11












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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.






share|improve this answer

















  • 2




    why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 17:31












  • If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
    – RyanWilcox
    Nov 7 '13 at 18:38






  • 1




    if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 22:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18617472%2fcreate-pre-push-hook-to-lint-test%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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.






share|improve this answer

















  • 2




    why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 17:31












  • If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
    – RyanWilcox
    Nov 7 '13 at 18:38






  • 1




    if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 22:46















up vote
2
down vote



accepted










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.






share|improve this answer

















  • 2




    why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 17:31












  • If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
    – RyanWilcox
    Nov 7 '13 at 18:38






  • 1




    if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 22:46













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 5 '13 at 2:04









RyanWilcox

11.8k12545




11.8k12545








  • 2




    why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 17:31












  • If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
    – RyanWilcox
    Nov 7 '13 at 18:38






  • 1




    if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 22:46














  • 2




    why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 17:31












  • If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
    – RyanWilcox
    Nov 7 '13 at 18:38






  • 1




    if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
    – Mike 'Pomax' Kamermans
    Nov 7 '13 at 22:46








2




2




why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
– Mike 'Pomax' Kamermans
Nov 7 '13 at 17:31






why make this is a pre-commit instead of a pre-push hook? As a pre-push hook, it would give the option to deny a push -- git-scm.com/docs/githooks#_pre-push
– Mike 'Pomax' Kamermans
Nov 7 '13 at 17:31














If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
– RyanWilcox
Nov 7 '13 at 18:38




If pre-push happens on the client machine, then it might be a good idea. If it happens on the server, like pre-recieve, then see my problem about it hanging. But pre-push might be interesting: I know I do a bunch of work, then make a bunch of commits extracting the different pieces out. pre-push could avoid "Yes, I know the unit tests fail for this commit, gimme a second". Unless you want that feature (aka: your team uses git-bisect a lot, so unit tests have to pass)
– RyanWilcox
Nov 7 '13 at 18:38




1




1




if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
– Mike 'Pomax' Kamermans
Nov 7 '13 at 22:46




if you add it to your local .git/hooks dir, all hooks are local. pre-push will kick in just before the push is allowed through, and if whatever script(s) run return something other than 0, the push gets cancelled.
– Mike 'Pomax' Kamermans
Nov 7 '13 at 22:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18617472%2fcreate-pre-push-hook-to-lint-test%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...