Skip to content

Instantly share code, notes, and snippets.

@withakay
Created December 21, 2021 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save withakay/acbe9d92b57edf7eb0915652b17da7a5 to your computer and use it in GitHub Desktop.
Save withakay/acbe9d92b57edf7eb0915652b17da7a5 to your computer and use it in GitHub Desktop.
prepend Jira ticket numbers to commit messages
#!/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"
#!/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