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
Git commit hook - How to use the commit-msg hook to check for string in message?
提问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-msg
file in .git/hooks
containing
我刚刚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-msg
file inside .git/hooks
. Then, as a solution, I added commit-msg
to the project folder (so I can pull this) with another file called commit-msg-hook.sh
containing:
每个用户都需要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.
我欢迎任何建议来改进我所做的事情。谢谢。