findTypeIdFromBranch() { local BRANCH=$(git branch | grep '*' | sed 's/* //') if [[ ! "$BRANCH" =~ $BRANCH_REX ]]; then return 1 fi local TYPE_ID=$($BRANCH#$BRANCH_PREFIX | grep -Eo '^[SBT]{1}[0-9]+') findTypeId $TYPE_ID }
findTypeIdFromMsg() { local MSG=$1 local TYPE_ID=$(echo$MSG | grep -Eo "^#[SBT]{1}[0-9]+" | tr -d '#') echo$TYPE_ID if [ -z $TYPE_ID ]; then return 1 fi
findTypeId $TYPE_ID }
findTypeId() { local TYPE_ID=$1 if [ ${#TYPE_ID} -lt 1 ]; then return 1 fi
# --- 如果已经添加关联,则不做处理 --- if [[ $COMMIT_MSG =~ $TAPD_REFER_MSG_REX ]]; then exit 0 fi
findTypeIdFromMsg $COMMIT_MSG FAIL=$?
if [ $FAIL -eq 1 ]; then findTypeIdFromBranch FAIL=$? fi
if [ $FAIL -eq 1 ]; then echo"WARN: The format of branch name and commit message is incorrect." echo"The format of branch should be ${BRANCH_PREFIX}[STB]<tapdId> (example: ${BRANCH_PREFIX}S12345);" echo"Or the commit message should start with #[STB]<tapdId> (example: #S12345, message)." # 格式不符合,中止提交 exit 0 fi
# --- 判断必要环境变量 ---
if [ -z $TAPD_USERNAME ]; then echo"WARN: environment value TAPD_USERNAME is required." echo"You can config with the following commands. (Replace [yourname] with your name in Tapd. Using .zshrc instead of .bash_profile if zsh)" printf"\n\t%s\n\t%s\n\n""echo -e '\nexport TAPD_USERNAME=\"[yourname]\"' >> ~/.bash_profile""source ~/.bash_profile" exit 0 fi
# --- 如果已经添加前缀,则不做处理 --- if [[ $COMMIT_MSG =~ $TAPD_MSG_PREFIX_REX.* ]]; then exit 0 fi
findIssueIdFromBranch FAIL=$?
if [ $FAIL -eq 1 ]; then echo"WARN: The branch name format shoud be ${BRANCH_PREFIX}<issue number>, example ${BRANCH_PREFIX}100011" # 强制格式,则可以`exit 1`以执行失败 exit 0 fi