git commit access restrictions client code

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37886429/article/details/101012476

Function: to limit the functions of a branch push
git hook Description See: Customizing Git - Git hook

1, the new commit-msg file in the client's local .git / hooks / directory permissions to 755
cd .git/hooks/

cat > commit-msg  << END
#!/bin/sh

# 当前分支名称
currentBranch=`git rev-parse --abbrev-ref HEAD`
#如果当前分支为dev,就退出
if [ $currentBranch == "dev" ] ;then
    exit 1
fi

END

#授权
chmod 755 commit-msg

Note:
the commit-msg hook effect: the hook script to exit non-zero, Git will abandon submitted

2, the expansion of knowledge

Requirements: Only their own branch, to be submitted; if not their own branch, want to force required to submit the information submitted, the information required to submit the first acts of "force commit"

cat > commit-msg  << END
#!/bin/sh

# 使用说明
# 1.只有是自己的分支,才能提交,可以在myBranchs中设置,例子:myBranchs=("feature/mbg_dev" "dev")
# 2.如果不是自己的分支,想强制提交需要在提交信息中,需要提交信息的第一行为"force commit"
# 3.myBranchs中的分支名称支持正则表达式,例子:myBranchs=("^feature/mbg_")

# 自己的分支(数组)
myBranchs=("^feature/mbg_")
# 当前分支名称
currentBranch=`git rev-parse --abbrev-ref HEAD`
for i in ${myBranchs[@]}
do
   [[ $currentBranch =~ $i ]] && exit 0
done

# 提交信息第一行
commitCntHead=`head -n 1 "$1"`
if [[ $commitCntHead = "force commit" ]]
  then
   exit 0
else
  echo "no authority commit to branch [ " $currentBranch "]"
  exit 1
fi
END

Note:
In the above version 2.9 git can set a global variable, low version can only put the file in the project .git / hooks / folder, use the command: git config --global core.hooksPath /d/git/hookswhere /d/git/hooksbehalf D:\git\hooks.

Guess you like

Origin blog.csdn.net/m0_37886429/article/details/101012476