简体   繁体   English

如何将客户端挂钩应用到git中的所有本地存储库?

[英]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? 谁能告诉我如何制作客户端挂钩,并让每个开发人员都从该挂钩中获得限制吗?

The problem in your steps: 您的步骤中存在的问题:

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'

How to restrict each developer's commit message 如何限制每个开发人员的提交消息

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.

相关问题 如何部署客户端git挂钩? - How to deploy client-side git hooks? 如何在TFS中应用服务器端挂钩-Git存储库 - How to apply server side hooks in TFS - Git repositories 在Git中自动推送元数据(例如,通过标签/注释)(不使用客户端挂钩)? - Pushing metadata (say, via tags/notes) automatically in Git (w/o using a client-side hook)? 在创建Git客户端'commit-msg'挂钩时需要帮助 - Need assistance with creating a Git client-side 'commit-msg' hook Git:在合并操作期间从客户端挂钩获取目标和源分支名称 - Git : get destination and source branche name during merge operation from client-side hook 如何将新的 git 挂钩安装到所有现有(克隆)存储库? - How to install new git hook to all existing (cloned) repositories? 如何在 git 存储库中拆分 Angular 应用程序客户端/服务器端代码 - How to split angular app client / server side code in git repositories 从客户端JavaScript使用Git - Using Git from client-side javascript 如何在Twol本地Windows系统上配置git服务器和客户端存储库? - How to configure git server and client repositories on twol local windows systems? 如果用户名/电子邮件不正确,如何在客户端挂钩中拒绝提交? - How can I deny a commit, in a client-side hook, if the user name/email is incorrect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM