更改默认的 git hooks

发布于 2024-08-16 04:46:05 字数 371 浏览 13 评论 0原文

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


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

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

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.


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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放飞的风筝 2024-08-23 04:46:05

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

请参阅提交 867ad08, 提交de0824e, 提交 bf7d977提交 49fa52f(2016 年 5 月 4 日),作者:Ævar Arnfjörð Bjarmason (avar)
(由 Junio C Hamano -- gitster -- 合并于 提交 6675f50,2016 年 5 月 17 日)

git config 文档 现在包括:

core.hooksPath

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

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

此配置变量在您想要的情况下非常有用
集中配置您的 Git 挂钩,而不是在
每个存储库的基础,或者作为更灵活和集中的
替代拥有已更改的 init.templateDir
默认挂钩。


该路径的作者 (Ævar Arnfjörð Bjarmason avar)添加他的提交

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


Git 2.10 使用 git rev-parse --git-path hooks/ 中的新设置

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

rev-parse:尊重--git-path

中的core.hooksPath

--git-path的思想> 选项不仅是为了避免
始终使用 --git-dir 的输出为路径添加前缀,同时也尊重 .git 目录内特定公共路径的覆盖
(例如 git rev-parse --git-path objects 将报告
环境变量GIT_OBJECT_DIRECTORY(如果设置)。

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

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

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

The git config documentation now includes:

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 hooks
instead of configuring them on a
per-repository basis, or as a more flexible and centralized
alternative to having an init.templateDir where you've changed
default hooks.

The author of this pathc (Ævar Arnfjörð Bjarmason avar) adds in his commit:

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 2.10 uses that new setting in 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)

rev-parse: respect core.hooksPath in --git-path

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

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

默嘫て 2024-08-23 04:46:05

来自 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.

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

[init]
     templatedir = /path/to/templates

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

       --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:

[init]
     templatedir = /path/to/templates
み零 2024-08-23 04:46:05

使用 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
$ 

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


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

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

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

我没有尝试过在目录中创建非示例文件;它可能会也可能不会被复制。不过,要小心更改默认设置 - 当您下次升级时,您将必须重做更改。

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

$ 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.


Addressing the question in a comment - to change the default sample hooks installed, you need to find the directory where git is 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:

$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.

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.

唐婉 2024-08-23 04:46:05

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

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

For MacPorts dorectory will be /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/hooks
then create Rake task or shell script
to copy cp $PROJECT_DIR/git/hooks/* $PROJECT_DIR/.git/hooks.
Symbolic links doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文