#!/bin/sh
COMMIT_MSG_FILE=$1
# Define branches to skip
BRANCHES_TO_SKIP="master develop test"
# Check if the hook is running during a rebase
if [ -d ".git/rebase-merge" ] || [ -d ".git/rebase-apply" ]; then
echo "Skipping prepare-commit-msg hook during rebase"
exit 0
fi
# Get the current branch name
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
# Exit if the current branch is in the skip list
if echo "$BRANCHES_TO_SKIP" | grep -qw "$BRANCH_NAME"; then
echo "Skipping commit message formatting for branch: $BRANCH_NAME"
exit 0
fi
# will auto transfer branch name to special words
# Eamples of branch to prefix:
# JIRA-1234 -> JIRA-1234: <COMMIT_MSG>
# feature/JIRA-1234 -> JIRA-1234: <COMMIT_MSG>
# JIRA-1234_my_feature -> JIRA-1234: <COMMIT_MSG>
TICKET=$(echo "$BRANCH_NAME" | grep -oE 'JIRA-[0-9]+')
# Check for merge commit
if [ -f ".git/MERGE_HEAD" ]; then
exit 0
fi
# Get the commit message without comments
MESSAGE=$(grep -v '^#' "$COMMIT_MSG_FILE" | sed '/^$/d')
# Abort if the commit message is empty
if [ -z "$MESSAGE" ]; then
echo "Aborting commit due to empty commit message."
exit 1
fi
# Prepend the ticket number or branch name to the commit message
if [ -n "$TICKET" ]; then
echo "$TICKET: $MESSAGE" > "$COMMIT_MSG_FILE"
else
echo "$BRANCH_NAME: $MESSAGE" > "$COMMIT_MSG_FILE"
fi