Skip to content

Instantly share code, notes, and snippets.

@ehuynh
Last active June 3, 2021 10:45
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 ehuynh/f184258a9b6943369aabb7437363e5a9 to your computer and use it in GitHub Desktop.
Save ehuynh/f184258a9b6943369aabb7437363e5a9 to your computer and use it in GitHub Desktop.
prepare-commit-msg hook for populating commit message template with the JIRA ticket number as a prefix
#!/bin/sh
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
#/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE"
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop)
fi
# Get the current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Ticket name is retrieved as a string between the first slash and the second hyphen
TICKET_NAME=$(echo $BRANCH_NAME | sed -e 's/.*\([A-Z]\{3\}-[0-9]\{3\}\).*/\1/')
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
ALREADY_IN_MSG=$(grep -c "\[$TICKET_NAME\]" $COMMIT_MSG_FILE)
# If it isn't excluded or already in commit message, prepend the ticket name to the given message
if ! [ "$TICKET_NAME" == "$BRANCH_NAME" ] && [ -n "$BRANCH_NAME" ] && ! [[ "$BRANCH_EXCLUDED" -eq 1 ]] && ! [[ "$ALREADY_IN_MSG" -eq 1 ]]; then
sed -i.bak -e "1s/^/[$TICKET_NAME] /" "$COMMIT_MSG_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment