Skip to content

Instantly share code, notes, and snippets.

@kerasai
Last active November 4, 2020 22:44
Show Gist options
  • Save kerasai/04071b7cf50bb6e5a9cda5315d2333d9 to your computer and use it in GitHub Desktop.
Save kerasai/04071b7cf50bb6e5a9cda5315d2333d9 to your computer and use it in GitHub Desktop.
Script for Git Prepare Commit Message Hook - Attempts to prefix with JIRA issue number
#!/bin/bash
# Looks for a JIRA style ticket name in the branch name, and prefixes the
# commit message with the ticket, if found.
#
# If you're using the `git commit -m` style commit command, the issue number
# will be prefixed without notice.
#
# If you use the standard `git commit` command where you're sent to an editor,
# the issue number will appear as the default commit message in the editor.
# You'll then edit to add the remainder of commit message to complete the
# commit, or delete the contents to abort the commit.
# Get the current branch name, convert to uppercase.
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME=`echo "$BRANCH_NAME" | awk '{ print toupper($0) }'`
# Attempt to match a pattern in the branch name.
PATTERN='([A-Z]+-[0-9]+)'
if [[ $BRANCH_NAME =~ $PATTERN ]]; then
TICKET_NAME=${BASH_REMATCH[1]}
else
exit 0
fi
# Write the output file, if provided.
if [ ! -z "$1" ]; then
sed -i.bak -e "1s/^/${TICKET_NAME}: /" "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment