This guide explains how to use the pre-commit hooks provided by flowspec to maintain code quality and catch issues before they enter your repository.
Pre-commit hooks run automatically before each commit, catching issues like:
- Trailing whitespace and formatting issues
- Syntax errors in YAML, JSON, TOML files
- Merge conflict markers left in code
- Security vulnerabilities (secrets, unsafe code)
- Linting and style violations
# Using pip
pip install pre-commit
# Using uv
uv pip install pre-commit
# Using homebrew (macOS)
brew install pre-commitIf you ran flowspec init, you already have a .pre-commit-config.yaml file. Install the hooks with:
pre-commit installRun hooks against all files:
pre-commit run --all-filesRun a specific hook:
pre-commit run trailing-whitespace --all-filesFlowspec provides project-type-specific pre-commit configurations:
The base template includes hooks that work for any project:
- Trailing whitespace removal
- End-of-file fixer
- YAML/JSON/TOML syntax checking
- Merge conflict detection
- Large file detection
- Private key detection
Python projects get additional hooks:
- Ruff: Fast Python linter and formatter
- Bandit: Security vulnerability scanner
- Mypy (optional): Static type checking
Node.js/TypeScript projects get:
- Prettier: Code formatter
- ESLint: JavaScript/TypeScript linter
- npm audit (optional): Security vulnerability scanner
The template includes commented-out advanced hooks. Uncomment them in .pre-commit-config.yaml:
# Before (disabled):
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.13.0
# hooks:
# - id: mypy
# After (enabled):
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
hooks:
- id: mypyExclude specific files or directories from a hook:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.3
hooks:
- id: ruff
exclude: ^(migrations/|vendor/)Bypass all hooks for an emergency commit:
git commit --no-verify -m "emergency fix"Warning: Use
--no-verifysparingly. Consider logging bypasses for audit purposes.
Enable fast security scanning before commits by uncommenting the flowspec hook:
- repo: local
hooks:
- id: flowspec-security-fast
name: Flowspec Security Scan (fast)
entry: flowspec security scan --fast --changed-only --fail-on critical
language: system
pass_filenames: false
stages: [pre-commit]This runs a fast security scan (<10 seconds) on changed files only, failing only on critical issues.
Keep hooks up to date:
pre-commit autoupdateThis updates all hooks to their latest versions.
# Clear cache and reinstall
pre-commit clean
pre-commit installSome hooks (like mypy) can be slow. Consider:
- Running them only in CI, not pre-commit
- Using
--changed-onlyflags where available - Excluding large directories
Ensure your project has the required ESLint plugins installed:
npm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-pluginPre-commit hooks can also run in CI:
# .github/workflows/pre-commit.yml
name: pre-commit
on: [push, pull_request]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- uses: pre-commit/action@v3.0.1