commit-msgā¢2.53 kB
#!/bin/bash
# Git commit-msg hook to validate conventional commit format
# This enforces the commit message format for automated versioning
commit_regex='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|major|feature|bug|hotfix)(\([a-z0-9-]+\))?!?:[[:space:]].+|^BREAKING CHANGE:[[:space:]].+'
error_msg="
ā Invalid commit message format!
Your commit message must follow the Conventional Commits specification:
<type>[optional scope]: <description>
Valid types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert, major, feature, bug, hotfix
Version bumping (automatic):
š“ MAJOR: feat!, major:, BREAKING CHANGE:
š” MINOR: feat:
š¢ PATCH: fix:, docs:, style:, refactor:, perf:, test:, build:, ci:, chore:
āŖ NONE: revert:, feature:, bug:, hotfix:
Examples:
ā
feat: add new password generator tool (minor bump)
ā
fix: resolve base64 encoding issue (patch bump)
ā
docs: update README examples (patch bump)
ā
feat!: change API structure (major bump)
ā
revert: undo previous feature (no bump)
For more info, see: COMMIT_TEMPLATE_SETUP.md
"
# Read the commit message
commit_message=$(cat "$1")
# Skip merge commits and revert commits with standard format
if echo "$commit_message" | grep -qE "^(Merge|Revert)"; then
exit 0
fi
# Skip automated version bump commits
if echo "$commit_message" | grep -qE "^chore: bump version to v[0-9]+\.[0-9]+\.[0-9]+$"; then
exit 0
fi
# Validate the commit message format
if ! echo "$commit_message" | grep -qE "$commit_regex"; then
echo "$error_msg" >&2
echo "Your commit message:" >&2
echo " $commit_message" >&2
exit 1
fi
# Additional check for BREAKING CHANGE in footer
if echo "$commit_message" | grep -qE "BREAKING CHANGE:"; then
echo "ā
Breaking change detected - will trigger MAJOR version bump"
elif echo "$commit_message" | grep -qE "^(feat!|major):"; then
echo "ā
Major change detected - will trigger MAJOR version bump"
elif echo "$commit_message" | grep -qE "^feat:"; then
echo "ā
Feature detected - will trigger MINOR version bump"
elif echo "$commit_message" | grep -qE "^(fix|docs|style|refactor|perf|test|build|ci|chore):"; then
echo "ā
Patch change detected - will trigger PATCH version bump"
elif echo "$commit_message" | grep -qE "!:"; then
echo "ā
Breaking change detected - will trigger MAJOR version bump"
else
echo "ā
No version bump - commit type not recognized for versioning"
fi
exit 0