问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/11326895/how-to-apply-client-side-hook-to-all-local-repositories-in-git