commit_msg_file="$1"
subject=$(head -1 "$commit_msg_file")
if echo "$subject" | grep -qE '^Merge '; then
exit 0
fi
if echo "$subject" | grep -qE '^Revert "'; then
exit 0
fi
types="feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|enforce"
pattern="^($types)(\([a-z0-9-]+\))?: [a-z]"
if ! echo "$subject" | grep -qE "$pattern"; then
echo ""
echo "✗ COMMIT MESSAGE REJECTED"
echo ""
echo " Your message: \"$subject\""
echo ""
echo " Expected format: <type>(<scope>): <description>"
echo ""
echo " Allowed types:"
echo " feat, fix, docs, style, refactor, perf,"
echo " test, build, ci, chore, revert, enforce"
echo ""
echo " Rules:"
echo " - Description must start with a lowercase letter"
echo " - No period at the end"
echo " - Scope is optional: feat(api): ..."
echo ""
echo " Examples:"
echo " feat(api): parse container labels from daemon response"
echo " fix(ipc): handle named pipe timeout on Windows"
echo " docs: update README with usage examples"
echo ""
exit 1
fi
desc=$(echo "$subject" | sed -E "s/^($types)(\([a-z0-9-]+\))?: //")
desc_len=${#desc}
if [ "$desc_len" -lt 5 ]; then
echo ""
echo "✗ COMMIT MESSAGE TOO SHORT"
echo " Description must be at least 5 characters."
echo " Got: \"$desc\" ($desc_len chars)"
echo ""
exit 1
fi
if [ "$desc_len" -gt 200 ]; then
echo ""
echo "✗ COMMIT MESSAGE TOO LONG"
echo " Subject line description must be ≤200 characters."
echo " Got: \"$desc\" ($desc_len chars)"
echo ""
exit 1
fi
if echo "$subject" | grep -qE '\.$'; then
echo ""
echo "✗ COMMIT MESSAGE ENDS WITH PERIOD"
echo " Do not end the subject line with a period."
echo " Got: \"$subject\""
echo ""
exit 1
fi