Created
April 3, 2021 14:23
-
-
Save bretonics/e3ea90efb3084034bccc60f770a2d37c to your computer and use it in GitHub Desktop.
Git Hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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