Skip to content

Instantly share code, notes, and snippets.

@bhenderson
Last active May 18, 2023 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhenderson/1f2e5510bf66235590f3e76a7c507284 to your computer and use it in GitHub Desktop.
Save bhenderson/1f2e5510bf66235590f3e76a7c507284 to your computer and use it in GitHub Desktop.
Prepend ticket number (jira) to commit messages

Save the file to .git/hooks and make executable (chmod u+x prepare-commit-msg)

#!/bin/sh
#
# Try to get the ticket number from the current branch and prepend it to the message.
# The current ticket regex matches jira numbers: JIRA-123
# This works with both `git commit` and `git commit -m`
#
# https://gist.github.com/bhenderson/1f2e5510bf66235590f3e76a7c507284
# hat tip: https://betterprogramming.pub/how-to-automatically-add-the-ticket-number-in-git-commit-message-bda5426ded05​
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
BRANCH=$(git rev-parse --abbrev-ref HEAD)
TICKET=$(echo $BRANCH | grep -o -E '\w{2,3}-\d+' | head -n 1)
MESSAGE=$(cat $COMMIT_MSG_FILE)
# if not ticket found OR message already has ticket, abort.
if [[ $TICKET == "" || "$MESSAGE" == "$TICKET"* || "$MESSAGE" == "fixup!"* ]];then
exit 0;
fi
# prepent ticket.
echo "$TICKET $MESSAGE" > $COMMIT_MSG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment