简体   繁体   English

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

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

I need to make a commit-msg hook to check if the commit message contains "app.asana" in any part of this. 我需要创建一个commit-msg钩子来检查提交消息中是否包含“app.asana”。 I searched some references and documentation and I know I need to use the commit-msg for this. 我搜索了一些参考资料和文档,我知道我需要使用commit-msg。 I have to make this using Perl or Bash. 我必须使用Perl或Bash来制作它。

Does anybody has a clue about this or somewhere I can look more examples to learn how to do it?? 有没有人知道这个或某个地方我可以看看更多的例子来学习如何做到这一点?

Thank you. 谢谢。

I found an answer to my question. 我找到了一个问题的答案。 In case it may help somebody... 如果它可以帮助某人......

I just created a commit-msg file in .git/hooks containing 我刚刚在.git/hooks中创建了一个commit-msg文件

#!/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
}

Each user needs to have a commit-msg file inside .git/hooks . 每个用户都需要在.git/hooks有一个commit-msg文件。 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添加到项目文件夹中(所以我可以将其拉出来)与另一个名为commit-msg-hook.sh文件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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM