Skip to content

Instantly share code, notes, and snippets.

@bretonics
Created April 3, 2021 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bretonics/e3ea90efb3084034bccc60f770a2d37c to your computer and use it in GitHub Desktop.
Save bretonics/e3ea90efb3084034bccc60f770a2d37c to your computer and use it in GitHub Desktop.
Git Hooks
#!/bin/sh
#
# Adapted from original source: https://bitbucket.org/snippets/atlassian/qedp7d
# Docs: https://git-scm.com/docs/githooks
# git prepare-commit-msg hook for automatically prepending an issue key
# from the start of the current branch name to commit messages.
COMMIT_MSG_FILE=$1 # $1 is the file name containing log message
COMMIT_SOURCE=$2 # $2 is the commit message source (message | template | squash | commit)
SHA1=$3 # $3 is the commit SHA-1
# Check a commit ammend
if [ $COMMIT_SOURCE = "merge" ] || [ $COMMIT_SOURCE = "commit" ]; then
exit
fi
# Ex.) feature/TICKET-1234
# - ISSUE_KEY = "TICKET-1234"
ISSUE_KEY=`git branch | grep -o "\* \(.*/\)*[A-Z]\{2,\}-[0-9]\+" | grep -o "[A-Z]\{2,\}-[0-9]\+"`
# If no issue key in branch, use the default message
if [ -z $ISSUE_KEY ]; then
exit
fi
# Issue key matched from branch prefix
# Prepend ticket ID to commit message
sed -i -e "1s/^/$ISSUE_KEY: /" $COMMIT_MSG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment