首页 > 其他分享 >git hook- prepare-commit-msg

git hook- prepare-commit-msg

时间:2022-12-30 21:00:12浏览次数:49  
标签:git BRANCHES prepare AUTHORINFO hook msg 033

1.不能提交到 master production main 这几个分支
2.分支名只能以feature|hotfix|bugfix|release|dev|improvement 这几个开头
3.自动加分支名到提交的消息上
4.变基的情况合并提交不会触发这个hook,处理掉了

#!/bin/bash

RED="\033[1;31m"
GREEN="\033[1;32m"
ORANGE="\033[0;33m"
NOCOLOR="\033[0m"

if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master production main)
fi

AUTHORINFO=$(git var GIT_AUTHOR_IDENT) || exit 1
NAME=$(printf '%s\n\n' "${AUTHORINFO}" | sed -n 's/^\(.*\) <.*$/\1/p')
VALID_BRANCH_REGEX="(^(feature|hotfix|bugfix|release|dev|improvement)(-[0-9]+$)?)"

BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
REBASING=$(printf "%s\n" "$BRANCH_NAME" | grep -c "rebasing")
if [[ ! $REBASING -ge 1 ]];then
	BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")

	# A developer has already prepended the commit in the format BRANCH_NAME
	BRANCH_IN_COMMIT=$(grep -c "$BRANCH_NAME" $1)

	# check the branch name is valid or not
	if [[ "$BRANCH_NAME" =~ $VALID_BRANCH_REGEX ]]; then
	  # if face any error in mac then run chmod u+x .git/hooks/prepare-commit-msg and restart your terminal
	  if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then 
	    sed -i.bak -e "1s/^/$BRANCH_NAME: /" $1
	  fi
	else
	  echo -e "\n${RED}分支名称不规范,或正在向不允许的提交的分支提交代码,操作中断${NOCOLOR}"
	  printf "\n分支名称需要以下面几种类型开头:  "
	  echo -e "${GREEN}dev, hotfix, bugfix, release, dev, improvement, 类型-数字\n"
	  echo -e "${ORANGE}不能直接提交的分支: ${BRANCHES_TO_SKIP[@]}\n"
	  echo -e "${ORANGE}当前分支名称: ${BRANCH_NAME}"
	  echo -e "\n${RED}请修改正确的分支后再提交: ${BRANCH_NAME}${NOCOLOR}"
	  exit 1;
	fi
fi

标签:git,BRANCHES,prepare,AUTHORINFO,hook,msg,033
From: https://www.cnblogs.com/nocanstillbb/p/17015809.html

相关文章