Prek: Faster Pre-commit Alternative in Rust
Comprehensive Overview of Prek: A Modern, High-Performance Git Hook Manager Built in Rust
Introduction
Prek (pronounced "preck") is an innovative alternative to the widely used pre-commit framework, designed with performance, efficiency, and developer experience in mind. Developed in Rust, prek eliminates unnecessary dependencies by offering a single binary installation that does not require Python or any other runtime environment. It is optimized for speed, disk space savings, and seamless integration into monorepos (multi-project repositories). Built on the foundation of pre-commit, prek introduces several novel features while maintaining backward compatibility with existing configurations.
This detailed description explores prek’s architecture, key advantages over traditional tools like pre-commit, installation methods, use cases, and real-world adoption by prominent projects. We will also examine its performance benchmarks, CLI improvements, and support for advanced workflows such as workspace mode (monorepos).
Key Features of Prek
1. Performance Optimization
Prek is engineered to outperform pre-commit in multiple dimensions:
- Faster Execution: Prek runs multiple times faster than its predecessor, reducing the time required for running hooks significantly.
- Reduced Disk Usage: By sharing toolchain dependencies across all hooks, prek minimizes disk space consumption. Unlike
pre-commit, which installs separate virtual environments and toolchains per hook, prek consolidates these resources, leading to a more efficient storage footprint. - Parallel Hook Execution: Prek supports running hooks in parallel based on their priority levels, reducing the total execution time for complex repositories.
- Built-in Rust Implementation of Common Hooks: Some frequently used hooks (e.g., linters and formatters) are implemented natively in Rust within prek itself. This eliminates the overhead of invoking external Python scripts, resulting in faster execution.
Performance Benchmark Comparison
| Metric | Prek | Pre-commit | |----------------------|-------------------------------|--------------------------------| | Hook Execution Time | ~50% faster | Slower (due to virtualenv setup)| | Disk Space Usage | Shared toolchains (~30% less) | Per-hook dependencies | | Parallelization | Yes (priority-based) | Limited (sequential by default) |
(Source: Prek Benchmark)
2. Zero-Dependency Installation
One of prek’s most compelling features is its standalone binary installation, which means:
- No Python runtime is required.
- No complex dependency management (e.g.,
pip,virtualenv). - Users can simply download a single executable and start using hooks immediately.
Installation Methods
Prek supports multiple installation channels, allowing users to choose the most convenient method for their environment:
| Platform | Installation Command |
|-------------------|------------------------------------------------------------------------------------|
| Linux/macOS | curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.3.4/prek-installer.sh | sh |
| Windows (PowerShell) | powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases/download/v0.3.4/prek-installer.ps1 | iex" |
| PyPI | pip install prek or uv install prek (recommended) |
| Homebrew | brew install prek |
| GitHub Releases | Download pre-compiled binaries from prek GitHub Releases |
| Cargo (Rust) | cargo install --locked prek |
| npmjs | npm install -g @j178/prek |
(Note: Prek is also available via mise, uv, and other package managers.)
3. Monorepo (Workspace) Support
Prek excels in managing multi-project repositories, where each subdirectory may have its own .pre-commit-config.yaml. This is achieved through:
- Shared Toolchain Management: Prek automatically detects workspace structures and optimizes toolchain installations across all projects.
- Independent Hook Configurations: Each subproject can define its own hooks without conflicts, similar to how
pre-commitworks but with better performance.
Example Workspace Structure
my-monorepo/
├── project-a/
│ ├── .pre-commit-config.yaml
│ └── src/
├── project-b/
│ ├── .pre-commit-config.yaml
│ └── src/
└── package.json (or other config files)
Prek ensures that hooks in project-a and project-b run independently while sharing dependencies where possible.
4. Enhanced CLI Commands
Prek’s command-line interface provides several improvements over pre-commit, making it more intuitive for developers:
| Feature | Prek Improvement |
|-----------------------------|--------------------------------------------------------------------------------|
| prek run --directory | Runs hooks only in a specified directory without using git ls-files. |
| prek run --last-commit| Executes hooks on files modified in the latest commit. |
| Multi-Hook Execution | Run multiple hooks at once (prek run hook1 hook2). |
| Hook Listing (prek list) | Displays all configured hooks with IDs and descriptions for quick reference. |
| Auto-Update (prek auto-update) | Supports cooldown periods to prevent supply chain attacks. |
Example CLI Usage
# Run a specific hook on modified files in the last commit
prek run --last-commit flake8
# Execute multiple hooks in a directory
prek run black isort
# List all configured hooks
prek list
5. Built-in Rust Hooks (Zero-Setup)
Prek includes native Rust implementations of common Git hooks, such as:
- Linters (e.g.,
flake8,ruff) - Formatters (e.g.,
black,isort)
These hooks are compiled directly into prek’s binary, eliminating the need for external Python dependencies and reducing startup time.
Example: Built-in Hook Execution
# Run a built-in Rust hook without additional toolchains
prek run --repo builtin/flake8
6. Integration with uv (Python Toolchain)
Prek leverages uv, a fast Python virtual environment manager, to handle:
- Dependency installation for Python projects.
- Virtual environment creation without manual setup.
This ensures that prek users do not have to manage Python environments separately, improving consistency across different repositories.
Who Is Using Prek?
Despite being relatively new (as of October 2023), prek has gained traction among several high-profile projects and organizations:
| Project | Repository Link | |-----------------------------|-------------------------------------------------------------------------------| | Apache Airflow | GitHub Issue #44995 | | CPython (Python Core) | GitHub Issue #143148 | | FastAPI | PR #14572 | | Ruff (Python Linter) | PR #22505 | | Home Assistant | PR #160427 | | OpenLineage | PR #3965 |
These projects demonstrate prek’s ability to replace pre-commit while maintaining compatibility with existing workflows.
Comparison: Prek vs. Pre-commit
| Feature | Prek | Pre-commit |
|------------------------|------------------------------------------------------------------------------|--------------------------------------------|
| Language | Rust (single binary) | Python-based |
| Installation | No Python required; standalone binary | Requires Python and virtualenv setup |
| Performance | ~50% faster, lower disk usage | Slower due to per-hook dependencies |
| Monorepo Support | Built-in workspace mode | Limited support |
| Hook Parallelization| Priority-based parallel execution | Sequential by default |
| Built-in Rust Hooks | Yes (e.g., flake8, ruff) | No |
Use Cases for Prek
1. Python Development
Prek is particularly well-suited for Python projects, where it can:
- Run linters (
flake8,pylint). - Format code with tools like
blackandisort. - Enforce coding standards automatically.
Example Configuration (prek.config.yaml)
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
hooks:
- id: ruff
2. JavaScript/TypeScript Projects
Prek integrates seamlessly with Node.js projects, supporting:
- ESLint and Prettier formatting.
- Type checking (e.g.,
typescript-eslint).
Example (prek.config.yaml)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
3. Multi-Language Monorepos
For repositories with multiple languages (e.g., Python + Go), prek’s workspace mode ensures that each language has its own toolchain without conflicts.
Conclusion
Prek represents a significant evolution in the Git hook management space, offering:
✅ Blazing-fast execution compared to pre-commit.
✅ Zero-dependency installation, eliminating runtime requirements.
✅ Built-in Rust hooks for faster performance.
✅ Monorepo support with shared toolchain optimization.
✅ Improved CLI usability (e.g., --directory, --last-commit).
While still in its early stages, prek’s adoption by major projects like Apache Airflow and FastAPI signals its potential as a future standard for Git hooks. Developers looking for a lightweight, high-performance alternative to pre-commit should give prek a try.
For further exploration, visit the official documentation at prek.j178.dev and explore its benchmarks, CLI reference, and language support matrix.
Note: For the most up-to-date installation instructions and features, refer to the GitHub Releases page or the official website.
(End of Description)
Enjoying this project?
Discover more amazing open-source projects on TechLogHub. We curate the best developer tools and projects.
Repository:https://github.com/j178/prek
GitHub - j178/prek: Prek: Faster Pre-commit Alternative in Rust
Prek is a high-performance Git hook manager built in Rust, offering faster execution, zero-dependency installation, and native Rust hooks for Python projects....
github - j178/prek