git hooks 在提交消息中强制执行 JIRA 票号

标签 git

我正在尝试强制用户将 JIRA 票证添加到 git commit。

我使用了一个预接收 Hook ,但它只有在推送后才能工作。我希望它在提交后工作,所以如果消息格式不正确,提交将失败,用户将可以选择编辑提交。

这是我的代码示例:

#!/usr/bin/env bash

# set this to your active development branch
#develop_branch="master"
#current_branch="$(git rev-parse --abbrev-ref HEAD)"

# only check commit messages on main development branch
#[ "$current_branch" != "$develop_branch" ] && exit 0

# regex to validate in commit msg
commit_regex='(#-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('#-1111') '"
rm -rf fl.txt
echo $1 >> fl.txt
fil="fl.txt"

if ! grep -iE $commit_regex $fil; then
    echo "$error_msg" >&2
    exit 1
fi
rm -rf fl.txt

最佳答案

the only thing that I managed to work is pre-receive



这是正确的方法:您在服务器端(在此实例中您的 GitLab 服务器带有 GitLab custom hook )强制执行策略(此处检查提交消息以获取 jira 票号):这是通过 完成的。 server-side hook .

这意味着它将哀号 git push,迫使用户修改他/她的提交并再次推送。

I must have both pre-commit and pre-receive in order the pre-receive to work ?



不,只需要一个预接收钩子(Hook)。尝试在服务器级别进行预提交 Hook 工作是不可能的:您必须以某种方式将其分发给所有开发人员,以便他们在自己的本地存储库中激活它。这不实用。

关于git hooks 在提交消息中强制执行 JIRA 票号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42327673/

相关文章:

git - 查找未 merge 的 Git 分支?

git - 自动 merge package-lock.json

Git clone --no-checkout again

Github 多个帐户-personalUserName/repoName.git 的权限被拒绝给 globalUserName

git - 从一个分支 merge 到另一个分支会引发冲突

git - 为什么这个git commit在日志中存在,而在文件的日志中却没有呢?

git - 允许更改的文件不被 git pull 覆盖

linux - 是否有必要在每个二分步骤后清理 Linux 内核源代码树?

git - 一次性将本地 git repo 与远程同步,丢弃本地更改/提交

git - 请改用个人访问 token 。 (但使用一个)