Skip to content

Instantly share code, notes, and snippets.

@rafaltrzop
Last active October 17, 2018 12:51
Show Gist options
  • Save rafaltrzop/c211cad3b02ef7e10699efd996b9aefc to your computer and use it in GitHub Desktop.
Save rafaltrzop/c211cad3b02ef7e10699efd996b9aefc to your computer and use it in GitHub Desktop.
Git prepare-commit-msg hook adding JIRA issue key before git commit message based on your branch name e.g. feature/KEY-1234
#!/bin/sh
###############################################################################
# Setup #
###############################################################################
# Use global hooks directory for each existing and new repository:
# $ git config --global core.hooksPath '~/.git-hooks'
# $ mkdir ~/.git-hooks
# $ chmod a+x ~/.git-hooks/prepare-commit-msg
# Or automatically copy your hooks to each new repository:
# $ git config --global init.templatedir '~/.git-templates'
# $ mkdir -p ~/.git-templates/hooks
# $ chmod a+x ~/.git-templates/hooks/prepare-commit-msg
# How to remove global git configuration:
# $ git config --global --list
# $ git config --global --unset core.hooksPath
# $ git config --global --unset init.templatedir
###############################################################################
# Hook #
###############################################################################
BRANCH_NAME="$(git symbolic-ref --short HEAD)"
ISSUE_KEY="$(echo $BRANCH_NAME | grep -oEe '[A-Z0-9]+-[0-9]+')"
COMMIT_MESSAGE="$(cat $1)"
COMMIT_SOURCE=$2
# echo $BRANCH_NAME
# echo $ISSUE_KEY
# echo "$COMMIT_MESSAGE"
# echo $COMMIT_SOURCE
if [[ -n $ISSUE_KEY && $COMMIT_SOURCE == 'message' ]]
then
echo "[$ISSUE_KEY] $COMMIT_MESSAGE" > $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment