21

Currently we are using SVN. I would like to start using GitHub, but one absolute requirement is that we will need to have precommit (premerge) validation of the code like we currently have. Does GitHub support precommithooks (premergehooks)?

We're a team of 5 developers. We made an agreement that all code (JavaScript) should pass JSLint-like validation. Voluntary validation has proven not to work because it's easily forgotten. How can we be sure that code that becomes available to the others is guaranteed to validate against JSLint (or similar)?

David Harkness
  • 35,992
  • 10
  • 112
  • 134
Corno
  • 5,448
  • 4
  • 25
  • 41
  • 4
    I am aware of that and I know that git supports precommithooks. so moving to git won't be an issue, but we won't be doing that unless we can use github. – Corno Dec 16 '11 at 03:54
  • the precommit hooks in git will run on your machine, if they stop the commit there is no chance of pushing non validated code to github – smitec Dec 16 '11 at 03:56
  • This seems strange to me. It is the github server that needs to do the validation as well for sure, otherwise there's always a way to bypass the precommithook – Corno Dec 16 '11 at 04:02
  • // , Do you trust your developers not to intentionally bypass the pre-commit hook? – Nathan Basanese Mar 31 '16 at 18:41
  • One solution may be to have a webhook post to a listener that gets the commit information, validates it and if it fails removes the commit from github... then shames the committer in slack for :D – Sam Jul 15 '16 at 09:53

5 Answers5

16

The concept I was looking for was the prereceive hook

Community
  • 1
  • 1
Corno
  • 5,448
  • 4
  • 25
  • 41
2

I think this article describes a very good workflow that could be a basis for automation:

http://scottchacon.com/2011/08/31/github-flow.html

The main idea is that you use pull requests as mentioned above, but you can also have a service that can use the github api to fetch or pull the branch making the request, merge, test, validate then push to the target branch.

Cheruvim
  • 127
  • 1
  • 5
2

I don't believe github supports pre-commit hooks. However, the git core does. You could set up the pre-commit hooks locally, or apply them as a test before merging branches into your main github.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • For us, a requirement is that you cannot commit without your code being validated, so testing before merging is not a solution unfortunately – Corno Dec 16 '11 at 03:56
  • Perhaps you should move away from github then. It's not difficult to set up a simple git server, and then you can set up hooks exactly the way you want. – Chris Eberle Dec 16 '11 at 03:59
  • 1
    The whole point of git is to allow for commits to happen easily - indeed, committing is something that happens on your local machine; no amount of hooks on the remote server can stop you from locally committing crap. Perhaps you should change your definition of 'commit' to 'merge'? Or if you trust your users put a copy of the hooks on every client working copy. – bdonlan Dec 16 '11 at 04:00
  • 1
    Maybe I'm using the wrong word indeed. I have a SVN-background and there the word would be commit. Should I be looking for 'pre-merge-hook' instead? – Corno Dec 16 '11 at 04:05
  • In git, the commit happens on the local machine, then you 'push' to a server. You can apply hooks to this push, which is a type of merge (a 'fast-forward' merge). Github does not support this specific feature, however. – bdonlan Dec 16 '11 at 04:06
  • It's worth noting an alternate model is to allow devs to push to their own individual (but visible to the team) repositories, and merge into a central repo as a separate step, probably after some code review or something. – bdonlan Dec 16 '11 at 04:08
  • You could include the pre-commit hooks in your github repo, then in your makefile, npm install script, in a standalone script, or whatever, copy the hooks into the `.git directory`. That is you could include a script like this `cp hooks/pre-commit .git/hooks/pre-commit`. – Max Heiber Mar 18 '17 at 20:55
2

I think you're missing something fundamental about git. It's not a centralized model (well ok, it can be, but if you're going to use it this way then github is probably the wrong approach). If you're using github, the right way to do this is:

  1. Host your main repo
  2. Have your developers each create their own fork
  3. Let them happily hack away, committing and pushing to their heart's content
  4. When they think a feature is ready, they send a pull request to you (the maintainer) which you yourself verify on the side to ensure stability. Then you merge / rebase their changes into the main repo.

Naturally there are many ways to skin a cat. But when you're talking about "real git" (the kind employed by the open source community), the centralized "check-it-in-and-it-damned-well-better-work" model is kind of difficult, especially when it comes to larger projects.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • 1
    I might indeed. I should have a look at the 'pull request' principle which I completely missed. – Corno Dec 16 '11 at 04:09
  • It's perfectly reasonable to want to prevent users from pushing to the repo that you deploy from whenever the tests fail if you're doing continuous deployment. – kranzky Jul 17 '12 at 08:49
  • @kranzky Thanks for the downvote, although you kind of missed the entire point of this answer. – Chris Eberle Jul 17 '12 at 16:28
  • @Chris I just did the usual "google for the answer, click on the first result, realise that the answer is preachy and not useful to me, use the feedback mechanism to update the page for others that may find themselves in the same boat". I did add a comment as SO suggested. Did I miss something? Did I abuse the downvote? – kranzky Jul 17 '12 at 18:14
  • 2
    The workflow in this answer has no built in security or guarantees. Hooks in git can provide invariants and guarantees. That is universally considered to be a good thing. – user239558 Apr 08 '14 at 06:22
1

No, GitHub doesn't support pre-commit hooks. How would that even work? Committing happens on your computer, do you really want to allow GitHub to run arbitrary code on your machine?

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 3
    It would be okay to merge as long as you are working locally, but when you merge with the main repository, then the code should be validated. I can imagine that this could be done with webhooks for example. – Corno Dec 16 '11 at 04:00
  • // , Perhaps you could explain how the question could be made more intelligible? Consider that many people hitting this will have a background in SVN. – Nathan Basanese Mar 31 '16 at 18:44