文章摘要
prek是用Rust重新开发的pre-commit工具改进版,旨在提供更好的性能和使用体验。该项目托管在GitHub上,由开发者j178维护。
文章总结
GitHub 项目:prek —— 基于 Rust 重构的增强版 pre-commit
项目简介
prek 是 pre-commit 的现代化重构版本,采用 Rust 编写,旨在提供更高效、无依赖的代码提交前钩子管理工具。它完全兼容原生 pre-commit 配置,同时通过以下特性提升开发体验:
- 极速性能:比原版快数倍,磁盘占用减少一半。
- 零依赖:单一二进制文件,无需 Python 或其他运行时环境。
- 高级功能:支持多仓库工作区、并行任务调度、内置常见钩子等。
核心优势
性能优化
- 并行化仓库克隆与钩子安装。
- 使用
uv加速 Python 虚拟环境管理。 - 内置 Rust 实现的钩子(如代码格式化、静态检查)。
用户体验
- 支持工作区模式(monorepo 友好)。
- 新增
prek run --directory定向运行、prek list快速查看钩子。 - 自动更新支持冷却期限制(
--cooldown-days)。
兼容性
无缝迁移现有.pre-commit-config.yaml,已被 CPython、FastAPI、Apache Airflow 等知名项目采用。
安装方式
- 一键脚本:
sh curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.3.1/prek-installer.sh | sh - 包管理器:
支持 Homebrew、pip、Cargo、npm、Nix 等(详见项目文档)。
用户案例
包括 Python 官方仓库、FastAPI、Ruff 等 600+ 项目,部分已完全替换原版工具。
开源协议
MIT License,贡献指南见项目仓库。
项目地址:j178/prek | 文档:prek.j178.dev
评论总结
以下是评论内容的总结:
支持prek的观点
性能与兼容性提升
- 认为prek比pre-commit更快且完全兼容,功能更多(评论1:"Not just faster than pre-commit, and totally compatible. Also with more features.")
- 用户反馈无缝集成体验(评论2:"This has been such a breath of fresh air. It was seamless to drop into my projects.")
对monorepo的支持
- 支持多工作区配置,结果汇总清晰(评论9:"prek has support for monorepo/workspaces... The results are collated nicely. Just works.")
- Rust实现和uv包管理器加速Python钩子更新(评论9:"Having the default hooks reimplemented in Rust is minor bonus... and also using uv as the package manager speeds up hook updates")
对pre-commit的批评
设计缺陷
- 混合工具安装与linting,并行支持不足(评论6:"pre-commit mixes tool installation with linting... The interface isn't built with parallelism in mind")
- 依赖第三方仓库存在供应链风险(评论6:"It also uses a bunch of rando open source repos which is a supply chain nightmare")
用户体验问题
- 反对强制使用钩子,认为应提供显式工具(评论11:"I hate pre-commit hooks... I don't want to receive surprise errors")
- 钩子在rebase时运行或与未暂存文件冲突(评论17:"They are broken by design... they run during rebase and aren’t compatible with commits that leave unstaged files")
替代方案讨论
其他工具推荐
- 推荐基于Rust和WASI的替代方案,强调安全性与并行性(评论8:"in my version the hooks are WASI programs... there are no security issues, and you can run them in parallel")
- 建议使用后台守护进程实时检查(评论14:"I'm advocating for JJ to build a proper daemon that runs 'checks' per change in the background")
质疑prek的价值
- 认为Rust实现并非核心优势(评论15:"What difference does it make that it's written in Rust?")
- 质疑复杂钩子的必要性,可能源于大型团队问题(评论7:"is this solving a problem or is this solving a problem that the real problem created?")
中立/实用视角
- 部分用户未遇到性能问题(评论4:"I never have had an issue with performance with pre-commit")
- 建议结合CI和编辑器实时检查替代钩子(评论18:"Can people give examples of how they use pre-commit hooks that cannot be replaced by a combination of CI and IDE live error callouts?")
总结:prek因其性能、兼容性和monorepo支持获得认可,但关于pre-commit的设计缺陷和替代方案的讨论显示工具选择需权衡团队规模、安全性和用户体验。