Last active
March 10, 2019 09:57
-
-
Save enis-ismail/f303ad674a75f9dcaaa703cb129097cb to your computer and use it in GitHub Desktop.
Git hooks: prepare-commit-msg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Instructions to make hooks executable: | |
#- for Windows (attrib +x prepare-commit-msg) | |
#- for Linux (chmod +x prepare-commit-msg) | |
# -------------------------------------------------------------------------------------- # | |
# Retrieve Ticket ID from the branch name and insert it as prefix for the commit message # | |
# -------------------------------------------------------------------------------------- # | |
branch_name="$(git symbolic-ref --short HEAD)" | |
regex='(feature|hotfix|bugfix)\/([[:alpha:]]+-[[:digit:]]+)' | |
if [[ $branch_name =~ $regex ]]; then | |
TICKET_ID=${BASH_REMATCH[2]} | |
COMMIT_EDITMSG_FILE="$1" | |
COMMIT_MESSAGE="$(cat $COMMIT_EDITMSG_FILE)" | |
# Check if the same prefix is not already added in the commit message | |
if ! [[ $COMMIT_MESSAGE =~ ^$TICKET_ID ]]; then | |
echo "$TICKET_ID"' '"$COMMIT_MESSAGE" > $COMMIT_EDITMSG_FILE | |
fi | |
elif [ $branch_name != "develop" ]; then | |
echo "Incorrect branch name. If you want to skip the prepare-commit-msg hook just add --no-verify: git commit --no-verify" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment