Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git remote/shared pre-commit hook

Tags:

git

githooks

With a one official repository as the remote, and multiple local repositories cloned from it, can a pre-commit hook be scripted on that main repository and be enforced on all clones of it?

like image 887
Samer Buna Avatar asked Sep 13 '10 18:09

Samer Buna


People also ask

How do you share a pre-commit hook?

In order to share the custom hooks that you created, with your team; you can create a separate folder inside your project and add all the hooks there first. In my case, I created a folder called . githooks in my project root directory.

Can I push git hooks to remote?

No. Hooks are per-repository and are never pushed.

How do you enforce a pre-commit hook in git?

If you want enforcement, use an update hook in the central repo. If the hook is doing per-commit verification, you can still provide a pre-commit hook; developers will likely adopt it voluntarily, so that they can find out right away when they've done something wrong, rather than waiting until they try to push.

What is git pre-commit hook?

The pre-commit script is executed every time you run git commit before Git asks the developer for a commit message or generates a commit object. You can use this hook to inspect the snapshot that is about to be committed.


2 Answers

I don't think so, as hooks are not cloned.
May be if that hook script is itself versioned, and then link to (symbolic link) in the clone servers (provided their OS support that link feature).

Or maybe if the hooks are part of a git template directory used for creating the clones (that would only ensure their presences in the clone repo, that would not guarantee they are actually used and executed).

But I don't think there is any "central" way to enforce a commit.


As Jefromi explains even more clearly in the comments (emphasis mine):

I think it really goes against the idea of a git repository to have enforced hooks distributed with the repo.
My clone is my repository. I should be able to use git on it however I like, including choosing whether or not to run hooks.
(And from a security standpoint, that'd be really kind of scary - no one should have the ability to force me to execute certain scripts whenever I run certain git commands.)

I agree with that comment, and have only seen ways to enforce rules applied locally, in a given specialized repo.
For instance, you wouldn't push to the central repo directly, but would first push to a QA repo which would accept your commit only if it follows certain rules. If it does, then the QA repo will push your commit to the central repo.

Another illustration directly derived from what I just mentioned would be "Serverless Continuous Integration with Git", a way to enforce locally private build that works before pushing them anywhere.

like image 189
VonC Avatar answered Oct 18 '22 20:10

VonC


You can not have the pre-commit hook forced on peoples local repositories, but in your central repo you can still run a pre-receive hook.

F. ex I needed to be sure the commit messages obeyed certain rules (for trac integration etc) so I used following pre-receive hook, which checks every commit messages being pushed to the central repository, and will deny the push if it is not welformed.

 #!/bin/sh while read rev_old rev_new ref do     MALFORMED="$(git rev-list --oneline $rev_old..$rev_new | egrep -v '#[0-9]+' |  awk '{print $1}' )"     if [ x"$MALFORMED" != x ]     then         echo Invallid commit message on $MALFORMED         exit 1     fi done  

for more info see f.ex https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

like image 32
Jens Timmerman Avatar answered Oct 18 '22 20:10

Jens Timmerman