git hook commit-msg

it2022-05-05  156

    今天公司遇见一个需求,就是所有人提交的时候都需要在注释后面加一个特殊标记,但是很多人都会忘记,所以就需要用到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)