Skip to content

Instantly share code, notes, and snippets.

@bhenderson
Last active October 28, 2024 21:13
Show Gist options
  • 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
# prepend 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