如何在本地系统上强制执行git commit消息策略以防止推送

时间:2016-04-19 12:43:22

标签: git shell

如何在本地强制执行消息格式(我被迫包含refs #<ticket_no>),这将阻止我提交。

如同git commit -m --ignore-fomrat ""

一样,支持绕过这一点也很好

Setup a git commit message policy远程系统的答案

1 个答案:

答案 0 :(得分:0)

你需要挂钩来完成这项任务。

挂钩可以在本地存储库中,但可以删除它们,因此您还需要在远程存储库中对其进行验证。

<强> Sample post-commit hook

#!/bin/sh
#Pickup the commit details
while read oldrev newrev refname
do
        branch=`echo $refname | cut -d'/' -f3`
        old=`echo $oldrev | cut -c1-5`
        new=`echo $newrev | cut -c1-5`
done

for rev in `git rev-list $old..$new`;do

message =`git cat-file commit $rev |  sed '1,/^$/d'`

echo $message | grep  -q ^'refs #'

if [ $? -ne 0 ] ;then

    # Output colors
    red='\033[0;31m';
    green='\033[0;32m';
    yellow='\033[0;33m';
    default='\033[0;m';

    # personal touch :-)
    echo "${red}"
    echo "                                         "
    echo "                   |ZZzzz                "
    echo "                   |                     "
    echo "                   |                     "
    echo "      |ZZzzz      /^\            |ZZzzz  "
    echo "      |          |~~~|           |       "
    echo "      |        |-     -|        / \      "
    echo "     /^\       |[]+    |       |^^^|     "
    echo "  |^^^^^^^|    |    +[]|       |   |     "
    echo "  |    +[]|/\/\/\/\^/\/\/\/\/|^^^^^^^|   "
    echo "  |+[]+   |~~~~~~~~~~~~~~~~~~|    +[]|   "
    echo "  |       |  []   /^\   []   |+[]+   |   "
    echo "  |   +[]+|  []  || ||  []   |   +[]+|   "
    echo "  |[]+    |      || ||       |[]+    |   "
    echo "  |_______|------------------|_______|   "
    echo "                                         "
    echo "                                         "
    echo "  ${green}Your message is unacceptable :-)${red}  " 
    echo "  Fix it if you wish to commit .!!!      "
    echo "                                         "
    echo "${default}"

    exit 0    
fi