starter-kit/.gitea/hooks/prepare-commit-msg
2025-05-21 12:39:40 +07:00

35 lines
1.1 KiB
Bash

#!/bin/sh
# prepare-commit-msg hook for ULFlow Golang Starter Kit
# This hook prepares the commit message template
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
# Only add template if this is not a merge, amend, or rebase
if [ -z "$COMMIT_SOURCE" ]; then
# Check the current branch name to suggest commit type
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Extract type from branch name (e.g., feat/123-user-auth -> feat)
BRANCH_TYPE=$(echo $BRANCH_NAME | cut -d '/' -f 1)
# If branch follows naming convention, prepopulate commit message
if [[ "$BRANCH_TYPE" =~ ^(feat|fix|docs|style|refactor|test|chore)$ ]]; then
# Get the first line of the current commit message
first_line=$(head -n 1 $COMMIT_MSG_FILE)
# If the first line doesn't already have the type, prepend it
if [[ ! "$first_line" =~ ^$BRANCH_TYPE ]]; then
# Read existing commit msg
commit_msg=$(cat $COMMIT_MSG_FILE)
# Create new commit msg with suggested type
echo "$BRANCH_TYPE: " > $COMMIT_MSG_FILE
echo "$commit_msg" >> $COMMIT_MSG_FILE
fi
fi
fi
exit 0