#!/bin/bash
# Source: https://gist.github.com/bartoszmajsak/1396344#gistcomment-65489
# For a quick install, into your project folder enter this command line:
#
# ```
# $ mkdir -p .git/hooks/ && curl https://gist.githubusercontent.com/FBente/19c3559c65cf3daa802e921438557370/raw/020c11b769c6f1535b7966c42d4c3fa670ccf3b6/prepare-commit-msg.sh > .git/hooks/prepare-commit-msg && chmod u+x .git/hooks/prepare-commit-msg
# ```
# * get Gist
# * copy into _.git/hooks/prepare-commit-msg_ file
# * make it executable
# This way you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test integration archive release)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[$BRANCH_NAME] /" $1
else
sed -i.bak -e "1s/^/[NOTICKET] /" $1
fi