tags:

views:

68

answers:

2

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?

A: 

can a pre-commit hook be scripted on that main repository and be enforced on all clones of it?

From githooks(5):

    pre-commit
      This hook is invoked by git commit, and can be bypassed with
      --no-verify option.

Since the hook can easily be bypassed, it seems the answer to your question is "no".

Also, since the .git/hooks directory is not cloned, there does not seem to be a mechanism to push it to the client.

bstpierre
+1  A: 

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.

VonC
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.)
Jefromi
@Jefromi: you know what's scarry? When I typed the comment, before submitting my edited answer, I began typing 'add...', and FireFox on my computer did propose me: "add Jefromi's comment". Not the first time I have been there, obviously ;)
VonC
Note to self: see also http://stackoverflow.com/questions/3209208/what-is-the-cleverest-use-of-source-repository-that-you-have-ever-seen/3209767#3209767
VonC