j178/prek

GitHub: j178/prek

用 Rust 重写的 pre-commit 替代品,更快速、零依赖,原生支持 monorepo。

Stars: 6610 | Forks: 178

prek
prek

[![prek](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json)](https://github.com/j178/prek) [![codecov](https://codecov.io/github/j178/prek/graph/badge.svg?token=MP6TY24F43)](https://codecov.io/github/j178/prek) [![GitHub Downloads](https://img.shields.io/github/downloads/j178/prek/total?logo=github)](https://github.com/j178/prek/releases) [![PyPI Downloads](https://img.shields.io/pypi/dm/prek?logo=python)](https://pepy.tech/projects/prek) [![Discord](https://img.shields.io/discord/1403581202102878289?logo=discord)](https://discord.gg/3NRJUqJz86)
[pre-commit](https://pre-commit.com/) 是一个用于运行多种语言编写的 hooks 的框架,它负责管理运行这些 hooks 所需的语言工具链和依赖项。 *prek* 是 pre-commit 的一个重塑版本,使用 Rust 构建。 它的设计目标是成为一个更快的、无依赖的、可直接替代原版的方案, 同时还提供了一些长期被请求的额外功能。 ## 特性 - 单一二进制文件,无任何依赖,不需要 Python 或任何其他运行时。 - 比 `pre-commit` [更快](https://prek.j178.dev/benchmark/),且磁盘空间使用效率更高。 - 完全兼容原有的 pre-commit 配置和 hooks。 - 内置支持 monorepos(即 [工作区模式](https://prek.j178.dev/workspace/))。 - 集成 [`uv`](https://github.com/astral-sh/uv) 用于管理 Python 虚拟环境和依赖项。 - 改进了 Python、Node.js、Bun、Go、Rust 和 Ruby 的工具链安装,并在 hooks 之间共享。 - 部分常用 hooks 的[内置](https://prek.j178.dev/builtin/) Rust 原生实现。 ## 目录 - [安装](#installation) - [快速开始](#quick-start) - [为什么选择 prek?](#why-prek) - [谁在使用 prek?](#who-is-using-prek) - [致谢](#acknowledgements) ## 安装
独立安装程序 prek 提供了一个独立的安装脚本用于下载和安装该工具, 在 Linux 和 macOS 上: ``` curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.3.4/prek-installer.sh | sh ``` 在 Windows 上: ``` powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases/download/v0.3.4/prek-installer.ps1 | iex" ```
PyPI prek 作为 Python 二进制 wheel 发布到 PyPI,你可以使用 `pip`、`uv`(推荐)或 `pipx` 安装它: ``` # 使用 uv(推荐) uv tool install prek # 使用 uvx(安装并运行在一个命令中) uvx prek # 将 prek 添加到项目 dev-dependencies uv add --dev prek # 使用 pip pip install prek # 使用 pipx pipx install prek ```
Homebrew ``` brew install prek ```
mise 要通过 [mise](https://mise.jdx.dev)([v2025.8.11](https://github.com/jdx/mise/releases/tag/v2025.8.11) 或更高版本)使用 prek: ``` mise use prek ```
Cargo binstall 使用 [cargo-binstall](https://github.com/cargo-bins/cargo-binstall) 从 GitHub 安装预编译的二进制文件: ``` cargo binstall prek ```
Cargo 使用 Cargo 从源码构建(需要 Rust 1.89+): ``` cargo install --locked prek ```
npmjs prek 作为 [Node.js 包](https://www.npmjs.com/package/@j178/prek)发布, 可以使用任何兼容 npm 的包管理器安装: ``` # 作为 dev dependency npm add -D @j178/prek pnpm add -D @j178/prek bun add -D @j178/prek # 或全局安装 npm install -g @j178/prek pnpm add -g @j178/prek bun install -g @j178/prek # 或直接运行而无需安装 npx @j178/prek --version bunx @j178/prek --version ```
Nix prek 可通过 [Nixpkgs](https://search.nixos.org/packages?channel=unstable&show=prek&query=prek) 获取。 ``` # 根据您的用例选择合适的方式。 # 在 Shell 中一次性运行: nix-shell -p prek # 不支持 flakes 的 NixOS 或非 NixOS: nix-env -iA nixos.prek # 支持 flakes 的非 NixOS: nix profile install nixpkgs#prek ```
Conda prek 可通过 [conda-forge](https://anaconda.org/conda-forge/prek) 以 `prek` 的名称获取。 ``` conda install conda-forge::prek ```
Scoop (Windows) prek 可通过 [Scoop](https://scoop.sh/#/apps?q=prek) 获取。 ``` scoop install main/prek ```
Winget (Windows) prek 可通过 [winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/) 获取。 ``` winget install --id j178.Prek ```
MacPorts prek 可通过 [MacPorts](https://ports.macports.org/port/prek/) 获取。 ``` sudo port install prek ```
GitHub Releases 预编译的二进制文件可从 [GitHub releases](https://github.com/j178/prek/releases) 页面下载。
GitHub Actions prek 可以通过 [j178/prek-action](https://github.com/j178/prek-action) 仓库在 GitHub Actions 中使用。 示例工作流: ``` name: Prek checks on: [push, pull_request] jobs: prek: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - uses: j178/prek-action@v1 ``` 此操作会安装 prek 并在你的仓库上运行 `prek run --all-files`。 prek 也可以通过 [`taiki-e/install-action`](https://github.com/taiki-e/install-action) 获取,用于安装各种工具。
如果是通过独立安装程序安装的,prek 可以自我更新到最新版本: ``` prek self update ``` ## 快速开始 - **我已经在使用 pre-commit:** 请按照[快速入门指南](https://prek.j178.dev/quickstart/#already-using-pre-commit)中的简短迁移清单安全地切换到 `prek`。 - **我是 pre-commit 类工具的新手:** 请在[初学者快速入门教程](https://prek.j178.dev/quickstart/#new-to-pre-commit-style-workflows)中学习基础知识——创建配置、运行 hooks 以及安装 git hooks。 ## 为什么选择 prek? ### prek 更快 - 它比 `pre-commit` [快好几倍](https://prek.j178.dev/benchmark/),且占用的一半磁盘空间。 - 它重新设计了 hook 环境和工具链的管理方式,它们在 hooks 之间共享,这减少了磁盘空间使用并加快了安装过程。 - 仓库是并行克隆的,如果 hooks 的依赖项不相交,它们也会并行安装。 - Hooks 可以按优先级并行运行(具有相同 [`priority`](https://prek.j178.dev/configuration/#priority) 的 hooks 可能会并发执行),从而减少端到端运行时间。 - 它使用 [`uv`](https://github.com/astral-sh/uv) 来创建 Python virtualenvs 和安装依赖项,`uv` 以其速度和效率著称。 - 它用 Rust 实现了一些常见的 hooks,[内置在 prek 中](https://prek.j178.dev/builtin/),比对应的 Python 版本更快。 - 它支持 `repo: builtin` 用于离线、零设置的 hooks,这在 `pre-commit` 中是不可用的。 ### prek 提供更好的用户体验 - 无需安装 Python 或任何其他运行时,只需下载一个单一的二进制文件。 - 无需为你的 Python 版本或虚拟环境烦恼,prek 会自动为你安装所需的 Python 版本并创建虚拟环境。 - 内置支持 [workspaces](https://prek.j178.dev/workspace/)(或 monorepos),每个子项目可以有自己的 `.pre-commit-config.yaml` 文件。 - [`prek run`](https://prek.j178.dev/cli/#prek-run) 相比 `pre-commit run` 有一些精妙的改进,例如: - `prek run --directory ` 为指定目录中的文件运行 hooks,不再需要使用 `git ls-files -- | xargs pre-commit run --files` 了。 - `prek run --last-commit` 为上次提交中更改的文件运行 hooks。 - `prek run [HOOK] [HOOK]` 选择并运行多个 hooks。 - [`prek list`](https://prek.j178.dev/cli/#prek-list) 命令列出所有可用的 hooks、它们的 id 和描述,提供已配置 hooks 的更好概览。 - [`prek auto-update`](https://prek.j178.dev/cli/#prek-auto-update) 支持 `--cooldown-days` 以缓解开源供应链攻击。 - prek 为 `prek run ` 命令提供 shell 补全,使得无需记住 id 即可更轻松地运行特定 hooks。 想了解 prek 提供的更详细的改进,请查看 [与 pre-commit 的差异](https://prek.j178.dev/diff/)。 ## 谁在使用 prek? prek 相当新,但已被一些项目和组织使用或推荐: - [apache/airflow](https://github.com/apache/airflow/issues/44995) - [python/cpython](https://github.com/python/cpython/issues/143148) - [pdm-project/pdm](https://github.com/pdm-project/pdm/pull/3593) - [fastapi/fastapi](https://github.com/fastapi/fastapi/pull/14572) - [fastapi/typer](https://github.com/fastapi/typer/pull/1453) - [fastapi/asyncer](https://github.com/fastapi/asyncer/pull/437) - [astral-sh/ruff](https://github.com/astral-sh/ruff/pull/22505) - [astral-sh/ty](https://github.com/astral-sh/ty/pull/2469) - [openclaw/openclaw](https://github.com/openclaw/openclaw/pull/1720) - [home-assistant/core](https://github.com/home-assistant/core/pull/160427) - [DetachHead/basedpyright](https://github.com/DetachHead/basedpyright/pull/1413) - [OpenLineage/OpenLineage](https://github.com/OpenLineage/OpenLineage/pull/3965) - [authlib/authlib](https://github.com/authlib/authlib/pull/804) - [django/djangoproject.com](https://github.com/django/djangoproject.com/pull/2252) - [Future-House/paper-qa](https://github.com/Future-House/paper-qa/pull/1098) - [requests-cache/requests-cache](https://github.com/requests-cache/requests-cache/pull/1116) - [Goldziher/kreuzberg](https://github.com/Goldziher/kreuzberg/pull/142) - [python-attrs/attrs](https://github.com/python-attrs/attrs/commit/c95b177682e76a63478d29d040f9cb36a8d31915) - [jlowin/fastmcp](https://github.com/jlowin/fastmcp/pull/2309) - [apache/iceberg-python](https://github.com/apache/iceberg-python/pull/2533) - [apache/iggy](https://github.com/apache/iggy/pull/2383) - [apache/lucene](https://github.com/apache/lucene/pull/15629) - [jcrist/msgspec](https://github.com/jcrist/msgspec/pull/918) - [python-humanize/humanize](https://github.com/python-humanize/humanize/pull/276) - [MoonshotAI/kimi-cli](https://github.com/MoonshotAI/kimi-cli/pull/535) - [simple-icons/simple-icons](https://github.com/simple-icons/simple-icons/pull/14245) - [ast-grep/ast-grep](https://github.com/ast-grep/ast-grep.github.io/commit/e30818144b2967a7f9172c8cf2f4596bba219bf5) - [commitizen-tools/commitizen](https://github.com/commitizen-tools/commitizen) - [cocoindex-io/cocoindex](https://github.com/cocoindex-io/cocoindex/pull/1564) - [cachix/devenv](https://github.com/cachix/devenv/pull/2304) - [copper-project/copper-rs](https://github.com/copper-project/copper-rs/pull/783) - [bramstroker/homeassistant-powercalc](https://github.com/bramstroker/homeassistant-powercalc/pull/3978) ## 致谢 本项目深受原版 [pre-commit](https://pre-commit.com/) 工具的启发,如果没有该项目维护者和贡献者的辛勤工作,它是不可能诞生的。 特别感谢 [Astral](https://github.com/astral-sh) 团队出色的项目,特别是 [uv](https://github.com/astral-sh/uv), 我从中学到了很多关于如何编写高效且地道的 Rust 代码的知识。
标签:Git钩子, GNU通用公共许可证, Golang, Lint工具, LNA, MITM代理, Node.js, Pre-commit, Python, Rust, SOC Prime, UV集成, 二进制文件, 云安全监控, 代码审查, 代码检查器, 代码规范, 依赖管理, 可视化界面, 安全编程, 开发工具, 开发者效率, 开源框架, 性能优化, 持续集成, 无后门, 日志审计, 替代方案, 检测绕过, 网络流量审计, 网络调试, 自动化, 逆向工具, 通知系统, 通知系统, 静态分析