Skip to content

Instantly share code, notes, and snippets.

@snufkin
Created June 25, 2015 12:16
Show Gist options
  • Save snufkin/af16dd665cc2f6f8d67a to your computer and use it in GitHub Desktop.
Save snufkin/af16dd665cc2f6f8d67a to your computer and use it in GitHub Desktop.
Prepend JIRA ticket ID from branch name and validate if it is missing
#!/bin/sh
#
# To enable this hook, developers should place it in .git/hooks/.
#
# You may adapt the message length check. Currently checking it's longer than 15 characters.
# First try to populate the ticket prefix based on the active branch
# The current branch worked on
branch=`git symbolic-ref --short HEAD`
# The regular expression to match the ticket ID
regex='\(WM-[0-9]\{1,4\}\).*'
# The ticket ID extracted from the branch name
ticket=$(echo $branch | sed -e "s/^$regex/\1/");
# The prefix pattern prepended before each commit message
prefix="$ticket - "
msg_text=`grep -e ''"$regex"'' "$1"`
# Ticked ID is not in the commit message
if [ "$msg_text" == "" ]; then
# Test if the ticket ID is not in the commit message and the branch name does not specify it either
if [ "$ticket" == "" ]; then
echo "No ticket ID found in the commit message."
echo "Valid example: WM-14 - Adding Paragraph bundle to the generic page feature"
exit 1
# Ticket ID is not in the commit message, but present in the branch
else
sed "1s/^/$prefix /" $1 > $1.new
mv $1.new $1
fi
# Ticket ID is in the commit message, everything is fine
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment