git - 向中央 repo (或类似的东西)添加了预提交 Hook

标签 git gitlab pre-commit-hook

好的,所以我想出了如何添加一个预提交钩子(Hook)来检查将引用 JIRA 项目的消息。

#!/bin/sh
test "" != "$(grep 'JIRA-' "$1")" || {
        echo >&2 "ERROR: Commit message is missing Jira issue number."
        exit 1
}

我将它添加到我的本地存储库中,一切都很顺利。我对它所做的每一次提交都有这个提交信息。 但是,我对这样一种情况很感兴趣,即有一群开发人员都提交了他们的本地存储库,然后推送/pull 到 GitHub 上的远程主机。我想要的是一种类似的机制,当推送更改到 GitLab 上的远程仓库时,他们必须类似地引用 JIRA。

执行此操作的好方法是什么?

最佳答案

Sytse Sijbrandij 中所述的 answer ,并在“GitLab Enterprise Edition”页面中详细介绍,GitLab EE 现在支持:

Git hooks rules (commit message must mention an issue, prevent tag deletion, etc.)

但同样,这是企业版(EE),而不是社区版(CE)。
对于后者,请参阅我的原始答案。


原始答案(2013 年 8 月)

您可以添加一个 pre-receive Hook ,它会阻止任何推送到 gitlab 管理的裸仓库的提交。

然而,困难在于为通过 GitLab 创建的任何新项目(和 repo)添加该 Hook ,因为 thread illustrates .

GitLab doesn't used gitolite anymore ,没有简单或 native 的方法来执行此操作(目前是 5.X 和 6.0)。

For now you could throw your hook in the hooks directory in gitlab-shell then modify https://github.com/gitlabhq/gitlab-shell/blob/master/lib/gitlab_projects.rb#L46 to reflect.

Gitlab-Shell/lib/gitlab_projects.rb

def create_hooks_cmd
  pr_hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive')
  up_hook_path = File.join(ROOT_PATH, 'hooks', 'update')
  "ln -s #{pr_hook_path} #{full_path}/hooks/post-receive && ln -s #{up_hook_path} #{full_path}/hooks/update"
end

to get our own centralized hooks in there.

关于git - 向中央 repo (或类似的东西)添加了预提交 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323230/

相关文章:

git - Emacs git 插件 - egg

git - 链接 Bitbucket Git 存储库上的最新文件

git - .gitignore : track a single file in an ignored directory

svn - 在预提交脚本中检测分支重新集成或合并

git - 这个预提交钩子(Hook)如何修复尾随空格?

git - 如何为位于 GIthub 中的存储库执行编码标准

git - 如何将多个 Git 提交(已推送)还原到已发布的存储库?

git - 如何保持两个存储库同步,其中一个存储库是另一个存储库的子存储库?

git - 无法访问远程存储库以导入 Go 模块

git - Git 上的安全问题