git hook commit-msg

    今天公司遇见一个需求,就是所有人提交的时候都需要在注释后面加一个特殊标记,但是很多人都会忘记,所以就需要用到git hook的commit-msg功能。在网上一番搜罗后,自己写了一个shell脚本给大家使用,具体代码如下:

shell脚本如下:

#!/bin/sh
#coding=utf8
#author=wangchuan@camera360.com

WORK_DIR=$1
DEPTH=${2:-2}
if [ ! $WORK_DIR ] ;then
echo 'Please input the work dir, see examplte: ./git_hook.sh /home/worker/data/www 2'
echo 'If your root dir is very depth, please use larger number, etc: ./git_hook.sh /home/worker/data/www 4'
exit 1
fi

function createGlobalHook(){
    HOOK_DIR="$1/.git_template/hooks"
    if [ ! -x $HOOK_DIR ];then
        mkdir -p $HOOK_DIR
    fi
    COMMIT_MSG="$HOOK_DIR/commit-msg"
    HOOK_STRING='#!/bin/sh\n\nINPUT_FILE=$1\nMESSAGE=`head -n1 $1`\nPATTERN="PLATFORM-[0-9]{1,}$"\nif ! [[ $MESSAGE =~ $PATTERN ]];then\n    echo "Bad commit message, see example: fixed t
he bug PLATFORM-123"\n    exit 1\nfi\n'
    echo -e $HOOK_STRING > $COMMIT_MSG
    chmod +x $COMMIT_MSG
}


function initGitHook(){
    git config --global init.templatedir "$1/.git_template"
    for FILE in `find "$WORK_DIR"  -mindepth 1 -maxdepth $2 -name '.git' | grep -v 'vendor'`
    do
        GIT_DIR=${FILE//.git/}
        echo $GIT_DIR
        COMMIT_MSG=$GIT_DIR"/.git/hooks/commit-msg"
        if [ -a "$COMMIT_MSG" ];then
            mv $COMMIT_MSG $COMMIT_MSG"-backup"
        fi
        #cd the dir, and git init
        cd $GIT_DIR
        git init --template "$1/.git_template"
    done


}
echo "Start to create global git hook: "
createGlobalHook $WORK_DIR
echo "Global git hook created done. "

echo "Start to git init: ";
initGitHook $WORK_DIR $DEPTH
echo "End to git init. "
Be the First to comment.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值