5

I create a commit-msg hook in myrepo/.git/hooks.

#!/bin/sh
message=`cat $1`
c=`echo $message|grep -c 'fff'`
if[ $c -gt 0 ];then
  echo "Error"
  exit 1
fi
exit 0

When I try to commit like so, an error occurs and it blocks the commit.

$ git commit -m "reffrffffeffff fffeef"
Error

I then do the following:

$ cd myrepo
$ mkdir .hooks
$ mv .git/hooks/commit-msg .hooks/commit-msg
$ ln -s .hooks/commit-msg .git/hooks/commit-msg

and try to commit again with the same message. The commit succeeds. I guess I may have done something wrong in the above steps?

Can anyone tell me how to make a client-side hook, and have each developer get restrictions from this hook?

David Cain
  • 16,484
  • 14
  • 65
  • 75
Venus
  • 1,184
  • 2
  • 13
  • 32
  • If you need *all* developers to be restricted by the hook, then you probably want a `pre-receive` hook on the shared remote, not a `pre-commit` one. – Romain Jul 04 '12 at 10:17
  • @Romain: As you probably know, there is a difference- `pre-receive` can block a push with bad commit messages, but `commit-msg` blocks the commit. Another option is to have each developer install the `commit-msg` hook themselves (I elaborate in my answer). – David Cain Jul 04 '12 at 10:23
  • Also, ``var=`cat $1`;var2=`echo $var | grep -c ` `` is equivalent to ``var2=`grep -c $1` ``. And you can replace counting occurences with simply checking grep's exit status (`$?`)- a grep command will return 0 if the regex is found. – David Cain Jul 04 '12 at 10:49

1 Answers1

6

The problem in your steps:

You made a bad symbolic link. The commit-msg symbolic link points to .git/hooks/.hooks/commit-msg. Instead, try this:

$ cd myrepo
$ mkdir .hooks
$ cd .git/hooks
$ mv commit-msg ../../.hooks/commit-msg
$ ln -s !$ commit-msg  # lazy: '!$' expands to '../../.hooks/commit-msg'

How to restrict each developer's commit message

As you know, the commit-msg hook is a client-side hook. If you want each developer's commit messages to be rejected if they don't follow some scheme, you need to have the developers install the hook themselves. You can't maintain hooks as part of your repository, but keeping them in another Git repo is an option. (To be clear, you could keep them in your repository, but your developers would still need to make symlinks in the .git/hooks directory, as you have).

If you truly want to force developers to be restricted by a hook, look into server-side hooks. You could use pre-receive, for example, to check that all pushed commit messages obey your scheme.

Chapter 8.3 (Customizing Git - Git Hooks) of Pro Git is an excellent resource. There are some quality walk-throughs there to help you. You can also take a look at the example files included in .git/hooks for your repository.

brittlewis12
  • 113
  • 6
David Cain
  • 16,484
  • 14
  • 65
  • 75
  • hi,David.i create the hook pre-receive in .git/hooks. #!/bin/sh echo "$1" exit 1 .but i can still succeed to push . – Venus Jul 04 '12 at 11:06
  • git didn't execute the pre-receive hook even i have chmod+x to it – Venus Jul 04 '12 at 11:30
  • I highly recommend you read the example policy in [Ch. 7.4](http://git-scm.com/book/en/Customizing-Git-An-Example-Git-Enforced-Policy) of Pro Git. The `pre-receive` hook belongs in a bare remote repository, not in your local repository. This is likely your issue. – David Cain Jul 04 '12 at 11:36
  • sorry David,i can not access your link address. "the bare remote repository" means the repository on remote server in "repositories" folder? now i add the pre-receive hook in repositories/test.git/hooks,but still can not get the correct result – Venus Jul 04 '12 at 11:55
  • That's correct. And I'm sorry, without relevant error messages or such, I can't help you any further. I'd advise you to put some print statements in your hooks to ensure that they're at least running, then try to debug from there. – David Cain Jul 04 '12 at 12:02
  • my mistake,i forget to chmod +x pre-receive hook on origin repo. – Venus Jul 04 '12 at 12:44
  • yes,David .i have resolved it.and if use "commit-msg",every developer has to do symlink,it's a little troublesome. – Venus Jul 05 '12 at 04:05