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

瘦欲@ 提交于 2019-12-06 09:12:33

问题


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.

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

Thank you.


回答1:


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

I just created a commit-msg file in .git/hooks containing

#!/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. 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:

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



来源:https://stackoverflow.com/questions/19272901/git-commit-hook-how-to-use-the-commit-msg-hook-to-check-for-string-in-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!