我试图强制用户将JIRA票证添加到git commit。git hooks在提交消息中执行JIRA票证号码
我使用了预先接收的钩子,但它只能在推后才能工作。我希望它在提交后工作,所以如果消息格式不正确,提交将失败,用户将可以选择编辑提交。
这是我的代码示例:
#!/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
如果您希望提交失败,则需要预先提交挂钩。在提交后它不应该工作。 – jonrsharpe
也许我错过了一些东西。如果我将使用预先提交,那么它将失败提交并允许用户进行编辑?我设法工作的唯一事情是预先接收。也许你有它的代码示例? – sarit
是的,当您想要的行为符合预先提交的钩子时,您正在编写预接收钩子。请参阅可用钩子文档:https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks – jonrsharpe