Git commit hook - 如何使用commit-msg钩子检查消息中的字符串?

时间:2013-10-09 13:04:47

标签: git perl bash hook

我需要创建一个commit-msg挂钩来检查提交消息中是否包含“app.asana”。我搜索了一些参考资料和文档,我知道我需要使用commit-msg。我必须使用Perl或Bash来制作它。

有没有人对这个或某个地方有所了解我可以看一些例子来学习如何去做?

谢谢。

1 个答案:

答案 0 :(得分:8)

我找到了问题的答案。如果它可以帮助某人...

我刚刚在commit-msg中创建了一个包含

.git/hooks文件
#!/bin/sh

test -n "$(grep 'app.asana.com/' ${1})" || {
        echo >&2 "ERROR: Commit message is missing Asana's task link.\n\nPlease append the Asana's task link relative to this commit into the commit message."
        exit 1
}

每个用户都需要commit-msg内的.git/hooks文件。然后,作为一个解决方案,我将commit-msg添加到项目文件夹(我可以将其拉出来)与另一个名为commit-msg-hook.sh的文件包含:

#!/bin/sh

cp commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
echo "\nThe commit-msg hook was added to git hooks"

我欢迎任何建议,以改善我所做的。感谢。

相关问题