Skip to content

Pre-Commit Hooks

PromptKit uses pre-commit hooks to maintain code quality. The hooks run automatically before each commit and only check changed code, making them fast and developer-friendly.

Terminal window
# Install required tools
brew install golangci-lint gosec
pip3 install diff-cover
# Enable the hook (one-time)
chmod +x .git/hooks/pre-commit

Or use the installation script:

Terminal window
./scripts/install-hooks.sh

The pre-commit hook runs four checks:

  1. Linting - Only on changed Go files using golangci-lint --new-from-rev=HEAD
  2. Security - Security scanning with gosec on changed code (if installed)
  3. Tests - Only for packages with changes
  4. Coverage - Requires ≥80% test coverage on changed lines only (via diff-cover)

Speed: Typically 15-35 seconds (vs. 2-4 minutes for full repo checks)

Just commit as usual - the hook runs automatically:

Terminal window
git add .
git commit -m "feat: add new feature"

For emergencies or work-in-progress commits, include [skip-pre-commit] in your message:

Terminal window
git commit -m "wip: experimental changes [skip-pre-commit]"

Note: CI will still run full checks on your PR.

Run the same checks manually before pushing:

Terminal window
make verify # Run all checks (recommended)
make lint-diff # Lint changed files only
make test-coverage-diff # Check coverage on changes

The pre-commit system only checks code you’ve changed:

  • Linting: Uses --new-from-rev=HEAD to only report issues in new/modified lines
  • Coverage: Uses diff-cover to calculate coverage percentage for changed lines only

This means:

  • ✅ You’re never blocked by legacy code issues
  • ✅ All new code must pass quality checks
  • ✅ Coverage improves gradually over time

GitHub Actions runs the same checks in the verify job:

  • Downloads coverage from the test job
  • Runs diff-cover against the PR base branch
  • Posts results as a PR comment
  • Fails the PR if coverage on changed lines is below 80%
Terminal window
chmod +x .git/hooks/pre-commit
Terminal window
# golangci-lint
brew install golangci-lint
# gosec (security scanner)
brew install gosec
# diff-cover
pip3 install diff-cover

The hook shows which lines need test coverage. Options:

  1. Add tests for the changed lines (recommended)
  2. Use [skip-pre-commit] if the code is truly not testable
  3. Run make test-coverage-diff to see a detailed report

Edit the --fail-under value in:

  • Makefile (test-coverage-diff target)
  • .git/hooks/pre-commit
  • .github/workflows/ci.yml (verify job)

Edit .golangci.yml to disable linters that don’t fit your workflow.