Skip to content

Instantly share code, notes, and snippets.

@anton-matosov
Last active September 7, 2015 02:41
Show Gist options
  • Save anton-matosov/2135acb3427feb4a4f23 to your computer and use it in GitHub Desktop.
Save anton-matosov/2135acb3427feb4a4f23 to your computer and use it in GitHub Desktop.
git commit-msg hook to put JIRA ticket ID (or other ID matching pattern) in front of the message
#!/bin/sh
#
# Automatically adds JIRA ticket ID based on branch name to every commit message.
#
set -x
MESSAGE=$(cat "$1")
BASEREGEX="([A-Z]{1,10}-[0-9]*)"
MSG_REGEX=$BASEREGEX
if [[ $MESSAGE =~ $MSG_REGEX ]]
then
echo branch is already in the message
exit
fi
BRANCH_REGEX="//?$BASEREGEX"
BRANCHNAME=$(git symbolic-ref -q HEAD)
[[ $BRANCHNAME =~ $BRANCH_REGEX ]]
TICKET="${BASH_REMATCH[1]}"
if [[ -n $TICKET ]]
then
echo "$TICKET "$(cat "$1") > "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment