Prek:一个更好、更快的、可直接替换的pre-commit工具,用Rust编写。
Prek: A better, faster, drop-in pre-commit replacement, engineered in Rust

原始链接: https://github.com/j178/prek

## Prek:`pre-commit` 的更快替代方案 Prek 是一个重新设计的、基于 Rust 的框架,旨在作为流行的 `pre-commit` 工具的直接替代品。它的目标是更快、更高效、无依赖——无需 Python 或其他运行时——同时添加长期以来要求的特性。Prek 已经被 CPython 和 FastAPI 等项目采用,它提供了显著的性能提升,使用的磁盘空间最多减少一半,运行速度比其前身快数倍。 主要特性包括内置的 monorepo 支持、与 `uv` 的集成以优化 Python 环境管理,以及改进的各种语言(Python、Node.js、Go 等)的工具链安装。Prek 还以 Rust 原生实现了一些常见的钩子以提高速度,并支持使用 `repo: builtin` 的离线钩子。 安装通过 shell 脚本、包管理器(pip、npm、Homebrew、Nix、Conda、Scoop)或预构建的二进制文件非常简单。Prek 提供了增强的命令行功能,例如在特定目录或上一次提交上运行钩子,以及改进的钩子列表和自动更新功能。 虽然仍处于发展阶段,但 Prek 正在迅速成熟,并为寻求简化和高性能 pre-commit 工作流程的开发人员提供了一个引人注目的替代方案。

## Prek:更快的预提交替代方案 Prek 是一款新的工具,使用 Rust 编写,旨在改进流行的 `pre-commit` 框架,用于在提交代码前运行检查。虽然 `pre-commit` 广泛使用,但它可能速度较慢,并且在处理复杂项目和 monorepo 时存在局限性。 Prek 得益于其 Rust 实现以及并行处理和更快的 Python hook 包管理等功能,提供了性能改进。它被设计为可以直接替换 `pre-commit` 的工具,并保持与现有 `pre-commit` 配置的兼容性。 讨论的重点在于预提交 hook 是否是*正确*的方法,一些人提倡使用后台检查和 CI 集成。另一些人则强调通过 hook 提供即时反馈和强制执行标准的重要性。Prek 解决了 `pre-commit` 的一些痛点,例如管理工作区中的 hook,并提供了预推送 hook 等功能。然而,一些人争论这种复杂性是否合理,以及核心问题是否在于预提交 hook 的概念本身。
相关文章

原文

pre-commit is a framework to run hooks written in many languages, and it manages the language toolchain and dependencies for running the hooks.

prek is a reimagined version of pre-commit, built in Rust. It is designed to be a faster, dependency-free and drop-in alternative for it, while also providing some additional long-requested features.

Note

Although prek is pretty new, it’s already powering real‑world projects like CPython, Apache Airflow, FastAPI, and more projects are picking it up—see Who is using prek?. If you’re looking for an alternative to pre-commit, please give it a try—we’d love your feedback!

Please note that some languages are not yet supported for full drop‑in parity with pre-commit. See Language Support for current status.

  • 🚀 A single binary with no dependencies, does not require Python or any other runtime.
  • Faster than pre-commit and more efficient in disk space usage.
  • 🔄 Fully compatible with the original pre-commit configurations and hooks.
  • 🏗️ Built-in support for monorepos (i.e. workspace mode).
  • 🐍 Integration with uv for managing Python virtual environments and dependencies.
  • 🛠️ Improved toolchain installations for Python, Node.js, Bun, Go, Rust and Ruby, shared between hooks.
  • 📦 Built-in Rust-native implementation of some common hooks.
Standalone installer

prek provides a standalone installer script to download and install the tool,

On Linux and macOS:

curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.3.1/prek-installer.sh | sh

On Windows:

powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases/download/v0.3.1/prek-installer.ps1 | iex"
PyPI

prek is published as Python binary wheel to PyPI, you can install it using pip, uv (recommended), or pipx:

# Using uv (recommended)
uv tool install prek

# Using uvx (install and run in one command)
uvx prek

# Adding prek to the project dev-dependencies
uv add --dev prek

# Using pip
pip install prek

# Using pipx
pipx install prek
Homebrew
mise

To use prek with mise (v2025.8.11 or later):

Cargo binstall

Install pre-compiled binaries from GitHub using cargo-binstall:

Cargo

Build from source using Cargo (Rust 1.89+ is required):

cargo install --locked prek
npmjs

prek is published as a Node.js package and can be installed with any npm-compatible package manager:

# As a dev dependency
npm add -D @j178/prek
pnpm add -D @j178/prek
bun add -D @j178/prek

# Or install globally
npm install -g @j178/prek
pnpm add -g @j178/prek
bun install -g @j178/prek

# Or run directly without installing
npx @j178/prek --version
bunx @j178/prek --version
Nix

prek is available via Nixpkgs.

# Choose what's appropriate for your use case.
# One-off in a shell:
nix-shell -p prek

# NixOS or non-NixOS without flakes:
nix-env -iA nixos.prek

# Non-NixOS with flakes:
nix profile install nixpkgs#prek
Conda

prek is available as prek via conda-forge.

conda install conda-forge::prek
Scoop (Windows)

prek is available via Scoop.

MacPorts

prek is available via MacPorts.

GitHub Releases

Pre-built binaries are available for download from the GitHub releases page.

GitHub Actions

prek can be used in GitHub Actions via the j178/prek-action repository.

Example workflow:

name: Prek checks
on: [push, pull_request]

jobs:
  prek:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: j178/prek-action@v1

This action installs prek and runs prek run --all-files on your repository.

prek is also available via taiki-e/install-action for installing various tools.

If installed via the standalone installer, prek can update itself to the latest version:

  • I already use pre-commit: follow the short migration checklist in the quickstart guide to swap in prek safely.
  • I'm new to pre-commit-style tools: learn the basics—creating a config, running hooks, and installing git hooks—in the beginner quickstart walkthrough.
  • It is multiple times faster than pre-commit and takes up half the disk space.
  • It redesigned how hook environments and toolchains are managed, they are all shared between hooks, which reduces the disk space usage and speeds up the installation process.
  • Repositories are cloned in parallel, and hooks are installed in parallel if their dependencies are disjoint.
  • Hooks can run in parallel by priority (hooks with the same priority may run concurrently), reducing end-to-end runtime.
  • It uses uv for creating Python virtualenvs and installing dependencies, which is known for its speed and efficiency.
  • It implements some common hooks in Rust, built in prek, which are faster than their Python counterparts.
  • It supports repo: builtin for offline, zero-setup hooks, which is not available in pre-commit.

prek provides a better user experience

  • No need to install Python or any other runtime, just download a single binary.
  • No hassle with your Python version or virtual environments, prek automatically installs the required Python version and creates a virtual environment for you.
  • Built-in support for workspaces (or monorepos), each subproject can have its own .pre-commit-config.yaml file.
  • prek run has some nifty improvements over pre-commit run, such as:
    • prek run --directory <dir> runs hooks for files in the specified directory, no need to use git ls-files -- <dir> | xargs pre-commit run --files anymore.
    • prek run --last-commit runs hooks for files changed in the last commit.
    • prek run [HOOK] [HOOK] selects and runs multiple hooks.
  • prek list command lists all available hooks, their ids, and descriptions, providing a better overview of the configured hooks.
  • prek auto-update supports --cooldown-days to mitigate open source supply chain attacks.
  • prek provides shell completions for prek run <hook_id> command, making it easier to run specific hooks without remembering their ids.

For more detailed improvements prek offers, take a look at Difference from pre-commit.

prek is pretty new, but it is already being used or recommend by some projects and organizations:

This project is heavily inspired by the original pre-commit tool, and it wouldn't be possible without the hard work of the maintainers and contributors of that project.

And a special thanks to the Astral team for their remarkable projects, particularly uv, from which I've learned a lot on how to write efficient and idiomatic Rust code.

联系我们 contact @ memedata.com