|
| 1 | +#!/usr/bin/env sh |
| 2 | +. "$(dirname -- "$0")/_/husky.sh" |
| 3 | + |
| 4 | +# Conventional Commits validation |
| 5 | +# Validates commit message format according to conventional commits specification |
| 6 | +# Format: <type>(<optional scope>): <description> |
| 7 | +# Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert |
| 8 | + |
| 9 | +commit_msg_file=$1 |
| 10 | +commit_msg=$(cat "$commit_msg_file") |
| 11 | + |
| 12 | +# Conventional commit regex pattern (default settings) |
| 13 | +# Matches: type(optional-scope): description |
| 14 | +# Allows: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert |
| 15 | +# Allows breaking changes: type!: or type(scope)!: |
| 16 | +conventional_commit_regex='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?(!)?: .{1,}' |
| 17 | + |
| 18 | +# Check if commit message matches conventional commit format |
| 19 | +if ! echo "$commit_msg" | grep -Eq "$conventional_commit_regex"; then |
| 20 | + echo "" |
| 21 | + echo "❌ Invalid commit message format" |
| 22 | + echo "" |
| 23 | + echo "Commit message must follow Conventional Commits specification:" |
| 24 | + echo "" |
| 25 | + echo " <type>[optional scope][!]: <description>" |
| 26 | + echo "" |
| 27 | + echo " [optional body]" |
| 28 | + echo "" |
| 29 | + echo " [optional footer(s)]" |
| 30 | + echo "" |
| 31 | + echo "Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert" |
| 32 | + echo "" |
| 33 | + echo "Examples:" |
| 34 | + echo " feat: add user authentication" |
| 35 | + echo " fix(api): resolve timeout issue" |
| 36 | + echo " feat!: redesign API (breaking change)" |
| 37 | + echo " docs: update README with setup instructions" |
| 38 | + echo "" |
| 39 | + echo "Your commit message:" |
| 40 | + echo " $commit_msg" |
| 41 | + echo "" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +# Check for WIP commits (optional - can be disabled) |
| 46 | +if echo "$commit_msg" | grep -Eqi '^(wip|todo|fixme)'; then |
| 47 | + echo "" |
| 48 | + echo "⚠️ Warning: WIP commit detected" |
| 49 | + echo "" |
| 50 | +fi |
0 commit comments