Skip to content

Instantly share code, notes, and snippets.

@gregbook
Created October 21, 2020 15:47
Show Gist options
  • Save gregbook/9fd954d10b7a13b327cd652a2d416ece to your computer and use it in GitHub Desktop.
Save gregbook/9fd954d10b7a13b327cd652a2d416ece to your computer and use it in GitHub Desktop.
Git Client Hook prepare-commit-msg
#!/bin/sh
#
# git prepare-commit-msg hook for automatically prepending an issue key
# from the start of the current branch name to commit messages.
# check if commit is merge commit or a commit ammend
if [ $2 = "merge" ] || [ $2 = "commit" ]; then
exit
fi
# Use of git symbolic-ref as git branch doesn't return yet to be born branches in case of a new branch
ISSUE_KEY=`git symbolic-ref --short HEAD | grep -o "\(.*/\)*[A-Z]\{2,\}-[0-9]\+"`
if [ $? -ne 0 ]; then
# no issue key in branch, use the default message
exit
fi
# issue key matched from branch prefix, prepend to commit message
sed -i -e "1 s/^/$ISSUE_KEY - /" $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment