Skip to content

Instantly share code, notes, and snippets.

@knuch
Last active March 1, 2023 09:13
Show Gist options
  • Save knuch/0e4307c0de8979aa3c24d8e6d3f0ab34 to your computer and use it in GitHub Desktop.
Save knuch/0e4307c0de8979aa3c24d8e6d3f0ab34 to your computer and use it in GitHub Desktop.
add jira ticket prefix to commit message
#!/bin/sh
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
CURR_MESSAGE=$(cat $COMMIT_MSG_FILE)
# if current commit message starts with a "["
if [ $(echo $CURR_MESSAGE | cut -c1) = "[" ]; then
exit 0
fi
# if commit is coming from anything other than a simple commit, exit (eg. merge, revert, amend, etc.)
if [ "$COMMIT_SOURCE" != "message" -a ! -z "$COMMIT_SOURCE" ]; then
exit 0
fi
# extract ticket number from branch name and add it to commit message
TICKET=$(git rev-parse --abbrev-ref HEAD | grep -Eo "/\w+-[0-9]+" | head -n1 | cut -c2- | tr a-z A-Z)
if [ -z $TICKET ];then
exit 0;
fi
echo "[$TICKET] $CURR_MESSAGE" > $COMMIT_MSG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment