#!/bin/sh
# Commit message linting hook
# Enforces Conventional Commits format

# Automation bypass: blacksmith authors its own intermediate commit messages
# ("blacksmith: WU-..") and commits WITHOUT --no-verify, then squashes/cherry-picks them
# into a single PR. Skip Conventional-Commits enforcement for those (and when a tool sets
# SKIP_COMMIT_LINT). Human commits never use this prefix, so they are unaffected.
if [ -n "$SKIP_COMMIT_LINT" ] || head -n1 "$1" | grep -qE '^blacksmith[:/]'; then
  exit 0
fi

# Run commitlint on the commit message
npx --no -- commitlint --edit "$1"

if [ $? -ne 0 ]; then
    echo ""
    echo "❌ Commit message does not follow Conventional Commits format."
    echo ""
    echo "Expected format: <type>(<scope>): <subject>"
    echo ""
    echo "Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert"
    echo ""
    echo "Examples:"
    echo "  feat(export): add support for Obsidian/Longform export"
    echo "  fix(editor): resolve cursor position issue on paste"
    echo "  docs(readme): update installation instructions"
    echo ""
    echo "See CONTRIBUTING.md for more details."
    exit 1
fi
