Created
December 21, 2021 14:31
-
-
Save withakay/acbe9d92b57edf7eb0915652b17da7a5 to your computer and use it in GitHub Desktop.
prepend Jira ticket numbers 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/bash | |
# | |
# .git/hooks/commit-msg is run when the -m flag is used | |
# | |
# Will extract Jira style ticket numbers from branch names e.g. | |
# 'AB-123-my-cool-branch' --> '[AB-123]' | |
# 'feature/XZ-321-my-cool-branch' --> '[XZ-321]' | |
# | |
TICKET=[$(git rev-parse --abbrev-ref HEAD | grep -Eo '^(\w+/)?(\w+[-_])?[0-9]+' | grep -Eo '(\w+[-])?[0-9]+' | tr "[:lower:]" "[:upper:]")] | |
if [[ $TICKET == "[]";then | |
exit 0; | |
fi | |
# prepends the ticket captured above to the message passed e.g. | |
# git commit -m "fixed a bug" --> "[AB-123] fixed a bug" | |
echo "$TICKET " >> "$1" |
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/bash | |
# | |
# .git/hooks/prepare-commit-msg is run when the -m flag is NOT used | |
# | |
# Will extract Jira style ticket numbers from branch names e.g. | |
# 'AB-123-my-cool-branch' --> '[AB-123]' | |
# 'feature/XZ-321-my-cool-branch' --> '[XZ-321]' | |
# | |
FILE=$1 | |
MESSAGE=$(cat $FILE) | |
TICKET=[$(git rev-parse --abbrev-ref HEAD | grep -Eo '^(\w+/)?(\w+[-_])?[0-9]+' | grep -Eo '(\w+[-])?[0-9]+' | tr "[:lower:]" "[:upper:]")] | |
if [[ $TICKET == "[]" || "$MESSAGE" == "$TICKET"* ]];then | |
exit 0; | |
fi | |
# prepends the ticket captured above to the message file | |
echo "$TICKET $MESSAGE" > $FILE | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses bits of code from https://betterprogramming.pub/how-to-automatically-add-the-ticket-number-in-git-commit-message-bda5426ded05