Skip to content

Instantly share code, notes, and snippets.

@john-humi
Last active March 25, 2020 00:12
Show Gist options
  • Select an option

  • Save john-humi/567f620193828980f9114544b123d909 to your computer and use it in GitHub Desktop.

Select an option

Save john-humi/567f620193828980f9114544b123d909 to your computer and use it in GitHub Desktop.
Add jira ticket to start of commit message

Adds a JIRA ticket number to Commit Message - taken from branch name

  1. Add script above to: .git/hooks/prepare-commit-msg
  2. Make executable chmod +x .git/hooks/prepare-commit-msg.

Any branch with names using JIRA ticket ids like ABC-1234-anything or DEF-234, will have the JIRA identifier prepended to commit messages.

#!/bin/sh
# Checks if current branch matches JIRA pattern (ABC-1234-anything), then adds ABC-1234 to start of commit message.
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
REGEX='^([a-zA-Z]{3}-[0-9]+)-*'
if [ -n "$BRANCH_NAME" ]; then
[[ $BRANCH_NAME =~ $REGEX ]]
if [ -n "$BASH_REMATCH" ]; then
echo "${BASH_REMATCH[1]} $(cat $1)" > $1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment