Skip to content

Instantly share code, notes, and snippets.

@evan-moon
Last active October 9, 2019 07:16
Show Gist options
  • Save evan-moon/e64f8a8c9cedcb6e5a42697cb81a9629 to your computer and use it in GitHub Desktop.
Save evan-moon/e64f8a8c9cedcb6e5a42697cb81a9629 to your computer and use it in GitHub Desktop.
커밋 전에 JIRA 이슈 번호를 브랜치에서 따와서 넣는 쉘 스크립트를 Git의 prepare-commit-msg 훅에 입력
#!/bin/bash
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
NO_COLOR='\033[0m'
TILDE='~'
echo -e "[${YELLOW}Insert path of your project directory.${NO_COLOR}]"
read -p '(ex. ~/dev/playground/proejct_name) -> ' PROEJCT_PATH
PROEJCT_PATH=${PROEJCT_PATH//$TILDE/$HOME}
GIT_DIR=$PROEJCT_PATH'/.git'
if [ ! -d "$GIT_DIR" ]; then
echo -e "[${RED}ERROR${NO_COLOR}] This project is not a git project!"
exit 0
fi
echo -e "[${YELLOW}INFO${NO_COLOR}] Find git directory..."
PREPARE_COMMIT_HOOK_FILE=$GIT_DIR'/hooks/prepare-commit-msg'
HOOK=$(cat << 'EOF'
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop release hotfix)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
JIRA_ID=`echo $BRANCH_NAME | egrep -o 'SD.?-[0-9]+'`
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "$JIRA_ID" $1)
if [ -n $JIRA_ID ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/$JIRA_ID /" $1
fi
EOF
)
echo -e "[${YELLOW}INFO${NO_COLOR}] Find git hook file..."
if [ ! -f "$PREPARE_COMMIT_HOOK_FILE" ]; then
echo -e "[${YELLOW}INFO${NO_COLOR}] There is no prepare-commit-msg hook file. It will be created."
touch $PREPARE_COMMIT_HOOK_FILE
chmod 755 $PREPARE_COMMIT_HOOK_FILE
echo "#!/bin/bash" >> $PREPARE_COMMIT_HOOK_FILE
else
echo -e "[${YELLOW}INFO${NO_COLOR}] prepare-commit-msg hook file is already exist. It will be added to tail of this file."
fi
echo "$HOOK" >> $PREPARE_COMMIT_HOOK_FILE
echo -e "[${YELLOW}INFO${NO_COLOR}] Please check your prepare-commit-msg hook file."
echo -e "${GREEN}FINISHED!"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment