Skip to content

Instantly share code, notes, and snippets.

@Nifled
Last active April 7, 2022 05:35
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 Nifled/40490167652da755152cac0b42713a6c to your computer and use it in GitHub Desktop.
Save Nifled/40490167652da755152cac0b42713a6c to your computer and use it in GitHub Desktop.
Script to automatically add JIRA ticket reference as a prefix to commit messages. File must be within project's `/.git/hooks/`
#!/bin/sh
#
# Commit hook to add a JIRA ticket reference as a prefix to any commit messages.
# NOTE: JIRA ticket number MUST be included in branch name or it does nothing...
# Doesn't make any specific checks, just does it.
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
REGEX_TICKET_ID='(SOFT-[0-9]{4})' # SOFT can be replaced by any JIRA team name
# Do the regex search
[[ $BRANCH_NAME =~ $REGEX_TICKET_ID ]]
# regex match
ticket_id=${BASH_REMATCH[1]}
# prefix the JIRA ticket id to commit message ($1)
echo "$ticket_id"': '$(cat "$1") > "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment