我正在尝试强制用户将 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/