Skip to content

Instantly share code, notes, and snippets.

@Nifled
Last active October 15, 2024 22:29
Show Gist options
  • Save Nifled/abd06ed2456ffd620bc512f61dd44ae0 to your computer and use it in GitHub Desktop.
Save Nifled/abd06ed2456ffd620bc512f61dd44ae0 to your computer and use it in GitHub Desktop.
Git hook to prefix commit messages with Jira ticket ids (e.g. `SER-8736`). 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='([A-Z]{3,4}-[0-9]{4})'
# 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