Save the file to .git/hooks
and make executable (chmod u+x prepare-commit-msg
)
Last active
October 28, 2024 21:13
-
-
Save bhenderson/1f2e5510bf66235590f3e76a7c507284 to your computer and use it in GitHub Desktop.
Prepend ticket number (jira) to commit messages
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/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