How to enforce a git commit message policy on loca

2019-09-09 17:17发布

问题:

How can I enforce a message format locally (where I am forced to include refs #<ticket_no>), which will prevent me from committing.

It would also be nice to have support for circumventing this like git commit -m --ignore-fomrat ""

Setup a git commit message policy answers for remote system

回答1:

You will need hook for this task.

Hooks can be on the local repository but they can be deleted so you will need to verify it in the remote repository as well.


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


标签: git shell