Skip to content

Instantly share code, notes, and snippets.

@GabrielCzar
Last active July 26, 2023 04:48
Show Gist options
  • Save GabrielCzar/e1dc9773830701015699fbc574b7e46c to your computer and use it in GitHub Desktop.
Save GabrielCzar/e1dc9773830701015699fbc574b7e46c to your computer and use it in GitHub Desktop.
Git hook prepare-commit-msg to append Jira id into the commit message
#!/bin/sh
# prevents the display of color codes on colorless consoles
if test -t 1; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BOLD='\e[01m'
RESET='\033[0m'
fi
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
COMMIT_MSG=$(cat $1)
HEAD_COMMIT_MSG=$(head -n 1 $1 | grep -v '^#')
FIRST_LINE_COMMIT_MSG=$(cat $1 | grep -v '^#' | grep -v '^\s*$' | head -n 1)
if [ -z "$HEAD_COMMIT_MSG" ]; then
# correct the first commit line by using first not empty message
COMMIT_MSG="$FIRST_LINE_COMMIT_MSG\n$COMMIT_MSG"
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID_REGEX="[A-Z0-9]{1,10}-{1}[A-Z0-9]+"
JIRA_ID_IN_CURRENT_BRANCH_NAME=$(echo "$CURRENT_BRANCH" | grep -Eo "$JIRA_ID_REGEX")
JIRA_ID_IN_COMMIT_MESSAGE=$(echo "$FIRST_LINE_COMMIT_MSG" | grep -m 1 -Eo "$JIRA_ID_REGEX")
if [ -n "$JIRA_ID_IN_CURRENT_BRANCH_NAME" ]; then
if [ -n "$JIRA_ID_IN_COMMIT_MESSAGE" ]; then
if [ "$JIRA_ID_IN_COMMIT_MESSAGE" != "$JIRA_ID_IN_CURRENT_BRANCH_NAME" ]; then
echo "${YELLOW}[Warning] The JIRA ID you specified in your commit message ${BOLD}'$JIRA_ID_IN_COMMIT_MESSAGE'${YELLOW} is not equal to current branch JIRA ID ${BOLD}'$JIRA_ID_IN_CURRENT_BRANCH_NAME'.${RESET}"
else
echo "${GREEN}JIRA ID ${BOLD}'$JIRA_ID_IN_COMMIT_MESSAGE'${GREEN} matched in commit message.${RESET}"
fi
else
# Update this if you want to add more context to the message using prefixes and scope, remember to disable the previous rule
# PREFIX=$(echo "$FIRST_LINE_COMMIT_MSG" | grep -Eo "(add|fix|docs|build|update)" | head -n 1 | sed -e 's/add/feat/g;s/update/feat/g')
# SCOPE=$(echo "$JIRA_ID_IN_CURRENT_BRANCH_NAME" | tr '[:upper:]' '[:lower:]')
# echo "${GREEN}JIRA ID ${BOLD}'$JIRA_ID_IN_CURRENT_BRANCH_NAME'${GREEN}, matched is current branch name, preprended to commit message.${RESET}"
# echo "$PREFIX($SCOPE): $COMMIT_MSG" > ${COMMIT_MSG_FILE}
echo "${GREEN}JIRA ID ${BOLD}'$JIRA_ID_IN_CURRENT_BRANCH_NAME'${GREEN}, matched is current branch name, preprended to commit message.${RESET}"
echo "$JIRA_ID_IN_CURRENT_BRANCH_NAME: $COMMIT_MSG" > ${COMMIT_MSG_FILE}
fi
fi
@GabrielCzar
Copy link
Author

GabrielCzar commented Aug 4, 2019

Install

  1. Copy the prepare-commit-msg script into your local git repository inside .git/hooks.
  2. Make the script executable by running chmod +x prepare-commit-msg.

How does it work?

If the current branch is a Feature/Release/Hotfix branch with the related Jira ID, this hook checks the actual commit message for the Jira ID.

Available ways to perform:

  1. No Jira ID found in the commit message:
    This hook will prepend Jira ID to the commit message.
  2. Has Jira ID in commit message:
    This hook will check if the Jira ID found is the same as the current branch case, if different it will send a warning message.

Example of branch name: feature/ABC-1234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment