prek
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseprek
prek
Onboard and configure prek — a fast, Rust-powered pre-commit hook manager.
接入并配置 prek——一款快速、基于 Rust 的 pre-commit hook 管理器。
When to use
使用场景
- Project has no and would benefit from pre-commit hooks
prek.toml - User wants to add/audit/extend hooks in an existing
prek.toml - User is migrating from to prek's native TOML format
.pre-commit-config.yaml
- 项目没有 ,且可从 pre-commit hooks 中获益
prek.toml - 用户希望在现有 中添加、审查或扩展 hooks
prek.toml - 用户正从 迁移到 prek 原生 TOML 格式
.pre-commit-config.yaml
Protocol
协议
1. Detect project type
1. 检测项目类型
Scan the repo root for language markers:
| Marker file | Language/Framework |
|---|---|
| Rust |
| Python |
| TypeScript/JavaScript |
| Go |
| Ruby |
| Shell |
| Docker |
Multiple markers = polyglot project. Suggest hooks for all detected languages.
扫描仓库根目录中的语言标记:
| 标记文件 | 语言/框架 |
|---|---|
| Rust |
| Python |
| TypeScript/JavaScript |
| Go |
| Ruby |
| Shell |
| Docker |
存在多个标记表示这是多语言项目。为所有检测到的语言推荐 hooks。
2. Check existing config
2. 检查现有配置
- If exists, read it and identify what's already configured
prek.toml - If exists, offer to migrate (prek reads YAML natively, but TOML is the native format with more features like glob patterns)
.pre-commit-config.yaml
- 如果存在 ,读取它并确定已经配置的内容
prek.toml - 如果存在 ,提供迁移选项(prek 原生支持读取 YAML,但 TOML 是其原生格式,并提供 glob patterns 等更多功能)
.pre-commit-config.yaml
3. Suggest hooks
3. 推荐 hooks
Present hooks in categories. Show what each hook does before adding. Use this reference:
按类别展示 hooks。添加前说明每个 hook 的作用。使用以下参考:
Built-in hooks (repo = "builtin")
内置 hooks(repo = "builtin"
)
repo = "builtin"Fast, offline, zero-setup. Always suggest these as the foundation:
| Hook ID | What it does |
|---|---|
| Trims trailing whitespace. Args: |
| Ensures files end with a newline |
| Validates YAML syntax. Args: |
| Validates TOML syntax |
| Validates JSON syntax (rejects duplicate keys) |
| Validates JSON5 syntax |
| Validates XML syntax |
| Detects unresolved merge conflict markers |
| Catches accidentally committed private keys |
| Blocks large files. Args: |
| Detects filenames that differ only by case |
| Validates symlinks point to existing targets |
| Ensures executable scripts have shebangs |
| Detects mixed line endings (LF vs CRLF) |
| Removes UTF-8 BOM |
| Blocks direct commits to protected branches |
快速、离线、零配置。始终将这些作为基础推荐:
| Hook ID | 作用 |
|---|---|
| 清理行尾空白。参数: |
| 确保文件以换行符结尾 |
| 验证 YAML 语法。参数: |
| 验证 TOML 语法 |
| 验证 JSON 语法(拒绝重复键) |
| 验证 JSON5 语法 |
| 验证 XML 语法 |
| 检测未解决的合并冲突标记 |
| 检测误提交的私钥 |
| 阻止大文件。参数: |
| 检测仅大小写不同的文件名 |
| 验证符号链接指向现有目标 |
| 确保可执行脚本包含 shebang |
| 检测混合换行符(LF 与 CRLF) |
| 移除 UTF-8 BOM |
| 阻止直接向受保护分支提交 |
Language-specific hooks (community repos)
语言特定 hooks(社区仓库)
Rust:
toml
undefinedRust:
toml
undefinedLocal hooks (no external repo needed)
Local hooks (no external repo needed)
[[repos]]
repo = "local"
[[repos.hooks]]
id = "cargo-fmt"
name = "cargo fmt"
language = "system"
entry = "cargo fmt --"
files = "\.rs$"
pass_filenames = true
[[repos.hooks]]
id = "cargo-clippy"
name = "cargo clippy"
language = "system"
entry = "cargo clippy --all-targets --all-features -- -D warnings"
files = "\.rs$"
pass_filenames = false
always_run = true
**Python:**
```toml[[repos]]
repo = "local"
[[repos.hooks]]
id = "cargo-fmt"
name = "cargo fmt"
language = "system"
entry = "cargo fmt --"
files = "\.rs$"
pass_filenames = true
[[repos.hooks]]
id = "cargo-clippy"
name = "cargo clippy"
language = "system"
entry = "cargo clippy --all-targets --all-features -- -D warnings"
files = "\.rs$"
pass_filenames = false
always_run = true
**Python:**
```tomlRuff (linter + formatter)
Ruff (linter + formatter)
[[repos]]
repo = "https://github.com/astral-sh/ruff-pre-commit"
rev = "v0.11.6"
hooks = [
{ id = "ruff", args = ["--fix"] },
{ id = "ruff-format" },
]
**TypeScript/JavaScript:**
```toml
[[repos]]
repo = "local"
[[repos.hooks]]
id = "eslint"
name = "eslint"
language = "system"
entry = "npx eslint --fix"
files = "\\.(ts|tsx|js|jsx)$"
pass_filenames = true
[[repos.hooks]]
id = "prettier"
name = "prettier"
language = "system"
entry = "npx prettier --write"
files = "\\.(ts|tsx|js|jsx|json|css|md)$"
pass_filenames = trueGo:
toml
[[repos]]
repo = "local"
[[repos.hooks]]
id = "go-fmt"
name = "go fmt"
language = "system"
entry = "gofmt -w"
files = "\\.go$"
pass_filenames = true
[[repos.hooks]]
id = "go-vet"
name = "go vet"
language = "system"
entry = "go vet ./..."
pass_filenames = false
always_run = trueShell:
toml
[[repos]]
repo = "https://github.com/shellcheck-py/shellcheck-py"
rev = "a23f6b85d0fdd5bb9d564e2579e678033debbdff"
hooks = [
{ id = "shellcheck" },
][[repos]]
repo = "https://github.com/astral-sh/ruff-pre-commit"
rev = "v0.11.6"
hooks = [
{ id = "ruff", args = ["--fix"] },
{ id = "ruff-format" },
]
**TypeScript/JavaScript:**
```toml
[[repos]]
repo = "local"
[[repos.hooks]]
id = "eslint"
name = "eslint"
language = "system"
entry = "npx eslint --fix"
files = "\\.(ts|tsx|js|jsx)$"
pass_filenames = true
[[repos.hooks]]
id = "prettier"
name = "prettier"
language = "system"
entry = "npx prettier --write"
files = "\\.(ts|tsx|js|jsx|json|css|md)$"
pass_filenames = trueGo:
toml
[[repos]]
repo = "local"
[[repos.hooks]]
id = "go-fmt"
name = "go fmt"
language = "system"
entry = "gofmt -w"
files = "\\.go$"
pass_filenames = true
[[repos.hooks]]
id = "go-vet"
name = "go vet"
language = "system"
entry = "go vet ./..."
pass_filenames = false
always_run = trueShell:
toml
[[repos]]
repo = "https://github.com/shellcheck-py/shellcheck-py"
rev = "a23f6b85d0fdd5bb9d564e2579e678033debbdff"
hooks = [
{ id = "shellcheck" },
]4. Fetch latest versions
4. 获取最新版本
Before writing config, check the community hook repos for their current recommended versions (ruff-pre-commit, shellcheck-py, etc.) — via the Context7 MCP if your harness has it, your harness's doc-lookup tool, or the repos' GitHub releases pages. Fall back to the versions listed above if no lookup is available.
写入配置前,检查社区 hook 仓库当前推荐的版本(ruff-pre-commit、shellcheck-py 等)——如果 harness 提供 Context7 MCP,则使用它;也可使用 harness 的文档查询工具或仓库的 GitHub Releases 页面。如果无法查询,则回退到上述版本。
5. Generate or augment prek.toml
5. 生成或扩展 prek.toml
New project: Generate a complete with:
prek.toml- Built-in hooks (always include: ,
trailing-whitespace,end-of-file-fixer,check-merge-conflict,detect-private-key)check-added-large-files - Language-specific hooks based on detection
- Comments explaining each section
Existing project: Show a diff of what would be added. Don't duplicate hooks already present.
新项目:生成完整的 ,其中包括:
prek.toml- 内置 hooks(始终包含:、
trailing-whitespace、end-of-file-fixer、check-merge-conflict、detect-private-key)check-added-large-files - 根据检测结果添加语言特定 hooks
- 用于说明各部分的注释
现有项目:显示将要添加内容的 diff。不要重复已经存在的 hooks。
6. Install and verify
6. 安装并验证
Ask the user before running these commands. For an audit or a proposed config change, stop at the proposed diff and do not run either command unless the user authorizes installation or a mutating run:
bash
prek install
prek run --all-filesIf isn't installed, tell the user to install it ( or ). Keep installation advice scoped to this repository; do not edit personal package manifests or dotfiles.
prekcargo install prekbrew install prek运行以下命令前先询问用户。对于审查或配置变更提案,停在拟议 diff 处;除非用户授权安装或执行可能修改文件的运行,否则不要运行上述任一命令:
bash
prek install
prek run --all-files如果尚未安装 ,告知用户可以通过 或 安装。安装建议仅限于当前仓库;不要修改个人 package manifests 或 dotfiles。
prekcargo install prekbrew install prekAdvanced options
高级选项
Stage-specific hooks
阶段特定 hooks
Slow checks (tests, full builds) belong on , not :
pre-pushpre-committoml
[[repos.hooks]]
id = "pytest"
name = "Run tests"
language = "system"
entry = "pytest -x"
pass_filenames = false
always_run = true
stages = ["pre-push"]
priority = 100stages = ["pre-push"]default_install_hook_types = ["pre-commit", "pre-push"]prek installprek run --hook-stage pre-push较慢的检查(测试、完整构建)应放在 阶段,而不是 :
pre-pushpre-committoml
[[repos.hooks]]
id = "pytest"
name = "Run tests"
language = "system"
entry = "pytest -x"
pass_filenames = false
always_run = true
stages = ["pre-push"]
priority = 100仅设置 并不会安装 pre-push Git shim。在顶层配置中添加 (请对照 prek 文档确认键名),然后请用户重新运行已授权的 ,以便 Git 安装 pre-push shim。 会运行该阶段,但不能证明 shim 已安装。
stages = ["pre-push"]default_install_hook_types = ["pre-commit", "pre-push"]prek installprek run --hook-stage pre-pushGlobal filters
全局过滤器
Exclude vendored/generated code from all hooks:
toml
exclude = "^(vendor/|generated/|node_modules/)"从所有 hooks 中排除 vendored/generated 代码:
toml
exclude = "^(vendor/|generated/|node_modules/)"Glob patterns (prek-only)
Glob patterns(prek 专属)
Prek supports glob patterns as an alternative to regex (not compatible with upstream pre-commit):
toml
files = { glob = "src/**/*.py" }
exclude = { glob = ["vendor/**", "dist/**"] }Prek 支持使用 glob patterns 作为 regex 的替代方案(与上游 pre-commit 不兼容):
toml
files = { glob = "src/**/*.py" }
exclude = { glob = ["vendor/**", "dist/**"] }Migration from pre-commit
从 pre-commit 迁移
If exists:
.pre-commit-config.yaml- prek reads YAML natively — the existing config works as-is
- Offer to convert to for access to prek-only features (glob patterns,
prek.toml)repo: builtin - Replace with
repo: https://github.com/pre-commit/pre-commit-hookswhere hooks are supportedrepo: builtin
如果存在 :
.pre-commit-config.yaml- prek 原生支持读取 YAML——现有配置可直接使用
- 提供将其转换为 的选项,以使用 prek 专属功能(glob patterns、
prek.toml)repo: builtin - 在相应 hooks 受支持的情况下,将 替换为
repo: https://github.com/pre-commit/pre-commit-hooksrepo: builtin
What You Don't Do
不执行的操作
- Configure CI pipelines, GitHub Actions, or other CI hook systems
- Manage husky, lint-staged, or pre-commit (Python) configs
- Remove existing hooks without user confirmation
- Modify directly — only generates
.pre-commit-config.yamlprek.toml
- 配置 CI pipelines、GitHub Actions 或其他 CI hook 系统
- 管理 husky、lint-staged 或 pre-commit(Python)配置
- 未经用户确认就删除现有 hooks
- 直接修改 ——只生成
.pre-commit-config.yamlprek.toml
Gotchas
注意事项
- Context7 may be unavailable — fall back to training data for hook recommendations
- can fail on first run if auto-fix hooks reformat everything
prek run --all-files - Rev pinning in goes stale — recommend periodic updates
prek.toml - needs write access to
prek install— fails in worktrees with shared hooks.git/hooks/
- Context7 可能不可用——回退到训练数据中的 hook 推荐
- 首次运行 时,如果自动修复 hooks 重新格式化所有内容,可能会失败
prek run --all-files - 中固定的
prek.toml会过时——建议定期更新rev - 需要对
prek install的写权限——在共享 hooks 的 worktree 中会失败.git/hooks/