Skip to content

Instantly share code, notes, and snippets.

@stacyhorton
Created April 20, 2014 08:40
Show Gist options
  • Save stacyhorton/11108805 to your computer and use it in GitHub Desktop.
Save stacyhorton/11108805 to your computer and use it in GitHub Desktop.
A bash Git prepare-commit-msg hook to work with Git Flow and Jira style issue keys
#!/bin/bash +x
#The issue prefix to look for. This must be edited to match your project.
ISSUE_PREFIX=feature/ABC-
#At which line the issue should be inserted. Usually either "1" or "3".
INSERT_AT_LINE=1
#A separator which will be added after the issue is inserted. For readability.
COMMIT_TEXT_SEPARATOR=" "
#Check if it's a "fixup" or "squash" commit. If so we shouldn't alter anything.
FIRST_LINE=`head -n 1 "$1"`
if [[ x$FIRST_LINE == xfixup!* || x$FIRST_LINE == xsquash!* ]]
then
exit
fi
BRANCH_NAME=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 4`
ISSUE_NAME=`echo $BRANCH_NAME | egrep -o "[A-Z]+-[0-9]+"`
if [[ x$ISSUE_NAME != x ]]
then
#Insert any needed new lines.
for (( i=1; i<${INSERT_AT_LINE}; i++ ))
do
sed -i '1i \
' "$1"
done
sed -i "${INSERT_AT_LINE}i ${ISSUE_NAME}${COMMIT_TEXT_SEPARATOR}" "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment