2017-07-31 153 views
1

我有git提交钩子脚本,谁检查提交输入消息,如果消息不包含单词“更新”脚本有拒绝提交。Git预先提交钩子检查消息

#!/bin/bash 
read -p "Enter a commit message: " message 

if [[ ${message} != *"updated"* ]];then 
    echo "Your commit message must contain the word 'updated'" 
    else 
    git commit -m "$message" 
    fi 

如何使这个钩自动执行的,如果我试图把一些文件在我的本地回购与消息:git的承诺-m“更新:东西”,我的想法是让不喜欢“运行此脚本做提交“,但是然后你打开控制台并尝试通过键入commant进行提交,脚本会自动检查你的提交信息并通过或拒绝。

+0

https://www.git-scm.com/docs/githooks#_pre_commit或https://www.git-scm.com/docs/githooks#_prepare_commit_msg或https://www.git-scm .COM /文档/ githooks#_commit_msg。 – ElpieKay

+0

感谢您的回复!也许你知道我可以如何改变我的钩子代码,拒绝提交如果我的消息不包含单词“更新”?例如,如果我在git commit -m中输入错误的信息,它会拒绝提交,如果真的通过它 – Andrej

回答

2

commit-msg为例。

#!/bin/bash 

MSG="$1" 

if ! grep -qE "updated" "$MSG";then 
    cat "$MSG" 
    echo "Your commit message must contain the word 'updated'" 
    exit 1 
fi 

chmod 755 commit-msg,并复制它作为.git/hooks/commit-msg

+0

你能解释最后一行吗? chmod 755 commit-msg并将其复制为.git/hooks/commit-msg。你的意思是chmod + x ./commit-msg? 755是做什么的? – Andrej

+0

现在我复制你的代码ant添加它在文件上,然后我尝试提交我有一个错误:错误:无法运行.git/hooks/pre-commit:没有这样的文件或目录 – Andrej

+0

@Andrej'755'是替代' a + x'虽然不完全一样。最后一行'chmod ...'不是钩子'commit-msg'的一部分。将代码复制到一个文件中,并命名文件'commit-msg',使其可执行并将其复制到当前存储库的'.git/hooks /'中。当'git commit'完成时,调用钩子'commit-msg'并检查提交消息。如果它不包含“更新”,则提交失败。 – ElpieKay