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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19272901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:15:09  来源:igfitidea点击:

Git commit hook - How to use the commit-msg hook to check for string in message?

gitperlbashhook

提问by lol

I need to make a commit-msg hook to check if the commit message contains "app.asana" in any part of this. I searched some references and documentation and I know I need to use the commit-msg for this. I have to make this using Perl or Bash.

我需要做一个 commit-msg 钩子来检查提交消息的任何部分是否包含“app.asana”。我搜索了一些参考资料和文档,我知道我需要为此使用 commit-msg。我必须使用 Perl 或 Bash 来实现。

Does anybody has a clue about this or somewhere I can look more examples to learn how to do it??

有没有人对此有任何线索,或者我可以在某个地方查看更多示例来学习如何做?

Thank you.

谢谢你。

回答by lol

I found an answer to my question. In case it may help somebody...

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

I just created a commit-msgfile in .git/hookscontaining

我刚刚commit-msg.git/hooks包含中创建了一个文件

#!/bin/sh

test -n "$(grep 'app.asana.com/' )" || {
        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
}

Each user needs to have a commit-msgfile inside .git/hooks. Then, as a solution, I added commit-msgto the project folder (so I can pull this) with another file called commit-msg-hook.shcontaining:

每个用户都需要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"

I welcome any advice to improve what I've done. Thanks.

我欢迎任何建议来改进我所做的事情。谢谢。