-
-
Save luc-tielen/01068c757fe4e56531529b3ed3282315 to your computer and use it in GitHub Desktop.
Git pre commit hook to add the jira ticket id to the commit message
This file contains hidden or 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/sh | |
# A hook script to prepare the commit log message if the branch name starts with a JIRA ticket. | |
# It adds the branch name to the commit message, if it is not already part of it. | |
# https://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message/59831864#59831864 | |
PROJECT_PREFIX="PROJ" # TODO fill in project here | |
BRANCH_PATH=$(git symbolic-ref -q HEAD) # Something like refs/heads/branch_name | |
BRANCH_NAME=${BRANCH_PATH##*/} # Get text behind the last / of the branch path | |
shopt -s nocasematch # Enable case insensitive match, jira project codes are uppercase | |
REGEX="(${PROJECT_PREFIX}-[0-9]*)" | |
if [[ $BRANCH_NAME =~ $REGEX ]]; then | |
# Get the captured portion of the branch name, and change it to upper case. | |
# Mac bash version is 3, so can't use the `^^` to uppercase the string, using `tr` command instead. | |
# If you do not need the uppercase, use the BASH_REMATCH dirrectly | |
# JIRA_TICKET=${BASH_REMATCH[1]} | |
JIRA_TICKET=$(echo ${BASH_REMATCH[1]} | tr [:lower:] [:upper:]) | |
ORIGINAL_MESSAGE=`cat $1` | |
# If the message already begins with $PROJECT_NAME-#, do not edit the commit message. | |
if [[ $ORIGINAL_MESSAGE == $JIRA_TICKET* ]]; then | |
exit | |
fi | |
sed -i.bak -e "1s/^/$JIRA_TICKET: /" $1 # Insert branch name at the start of the commit message file | |
fi | |
# else => do not edit the commit message. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions: