prek

repository·master·Indexed 24 days ago

https://github.com/j178/prek

A high-performance, Rust-based Git hook manager designed as a dependency-free, drop-in alternative to the pre-commit framework. It features full compatibility with pre-commit configurations, built-in support for monorepos via workspace mode, and integration with uv for Python environment management. It provides improved toolchain installations for Python, Node.js, Bun, Go, Rust, and Ruby, and includes a utility to migrate pre-commit YAML configurations to the native prek.toml format.

Tokens
43.1K
Snippets
102
Records
272
Agent score
86%

What's inside prek

  1. Overview of prek

    master

    prek is a high-performance, Rust-based reimagining of the pre-commit framework. It is designed as a faster, dependency-free, and drop-in alternative that manages language toolchains and dependencies for running hooks.

    Key features include:

    • A single binary with no external runtime dependencies (no Python required).
    • Full compatibility with original pre-commit configurations and hooks.
    • Built-in support for monorepos via workspace mode.
    • Integration with uv for Python environment management.
    • Improved toolchain installations for Python, Node.js, Bun, Go, Rust, and Ruby.
    • Built-in Rust-native implementations of common hooks.
  2. Overview of prek CLI commands

    master

    The prek CLI is a fast Git hook manager written in Rust. The general usage pattern is:

    prek [OPTIONS] [HOOK|PROJECT]... [COMMAND]

    Available commands include:

    • prek install: Install Git hook shims.
    • prek prepare-hooks: Prepare environments for configured hooks.
    • prek run: Run configured hooks.
    • prek list: List configured hooks.
    • prek uninstall: Uninstall Git hook shims.
    • prek validate-config: Validate prek configuration files.
    • prek validate-manifest: Validate pre-commit hook manifests (.pre-commit-hooks.yaml).
    • prek sample-config: Generate a sample prek configuration file.
    • prek update: Update configured repositories.
    • prek cache: Manage the prek cache.
    • prek try-repo: Try hooks from a repository.
    • prek util: Run utility commands.
    • prek self: Manage the prek installation.
    prek [OPTIONS] [HOOK|PROJECT]... [COMMAND]
  3. Getting started with prek

    master

    If you are new to prek, follow these steps to begin:

    1. Installation: Follow the Installation guide to set up the tool on your system.
    2. Quickstart: Follow the Quickstart guide to run your first hooks.

    For existing users, refer to:

  4. Test hooks locally with prek try-repo

    master

    Use prek try-repo to run hooks from a local repository without needing to publish a release. This is useful for iterating on hook development.

    Syntax: prek try-repo <path-to-hook-repo> <hook-id> [options]

    Note: For prepare-commit-msg or commit-msg hooks, you must pass the --commit-msg-filename argument during testing.

    # In another repository where you want to test the hook
    prek try-repo ../path/to/hook-repo my-hook-id --verbose
  5. Skip projects or hooks in Workspace Mode

    master

    You can exclude specific projects or hooks from execution using the --skip flag or environment variables.

    Using the --skip flag

    Use the same selector syntax as for selection:

    • --skip <project-path>/: Skips all hooks in a project and its subprojects.
    • --skip <project-path>/ --skip <subproject-path>/: Skips a project and then specifically skips a subproject within a selected project.
    • --skip <hook-id>: Skips all hooks with that ID across all projects.

    Using Environment Variables

    You can use PREK_SKIP or SKIP. Multiple values must be comma-delimited.

    Precedence: --skip (CLI) > PREK_SKIP > SKIP.

    # Skip all hooks except those from the 'frontend' project
    prek run --skip frontend/
    
    # Run hooks from 'frontend' but skip 'frontend/docs'
    prek run frontend/ --skip frontend/docs
    
    # Skip 'frontend' and 'tests' projects via environment variable
    PREK_SKIP=frontend/,tests prek run
    
    # Skip 'frontend/docs' project and 'src/backend:lint' hook
    SKIP=frontend/docs,src/backend:lint prek run
  6. Configure Bun hooks

    master

    Bun hooks are installed via bun install and run the configured entry. The repository must contain a package.json. The entry should be a Bun command or a provided bin name. additional_dependencies are supported. Bun hooks can run without a pre-installed Bun runtime if toolchain download is available.

    Supported language_version formats:

    • default or system
    • bun, bun@latest
    • bun@1, 1
    • bun@1.1, 1.1
    • bun@1.1.0, 1.1.0
    • Semver ranges (e.g., >=1.0, <2.0)

    Note: Bun support is a prek-only extension.

  7. Configure Docker hooks

    master

    Builds an image from the repository root using docker build . and runs the hook inside the container. The first token of entry is used as the container --entrypoint.

    Runtime Details:

    • The repository is bind-mounted to /src with working directory /src.
    • Supports Docker, Podman, or Container (auto-detected).
    • Environment variables from env are passed via -e.
    • Uses --init for signal forwarding.

    Environment Variables:

    • PREK_CONTAINER_RUNTIME: Override auto-detection.
    • PREK_DOCKER_NO_INIT=1: Skip the --init flag (use if the environment cannot run the init helper).
  8. Configure Conda hooks

    master

    For remote hooks, prek creates a Conda environment from the repository's environment.yml. For repo: local hooks, it creates a minimal environment using only additional_dependencies.

    Conda Executables:

    • Uses system conda by default.
    • Set PRE_COMMIT_USE_MAMBA=1 to use mamba.
    • Set PRE_COMMIT_USE_MICROMAMBA=1 to use micromamba.

    Note on language_version: Explicit language_version requests are rejected for Conda. Python or package versions must be declared in the environment.yml file.

    repos:
      - repo: local
        hooks:
          - id: conda-hook
            name: Conda hook
            language: conda
            entry: python -c "import colorama; print('ok')"
            additional_dependencies: [colorama]
  9. Run configured hooks manually

    master

    You can execute hooks without creating a Git commit using the prek run command. Use these commands to verify changes or check the entire repository.

    • Run hooks against currently staged files.
    • Run hooks against all files in the repository.
    • Run a specific hook using its ID.
    • Perform a dry run to see what would run without executing them or modifying files.
  10. Understand Workspace Execution Order and File Visibility

    master

    Execution Order

    Projects are executed from deepest to shallowest. For example, in a structure with src/backend/ and src/, src/backend/ runs first, followed by src/, and finally the workspace root.

    File Visibility

    Each project is isolated: a hook in a project can only see and process files within its own directory tree. It cannot reference files in sibling directories. If a hook needs access to files across multiple projects, move its configuration to a common ancestor directory (like the workspace root).

    Concurrency

    Projects at the same depth can run concurrently. Concurrency is bounded by the PREK_CONCURRENT_HOOKS environment variable. Note that hooks should be designed to avoid contending for shared resources (like sibling project files or shared caches) outside their own directory.

  11. Create a prek configuration

    master

    To start a new workflow, create a prek.toml file in the root of your repository. This file defines the repositories and hooks to be used. While prek.toml is the native format, prek can also read existing .pre-commit-config.yaml files.

    [[repos]]
    repo = "https://github.com/pre-commit/pre-commit-hooks"
    rev = "v6.0.0"
    hooks = [
      { id = "check-yaml" },
      { id = "end-of-file-fixer" },
    ]
  12. Configure Deno hooks

    master

    Installs additional_dependencies via deno install --global into an isolated DENO_DIR. Deno hooks can run without a pre-installed runtime if toolchain download is available.

    Dependency Rules:

    • Use npm: or jsr: specifiers.
    • Local files: ./path/to/tool.ts:name.
    • To override executable name: npm:semver@7:semver-tool.

    Supported language_version formats:

    • default or system
    • deno, deno@latest, deno@x, deno@x.y, deno@x.y.z
    • Semver ranges (e.g., >=x.y, <x+1.0)

    Examples:

    • NPM package: entry: deno run -A npm:eslint
    • JSR package: entry: deno run -A jsr:@biomejs/biome
    • Built-in command: entry: deno fmt
    repos:
      - repo: local
        hooks:
          - id: eslint
            name: ESLint
            language: deno
            entry: deno run -A npm:eslint
            types: [ts, tsx, js, jsx]