Git 钩子脚本可以与存储库一起管理吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/427207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Git hook scripts be managed along with the repository?
提问by Pat Notz
We'd like to make a few basic hook scripts that we can all share -- for things like pre-formatting commit messages. Git has hook scripts for that that are normally stored under <project>/.git/hooks/
. However, those scripts are not propagated when people do a clone and they are not version controlled.
我们想制作一些我们都可以共享的基本钩子脚本——用于诸如预格式化提交消息之类的事情。Git 具有通常存储在<project>/.git/hooks/
. 但是,当人们进行克隆时,这些脚本不会传播,并且它们不受版本控制。
Is there a good way to help everyone get the right hook scripts? Can I just make those hook scripts point to version controlled scripts in my repo?
有没有什么好方法可以帮助大家获得正确的钩子脚本?我可以让这些钩子脚本指向我的 repo 中的版本控制脚本吗?
采纳答案by mipadi
Theoretically, you could create a hooks
directory (or whatever name you prefer) in your project directory with all the scripts, and then symlink them in .git/hooks
. Of course, each person who cloned the repo would have to set up these symlinks (although you could get really fancy and have a deploy script that the cloner could run to set them up semi-automatically).
理论上,您可以hooks
在包含所有脚本的项目目录中创建一个目录(或您喜欢的任何名称),然后将它们符号链接到.git/hooks
. 当然,每个克隆 repo 的人都必须设置这些符号链接(尽管您可能会非常喜欢并拥有一个部署脚本,克隆程序可以运行该脚本以半自动地设置它们)。
To do the symlink on *nix, all you need to do is:
要在 *nix 上执行符号链接,您需要做的就是:
root="$(pwd)"
ln -s "$root/hooks" "$root/.git/hooks"
use ln -sf
if you're ready to overwrite what's in .git/hooks
使用ln -sf
,如果你准备覆盖什么在.git/hooks
回答by Max Shenfield
In Git 2.9, the
configuration option core.hooksPath
specifies a custom hooks directory.
在Git 2.9 中,配置选项core.hooksPath
指定自定义钩子目录。
Move your hooks to a hooks
tracked directory in your repository. Then, configure each instanceof the repository to use the tracked hooks
instead of $GIT_DIR/hooks
:
将钩子移动到hooks
存储库中的跟踪目录。然后,配置存储库的每个实例以使用被跟踪的hooks
而不是$GIT_DIR/hooks
:
git config core.hooksPath hooks
In general, the path may be absolute, or relative to the directory where the hooks are run (usually the working tree root; see DESCRIPTION section of man githooks
).
通常,路径可以是绝对的,也可以是相对于运行钩子的目录(通常是工作树根;请参阅 的说明部分man githooks
)。
回答by kilianc
If your project is a JavaScript project and you use npm
as package manager you can use shared-git-hooksto enforce githooks on npm install
.
如果您的项目是一个 JavaScript 项目并且您npm
用作包管理器,则可以使用shared-git-hooks在npm install
.
回答by Shane Gannon
For Nodejsusers a simple solution is to update package.jsonwith
对于的NodeJS用户一个简单的解决方案是更新的package.json与
{
"name": "name",
"version": "0.0.1",
......
"scripts": {
"preinstall": "git config core.hooksPath hooks",
The preinstallwill run before
该预安装将前运行
npm install
安装
and redirects git to look for hooks inside the .\hooks(or whatever name you choose) directory. This directory should mimic .\.git\hooksin terms of file name (minus the .sample) and structure.
并重定向 git 以在.\hooks(或您选择的任何名称)目录中查找挂钩。这个目录应该在文件名(减去 .sample)和结构方面模仿.\.git\hooks。
Imagine Maven and other build tools will have an equivalent to preinstall.
想象一下 Maven 和其他构建工具将有一个与preinstall等效的工具。
It should also work across all platforms.
它也应该适用于所有平台。
If you need any more info see https://www.viget.com/articles/two-ways-to-share-git-hooks-with-your-team/
如果您需要更多信息,请参阅https://www.viget.com/articles/two-ways-to-share-git-hooks-with-your-team/
回答by cattail
回答by yuranos
Most of the modern programming languages, or rather their build tools, support plugins to manage git hooks. That means all you need to do is configure your package.json, pom.xml, etc., and anyone in your team will have no option but to comply unless they change the build file. The plugin will add content to .git directory for you.
大多数现代编程语言,或者说它们的构建工具,都支持插件来管理 git 钩子。这意味着您需要做的就是配置您的 package.json、pom.xml 等,并且您团队中的任何人都别无选择,只能遵守,除非他们更改构建文件。该插件将为您添加内容到 .git 目录。
Examples:
例子:
https://github.com/rudikershaw/git-build-hook
https://github.com/rudikershaw/git-build-hook
https://github.com/olukyrich/githook-maven-plugin
https://github.com/olukyrich/githook-maven-plugin
回答by Mike de Klerk
We are using Visual Studio solutions (and thus projects) which have pre and post build events. I'm adding an additional project named 'GitHookDeployer'. The project self modifies a file in the post build event. That file is set to copy to the build directory. Thus the project is build every time and is never skipped. In the build event, it also makes sure that all git hooks are in place.
我们正在使用具有构建前和构建后事件的 Visual Studio 解决方案(以及项目)。我正在添加一个名为“GitHookDeployer”的附加项目。项目自己在构建后事件中修改了一个文件。该文件设置为复制到构建目录。因此,该项目每次都被构建并且永远不会被跳过。在构建事件中,它还确保所有 git 钩子都到位。
Note that this is not a general solution, as some projects, of course, have nothing to build.
请注意,这不是通用的解决方案,因为有些项目当然没有什么可构建的。
回答by Do-do-new
You can make your hooks folder another git repository and link it as a submodule... I guess worth it only if you have a lot of members and hooks changed regularly.
您可以将您的 hooks 文件夹设为另一个 git 存储库并将其链接为子模块...我想只有当您有很多成员并且 hooks 定期更改时才值得。
回答by Shimon Tolts
You could use a managed solution for pre-commit hook management like pre-commit. Or a centralized solution for server-side git-hooks like Datree.io. It has built-in policies like:
您可以使用托管解决方案进行预提交挂钩管理,例如pre-commit。或者像Datree.io这样的服务器端 git-hooks 的集中式解决方案。它具有内置策略,例如:
- Detect and prevent merging of secrets.
- Enforce proper Git user configuration.
- Enforce Jira ticket integration- mention ticket number in pull request name / commit message.
It won't replace all of your hooks, but it might help your developers with the most obvious ones without the configuration hell of installing the hooks on every developers computer/repo.
它不会替换您所有的钩子,但它可能会帮助您的开发人员使用最明显的钩子,而无需在每个开发人员的计算机/存储库上安装钩子的配置地狱。
Disclaimer: I am one of Datrees founders
免责声明:我是 Datrees 的创始人之一
回答by Lovato
Ideally, hooks are written in bash, if you follow the sample files. But you can write it in any language available, and just make sure it has the executable flag.
理想情况下,如果您遵循示例文件,钩子是用 bash 编写的。但是您可以用任何可用的语言编写它,只要确保它具有可执行标志即可。
So, you can write a Python or Go code to achieve your goals, and place it under the hooks folder. It will work, but it will not be managed along with the repository.
因此,您可以编写 Python 或 Go 代码来实现您的目标,并将其放在 hooks 文件夹下。它可以工作,但不会与存储库一起管理。
Two Options
两种选择
a) Multi Scripts
a) 多脚本
You can code your hooks inside your help, and add a small fragment of code to hooks, to call your perfect script, like this:
你可以在你的帮助中编写你的钩子,并添加一小段代码到钩子,来调用你完美的脚本,像这样:
$ cat .git/hooks/pre-commit
#!/bin/bash
../../hooks/myprecommit.js
b) Single Script
b) 单一脚本
A cooler option is to add just one script to rule them all, instead of several ones. So, you create an hooks/mysuperhook.go and point every hook you wanna have to it.
一个更酷的选择是只添加一个脚本来统治它们,而不是几个。所以,你创建一个 hooks/mysuperhook.go 并指向你想要的每个钩子。
$ cat .git/hooks/pre-commit
#!/bin/bash
../../hooks/mysuperhook.go $(basename ##代码##)
The parameter will provide your script which hook was triggered, and you can differentiate it inside your code. Why? Sometimes you might wanna run the same check for commit and push, for instance.
该参数将为您的脚本提供触发了哪个挂钩,您可以在代码中区分它。为什么?例如,有时您可能希望对提交和推送运行相同的检查。
And then?
进而?
Then, you might want to have further functionalities, like:
然后,您可能想要更多功能,例如:
- Trigger the hook manually to check if everything is ok even prior to a commit or push. If you just call your script (option a or b) would do the trick.
- Trigger the hooks on CI, so you don't need to rewrite the same checks for CI, it would be just calling the commit and push triggers, for instance. The same as the above should solve it.
- Call external tools, like a markdown validator, or a YAML validator. You can make syscalls and need to handle STDOUT and STDERR.
- Make sure all developers have a simple way to install the hooks, so a nice script needs to be added to the repository to replace default hooks with the correct ones
- Have some global helpers, like a check to block commits to develop and master branches, not having to add it to every repository. You can solve it by having another repository with global scripts.
- 甚至在提交或推送之前手动触发钩子以检查一切是否正常。如果你只是调用你的脚本(选项 a 或 b)就可以了。
- 触发 CI 上的钩子,因此您不需要为 CI 重写相同的检查,例如,它只是调用提交和推送触发器。和上面一样应该可以解决。
- 调用外部工具,如降价验证器或 YAML 验证器。您可以进行系统调用并需要处理 STDOUT 和 STDERR。
- 确保所有开发人员都有一个简单的方法来安装钩子,因此需要将一个不错的脚本添加到存储库中,以将默认钩子替换为正确的钩子
- 有一些全局助手,比如检查阻止提交到开发和主分支,而不必将它添加到每个存储库。您可以通过使用另一个带有全局脚本的存储库来解决它。
Can this be simpler?
这可以更简单吗?
Yes, there are several tools to help you manage git-hooks. Each of them is tailored to tackle the problem from a different perspective, and you might need to understand all of them to get the one which is best for you or your team. GitHooks.comoffers a lot of reading about hooking, and several tools available today.
是的,有几个工具可以帮助您管理 git-hooks。它们中的每一个都是为从不同的角度解决问题而量身定制的,您可能需要了解所有这些才能找到最适合您或您的团队的方法。GitHooks.com提供了大量关于挂钩的阅读材料,以及当今可用的几种工具。
As of today, there are 21 projects listed there with different strategies to manage git hooks. Some only do it for a single hook, some for a specific language, and so on.
截至今天,那里列出了 21 个项目,它们使用不同的策略来管理 git hooks。有些只为一个钩子做,有些只为特定的语言做,等等。
One of those tools, written by me and offered for free as an opensource project, is called hooks4git. It is written in Python (because I like it) but the idea is to handle all items listed above in a single configuration file called .hooks4git.ini, which lives inside your repository and can call any script you want to call, in any language.
其中一个由我编写并作为开源项目免费提供的工具称为hooks4git。它是用 Python 编写的(因为我喜欢它)但它的想法是在一个名为 .hooks4git.ini 的配置文件中处理上面列出的所有项目,该文件位于您的存储库中,可以使用任何语言调用您想要调用的任何脚本.
Using git hooks is absolutely fantastic, but the way they are offered usually only gets people away from it.
使用 git hooks 非常棒,但它们提供的方式通常只会让人们远离它。