[英]How to apply client-side hook to all local repositories in git?
I create a commit-msg hook in myrepo/.git/hooks
. 我在
myrepo/.git/hooks
创建了一个commit-msg 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? 谁能告诉我如何制作客户端挂钩,并让每个开发人员都从该挂钩中获得限制吗?
You made a bad symbolic link. 您建立了错误的符号链接。 The
commit-msg
symbolic link points to .git/hooks/.hooks/commit-msg
. commit-msg
符号链接指向.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'
As you know, the commit-msg
hook is a client-side hook. 如您所知,
commit-msg
挂钩是一个客户端挂钩。 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.
您不能在存储库中保留钩子,但是可以选择将其保留在另一个Git存储库中。 (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). (要明确,您可以将它们保留在存储库中,但是开发人员仍然需要像在现有的方法一样在
.git/hooks
目录中进行符号链接)。
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. 例如,您可以使用
pre-receive
来检查所有推送的提交消息是否符合您的方案。
Chapter 8.3 (Customizing Git - Git Hooks) of Pro Git is an excellent resource. Pro Git的第8.3章(自定义Git-Git Hooks)是一个很好的资源。 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. 您还可以查看存储库
.git/hooks
包含的示例文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.