diff --git a/.lefthook/commit-msg/commit-msg-lint.sh b/.lefthook/commit-msg/commit-msg-lint.sh new file mode 100644 index 0000000..92310dd --- /dev/null +++ b/.lefthook/commit-msg/commit-msg-lint.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Commit message validation — called by lefthook commit-msg hook. +# $1 is the path to the temporary commit message file. + +MSG=$(cat "$1") + +# Skip validation for merge commits +if echo "$MSG" | grep -qE '^Merge '; then + exit 0 +fi + +# Enforce type(scope): summary format per CONTRIBUTING.md +if ! echo "$MSG" | grep -qE '^(feat|fix|docs|chore|refactor|test)(\([a-z-]+\))?: .+'; then + echo "ERROR: commit message must match: type(scope): summary" >&2 + echo " Types: feat, fix, docs, chore, refactor, test" >&2 + exit 1 +fi diff --git a/README.md b/README.md index 0421be3..15c15a9 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ A starter template for new GitHub repositories. | `.github/workflows/ci.yml` | Generic GitHub Actions CI workflow (the `npm ci` and `npm test` steps skip when no `package.json` exists) | | `AGENTS.md` | Instructions for AI coding agents | | `CLAUDE.md` | Imports `AGENTS.md` so Claude Code picks up the same instructions | +| `lefthook.yml` | Git hooks manager config (pre-commit + commit-msg) | ## How to use @@ -20,6 +21,24 @@ A starter template for new GitHub repositories. 2. Create a new repository from this template. 3. Customize the files for your project. +## Git Hooks (Lefthook) + +This template includes `lefthook.yml` for git hook management. + +```bash +# Install lefthook (via mise or your package manager) +mise use lefthook + +# Install hooks into .git/hooks/ +lefthook install +``` + +Hooks configured: +- **pre-commit**: runs `npm run lint` if package.json exists (skips merge/rebase) +- **commit-msg**: enforces `type(scope): summary` commit message format via `.lefthook/commit-msg/commit-msg-lint.sh` + +Scripts live under `.lefthook//`. Override with `git commit --no-verify` when needed. + ## License This project is licensed under the MIT License — see the [LICENSE](LICENSE) file for details. diff --git a/lefthook.yml b/lefthook.yml new file mode 100644 index 0000000..cf1d140 --- /dev/null +++ b/lefthook.yml @@ -0,0 +1,22 @@ +# Lefthook — git hooks manager +# Docs: https://lefthook.dev +# +# Install: lefthook install +# Validate: lefthook validate + +pre-commit: + commands: + lint: + run: | + # Run project lint if available + if [ -f package.json ] && command -v npm >/dev/null 2>&1; then + npm run lint + fi + skip: + - merge + - rebase + +commit-msg: + scripts: + "commit-msg-lint.sh": + runner: bash