更改默认的 git 钩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1977610/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:35:11  来源:igfitidea点击:

change default git hooks

gitgithooks

提问by rplevy

Not sure if this is possible in git (I haven't found it but I may be using the wrong vocabulary in my searches), but it be would useful to be able to modify and enable hooks as the defaults for all new repositories (at the time of creation I mean) so these don't have to be customized each time a new repository is created. It seems the easy way to do this is write a wrapper that sets my hooks and chmods them when I create a new repository, but if there's a way built into git I would rather use that instead of having unnecessary wrapper scripts however little lying around.

不确定这在 git 中是否可行(我还没有找到它,但我可能在搜索中使用了错误的词汇),但是能够修改和启用钩子作为所有新存储库的默认值会很有用(在我的意思是创建时间)所以每次创建新存储库时都不必自定义这些。似乎最简单的方法是编写一个包装器,在我创建新存储库时设置我的钩子和 chmods,但如果有一种内置于 git 的方法,我宁愿使用它,而不是使用不必要的包装器脚本,但几乎没有。



Clarification copied up from a comment to a now-deleted answer:

澄清从评论复制到现已删除的答案:

My question is whether the default behavior for ALL new repositories can be changed, so they don't need to be customized in the same way each time for each new repository. The easy answer is write a wrapper for creating and customizing the repos (it generates the hook scripts and chmods them), but it seems like this default behavior should also be customizable without having to do that.

我的问题是是否可以更改所有新存储库的默认行为,因此不需要每次为每个新存储库都以相同的方式自定义它们。简单的答案是编写一个用于创建和自定义存储库的包装器(它生成挂钩脚本并对其进行 chmods),但似乎这种默认行为也应该可以自定义,而不必这样做。

采纳答案by Brian Campbell

From the git-initman page (also works with git-cloneif you are cloning an existing repo instead of creating a new one from scratch):

git-init手册页(git-clone如果您要克隆现有存储库而不是从头开始创建新存储库,也可以使用):

       --template=<template_directory>
           Provide the directory from which templates will be used. The
           default template directory is /usr/share/git-core/templates.

           When specified, <template_directory> is used as the source of the
           template files rather than the default. The template files include
           some directory structure, some suggested "exclude patterns", and
           copies of non-executing "hook" files. The suggested patterns and
           hook files are all modifiable and extensible.

You can modify the system-wide template directory (which defaults to /usr/share/git-core/templates, but may be in a different location on your machine), you can supply --template=<template_directory>on the command line when you create or clone the repo, or you can configure the default template directory in your config file:

您可以修改系统范围的模板目录(默认为/usr/share/git-core/templates,但可能在您机器上的不同位置),您可以--template=<template_directory>在创建或克隆 repo 时在命令行中提供,或者您可以在你的配置文件:

[init]
     templatedir = /path/to/templates

回答by VonC

With git 2.9, you have a new option made for centralizing hooks: core.hooksPath

使用 git 2.9,您有一个用于集中钩子的新选项: core.hooksPath

See commit 867ad08, commit de0824e, commit bf7d977, commit 49fa52f(04 May 2016) by ?var Arnfj?re Bjarmason (avar).
(Merged by Junio C Hamano -- gitster--in commit 6675f50, 17 May 2016)

请参阅?var Arnfj?re Bjarmason ( ) 的提交 867ad08提交 de0824e提交 bf7d977提交 49fa52f(2016 年 5 月 4 日(由Junio C Hamano合并-- --提交 6675f50 中,2016 年 5 月 17 日)avar
gitster

The git configdocumentationnow includes:

git config文档现在包括:

core.hooksPath

By default Git will look for your hooks in the '$GIT_DIR/hooks' directory.
Set this to different path, e.g. '/etc/git/hooks', and Git will try to find your hooks in that directory, e.g. '/etc/git/hooks/pre-receive' instead of in '$GIT_DIR/hooks/pre-receive'.

The path can be either absolute or relative. A relative path is taken as relative to the directory where the hooks are run

This configuration variable is useful in cases where you'd like to centrally configure your Git hooksinstead of configuring them on a per-repository basis, or as a more flexible and centralized alternative to having an init.templateDirwhere you've changed default hooks.

默认情况下,Git 会在 ' $GIT_DIR/hooks' 目录中查找您的钩子。
将此设置为不同的路径,例如“ /etc/git/hooks”,Git 将尝试在该目录中查找您的钩子,例如“ /etc/git/hooks/pre-receive”而不是“ $GIT_DIR/hooks/pre-receive”。

路径可以是绝对的,也可以是相对的。相对路径被视为相对于运行钩子的目录

如果您希望集中配置 Git 挂钩而不是在每个存储库的基础上配置它们,或者作为更改默认挂钩init.templateDir位置的更灵活和集中的 替代方案,则此配置变量非常有用

The author of this pathc (?var Arnfj?re Bjarmasonavar) adds in his commit:

这个 pathc 的作者(?var Arnfj?re Bjarmasonavar)在他的提交中添加:

I plan on using this on a centralized Git server where users can create arbitrary repositories under /gitroot, but I'd like to manage all the hooks that should be run centrally via a unified dispatch mechanism.

我计划在集中式 Git 服务器上使用它,用户可以在其中创建任意存储库/gitroot,但我想管理所有应该通过统一调度机制集中运行的钩子。



Git 2.10 uses that new setting in git rev-parse --git-path hooks/<hook>

Git 2.10 在 git rev-parse --git-path hooks/<hook>

See commit 9445b49(16 Aug 2016) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster--in commit d05d0e9, 19 Aug 2016)

请参阅Johannes Schindelin ( ) 的提交 9445b49(2016 年 8 月 16 日(由Junio C Hamano合并-- --d05d0e9 提交中,2016 年 8 月 19 日)dscho
gitster

rev-parse: respect core.hooksPathin --git-path

rev-parse:尊敬core.hooksPath--git-path

The idea of the --git-pathoption is not only to avoid having to prefix paths with the output of --git-dirall the time, but also to respect overrides for specific common paths inside the .gitdirectory
(e.g. git rev-parse --git-path objectswill report the value of the environment variable GIT_OBJECT_DIRECTORY, if set).

--git-path选项的想法不仅是避免必须在--git-dir所有时间的输出中添加路径前缀,而且还要尊重.git目录内特定公共路径的覆盖
(例如,git rev-parse --git-path objects将报告环境变量的值GIT_OBJECT_DIRECTORY,如果设置)。

When introducing the core.hooksPathsetting, we forgot to adjust git_path()accordingly. This patch fixes that.

在介绍core.hooksPath设置时,我们忘了相应地调整git_path()。这个补丁修复了这个问题。

回答by Jonathan Leffler

With git 1.6.5.3 (and some earlier versions), you get sample hooks delivered in your .git/hooksdirectory:

使用 git 1.6.5.3(和一些早期版本),您可以在您的.git/hooks目录中获得示例挂钩:

$ ls .git/hooks
applypatch-msg.sample     post-update.sample        prepare-commit-msg.sample
commit-msg.sample         pre-applypatch.sample     update.sample
post-commit.sample        pre-commit.sample
post-receive.sample       pre-rebase.sample
$ 

They are all executable on my system. To make use of one of the hooks, either copy or rename the file removing the '.sample' suffix. Edit it as appropriate to suit your requirements.

它们在我的系统上都是可执行的。要使用其中一个挂钩,请复制或重命名文件并删除“ .sample”后缀。根据您的要求进行适当编辑。



Addressing the question in a comment - to change the default sample hooks installed, you need to find the directory where gitis installed. On my machine, that is $HOME/git- so the binary is in $HOME/git/bin/git. Then the directory containing the sample hooks is:

解决评论中的问题 - 要更改安装的默认示例挂钩,您需要找到git安装的目录。在我的机器上,那就是$HOME/git- 所以二进制文件在$HOME/git/bin/git. 那么包含示例钩子的目录是:

$HOME/git/share/git-core/templates/hooks

If you edit those templates (be careful), then that is what will be copied to new git repositories. They'll still be samples, but they'll be your samples.

如果您编辑这些模板(小心),那么这些模板将被复制到新的 git 存储库中。它们仍然是样品,但它们将是您的样品。

I've not experimented with creating a non-sample file in the directory; it might or might not be copied. Be wary of changing the defaults, though - when you next upgrade, you'll have to redo the changes.

我还没有尝试在目录中创建非示例文件;它可能会或可能不会被复制。但是,请注意更改默认值 - 下次升级时,您必须重做更改。

回答by A B

For MacPorts dorectory will be /opt/local/share/git-core/templates/

对于 MacPorts 目录将是 /opt/local/share/git-core/templates/

To share hooks or templates with team members I usually create subdirectory in project like $PROJECT_DIR/lib/git/hooksthen create Rake task or shell script to copy cp $PROJECT_DIR/git/hooks/* $PROJECT_DIR/.git/hooks. Symbolic links doesn't work.

为了与团队成员共享钩子或模板,我通常在项目中创建子目录,$PROJECT_DIR/lib/git/hooks然后创建 Rake 任务或 shell 脚本来复制cp $PROJECT_DIR/git/hooks/* $PROJECT_DIR/.git/hooks. 符号链接不起作用。