prek
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseprek
prek
Use this skill when the user wants to set up or run Git hooks with .
prekprekpre-commit当用户想要使用设置或运行Git钩子时,使用本技能。
prekprekpre-commitStart with the public docs
从官方文档开始
Use the LLM-oriented index first:
Per that file, prefer explicit markdown docs when you need details:
What prek is for
prek 的适用场景
Use to:
prek- run hooks that check source code and repo contents
- install Git hook shims such as ,
pre-commit, andpre-pushcommit-msg - run the same hook ecosystem used by
pre-commit - manage hook runtimes and toolchains for supported languages
- validate configs, update pinned hook revisions, and inspect configured hooks
Typical jobs for hooks run by :
prek- formatting and linting code
- validating YAML, JSON, TOML, XML, and similar files
- preventing merge-conflict markers, private keys, oversized files, and bad line endings
使用可以:
prek- 运行用于检查源代码和仓库内容的钩子
- 安装Git钩子垫片(shims),如、
pre-commit和pre-pushcommit-msg - 使用与相同的钩子生态系统
pre-commit - 为支持的语言管理钩子运行时和工具链
- 验证配置、更新固定的钩子版本号以及检查已配置的钩子
prek- 代码格式化与审查
- 验证YAML、JSON、TOML、XML等类似文件
- 防止出现合并冲突标记、私钥、过大文件以及错误的换行符
Authoring configs
编写配置
For new configs, prefer .
prek.tomlImportant repo types:
- remote repo: normal hook repository such as
https://github.com/astral-sh/ruff-pre-commit - : hooks defined in the current repository
repo = "local" - : config-checking hooks like
repo = "meta",check-hooks-apply, andcheck-useless-excludesidentity - :
repo = "builtin"'s offline Rust-native hooksprek
Minimal examples:
toml
[[repos]]
repo = "https://github.com/astral-sh/ruff-pre-commit"
rev = "v0.14.3"
hooks = [
{ id = "ruff" },
{ id = "ruff-format" },
]toml
[[repos]]
repo = "local"
hooks = [
{
id = "cargo-fmt",
name = "cargo fmt",
language = "system",
entry = "cargo fmt --",
files = "\\.rs$",
},
]toml
[[repos]]
repo = "builtin"
hooks = [
{ id = "trailing-whitespace" },
{ id = "check-yaml" },
]These examples use TOML 1.1 multiline inline tables. Use array-of-tables if an editor or parser in the toolchain does not support that syntax yet, or when a hook has many fields such as , , or .
[[repos.hooks]]envpass_filenames = falsepriorityFiltering patterns:
- regex is the most portable choice:
files = "\\.rs$" - also supports globs:
prekfiles = { glob = "src/**/*.rs" } - use glob lists for multiple roots:
exclude = { glob = ["target/**", "dist/**"] }
Scheduling:
- smaller values run earlier
priority - hooks with the same can run concurrently
priority - top-level can declare configuration-local aliases for numeric priorities
priorities - hook accepts either a non-negative integer or a declared alias
priority - is evaluated within one config file, not across workspace projects
priority
Useful -specific hook/config fields when editing TOML:
prek- for per-hook environment variables
env - for readable, reusable priority aliases
priorities - for hook ordering and concurrency
priority - for gating newer config features
minimum_prek_version - to isolate a nested workspace project from parent configs
orphan = true
对于新配置,优先使用。
prek.toml重要的仓库类型:
- 远程仓库:常规钩子仓库,如
https://github.com/astral-sh/ruff-pre-commit - :当前仓库中定义的钩子
repo = "local" - :配置检查钩子,如
repo = "meta"、check-hooks-apply和check-useless-excludesidentity - :
repo = "builtin"的离线Rust原生钩子prek
最简示例:
toml
[[repos]]
repo = "https://github.com/astral-sh/ruff-pre-commit"
rev = "v0.14.3"
hooks = [
{ id = "ruff" },
{ id = "ruff-format" },
]toml
[[repos]]
repo = "local"
hooks = [
{
id = "cargo-fmt",
name = "cargo fmt",
language = "system",
entry = "cargo fmt --",
files = "\\.rs$",
},
]toml
[[repos]]
repo = "builtin"
hooks = [
{ id = "trailing-whitespace" },
{ id = "check-yaml" },
]这些示例使用了TOML 1.1多行内联表。如果工具链中的编辑器或解析器尚不支持该语法,或者钩子包含多个字段(如、或),请使用数组表格式。
envpass_filenames = falsepriority[[repos.hooks]]过滤模式:
- 正则表达式是最通用的选择:
files = "\\.rs$" - 也支持通配符:
prekfiles = { glob = "src/**/*.rs" } - 多根目录使用通配符列表:
exclude = { glob = ["target/**", "dist/**"] }
调度规则:
- 值越小,运行顺序越靠前
priority - 相同的钩子可以并发运行
priority - 顶层可以为数值优先级声明配置本地别名
priorities - 钩子的接受非负整数或已声明的别名
priority - 仅在单个配置文件内生效,不会跨工作区项目
priority
编辑TOML时,特有的实用钩子/配置字段:
prek- :为单个钩子设置环境变量
env - :创建可读性强、可复用的优先级别名
priorities - :控制钩子的运行顺序和并发
priority - :限制仅支持较新配置特性的prek版本
minimum_prek_version - :将嵌套工作区项目与父配置隔离
orphan = true
Default workflow
默认工作流
When adopting in a repository:
prek- Check whether the repo already has or
.pre-commit-config.yaml..pre-commit-config.yml - If it does, usually keep that config and switch the commands from to
pre-commit.prek - If migrating from , reinstall the Git shims with
pre-commit.prek install -f - If it does not, prefer creating for a fresh setup.
prek.toml - Install .
prek - Validate the config with .
prek validate-config - Install the Git shims and prepare hook environments with .
prek install --prepare-hooks - Run everything once with .
prek run --all-files - For monorepos, consider nested configs, , and
.prekignore.orphan: true
If the user explicitly wants maximum upstream portability, stay with and avoid -only keys.
.pre-commit-config.yamlprek在仓库中采用的步骤:
prek- 检查仓库是否已有或
.pre-commit-config.yaml文件。.pre-commit-config.yml - 如果存在,通常保留该配置,只需将命令从切换为
pre-commit。prek - 如果从迁移,使用
pre-commit重新安装Git垫片。prek install -f - 如果不存在,优先创建进行全新设置。
prek.toml - 安装。
prek - 使用验证配置。
prek validate-config - 使用安装Git垫片并准备钩子环境。
prek install --prepare-hooks - 使用运行所有钩子一次。
prek run --all-files - 对于单体仓库,考虑使用嵌套配置、和
.prekignore。orphan: true
如果用户明确要求与上游最大程度兼容,请继续使用,避免使用专属配置项。
.pre-commit-config.yamlprekInstall and run
安装与运行
Common install methods:
uv tool install prekbrew install prekmise use prekcargo binstall prekcargo install --locked prek
常见安装方式:
uv tool install prekbrew install prekmise use prekcargo binstall prekcargo install --locked prek
Command guide
命令指南
- : install Git hook shims into the repo's effective hooks directory
prek install - : prepare hook environments without installing Git shims
prek prepare-hooks - : install shims and prepare environments in one step
prek install --prepare-hooks - : run hooks for the current staged file selection
prek run - : run hooks across the whole repository
prek run --all-files - : run only one hook
prek run <hook-id> - : list discovered hooks and projects
prek list - : validate
prek validate-configorprek.toml.pre-commit-config.yaml - : update pinned hook revisions
prek auto-update - : convert an existing YAML config to
prek util yaml-to-tomlprek.toml - : inspect file tags when
prek util identify <path>,types, ortypes_ordo not match as expectedexclude_types
Useful quality-of-life commands and options mentioned in the docs:
prek run --dry-runprek run --directory <dir>prek run --last-commitprek run --skip <hook-or-project>prek -C <dir> ...
For debugging:
prek run -vvv- : compare builtin fast-path behavior against the standard execution path
PREK_NO_FAST_PATH=1 prek run - Check the Environment Variable Reference for controls such as
PREK_*,PREK_HOME, and concurrency limits.PREK_SKIP
- :将Git钩子垫片安装到仓库的有效钩子目录
prek install - :准备钩子环境但不安装Git垫片
prek prepare-hooks - :一步完成垫片安装和环境准备
prek install --prepare-hooks - :为当前暂存的文件运行钩子
prek run - :在整个仓库中运行钩子
prek run --all-files - :仅运行指定钩子
prek run <hook-id> - :列出已发现的钩子和项目
prek list - :验证
prek validate-config或prek.toml配置.pre-commit-config.yaml - :更新固定的钩子版本号
prek auto-update - :将现有YAML配置转换为
prek util yaml-to-tomlprek.toml - :当
prek util identify <path>、types或types_or匹配不符合预期时,检查文件标签exclude_types
文档中提到的实用便捷命令和选项:
prek run --dry-runprek run --directory <dir>prek run --last-commitprek run --skip <hook-or-project>prek -C <dir> ...
调试相关:
prek run -vvv- :对比内置快速路径与标准执行路径的行为差异
PREK_NO_FAST_PATH=1 prek run - 查看环境变量参考获取控制项,如
PREK_*、PREK_HOME和并发限制。PREK_SKIP
Built-in hook guidance
内置钩子指南
prek- automatic fast path for supported hooks from
https://github.com/pre-commit/pre-commit-hooks - explicit for offline, zero-setup built-in hooks
repo: builtin
Reach for when speed, no-network setup, or minimal bootstrapping matters more than upstream compatibility.
repo: builtinpre-commitBuiltin hooks called out by the docs include:
trailing-whitespacecheck-added-large-filescheck-case-conflictcheck-illegal-windows-namesend-of-file-fixerfile-contents-sorterfix-byte-order-markercheck-jsoncheck-json5pretty-format-jsoncheck-tomlcheck-vcs-permalinkscheck-yamlcheck-xmlmixed-line-endingcheck-symlinksdestroyed-symlinkscheck-merge-conflictdetect-private-keyno-commit-to-branchcheck-shebang-scripts-are-executablecheck-executables-have-shebangs
prek- 针对中支持的钩子的自动快速路径
https://github.com/pre-commit/pre-commit-hooks - 用于离线、零配置内置钩子的显式
repo: builtin
当速度、无网络设置或最小化引导流程比与上游兼容性更重要时,选择。
pre-commitrepo: builtin文档中重点提到的内置钩子包括:
trailing-whitespacecheck-added-large-filescheck-case-conflictcheck-illegal-windows-namesend-of-file-fixerfile-contents-sorterfix-byte-order-markercheck-jsoncheck-json5pretty-format-jsoncheck-tomlcheck-vcs-permalinkscheck-yamlcheck-xmlmixed-line-endingcheck-symlinksdestroyed-symlinkscheck-merge-conflictdetect-private-keyno-commit-to-branchcheck-shebang-scripts-are-executablecheck-executables-have-shebangs
Practical guidance for agents
面向Agent的实用指南
- Prefer for new setups.
prek.toml - Prefer existing for migrations unless the user asks for TOML.
.pre-commit-config.yaml - Reach for workspace mode in monorepos instead of forcing one giant root-only config.
- Consider when offline or zero-setup hooks are useful.
repo: builtin - If the repo already uses , remember that
pre-commit-hookscan use built-in Rust implementations for some common hooks.prek - Start with a small default hook set, then add language-specific hooks the project already uses.
- Use instead of hand-converting YAML when migrating.
prek util yaml-to-toml - Before promising parity for a specific hook language, verify it in Language Support.
- 全新设置优先使用。
prek.toml - 迁移时优先保留现有,除非用户要求使用TOML。
.pre-commit-config.yaml - 在单体仓库中使用工作区模式,而非强制使用单一的根配置。
- 当需要离线或零配置钩子时,考虑使用。
repo: builtin - 如果仓库已使用,请记住
pre-commit-hooks可为部分常见钩子使用内置Rust实现。prek - 从一个小型默认钩子集合开始,再添加项目已在使用的特定语言钩子。
- 迁移时使用而非手动转换YAML。
prek util yaml-to-toml - 在承诺支持特定钩子语言之前,请在语言支持中进行验证。