prek

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

prek

prek

Use this skill when the user wants to set up or run Git hooks with
prek
.
prek
is a Rust reimplementation of
pre-commit
. Its main job is to run hooks that check code and repository files before commit or on demand: formatters, linters, validators, security checks, and custom project checks.
当用户想要使用
prek
设置或运行Git钩子时,使用本技能。
prek
pre-commit
的Rust重实现版本。它的主要功能是在提交前或按需运行钩子,用于检查代码和仓库文件:包括格式化工具、代码审查工具、验证工具、安全检查以及自定义项目检查。

Start with the public docs

从官方文档开始

优先使用面向大语言模型的索引:
根据该文件的指引,如需详细内容,请优先使用明确的Markdown文档:

What prek is for

prek 的适用场景

Use
prek
to:
  • run hooks that check source code and repo contents
  • install Git hook shims such as
    pre-commit
    ,
    pre-push
    , and
    commit-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-push
    commit-msg
  • 使用与
    pre-commit
    相同的钩子生态系统
  • 为支持的语言管理钩子运行时和工具链
  • 验证配置、更新固定的钩子版本号以及检查已配置的钩子
prek
运行的钩子通常用于以下任务:
  • 代码格式化与审查
  • 验证YAML、JSON、TOML、XML等类似文件
  • 防止出现合并冲突标记、私钥、过大文件以及错误的换行符

Authoring configs

编写配置

For new configs, prefer
prek.toml
.
Important repo types:
  • remote repo: normal hook repository such as
    https://github.com/astral-sh/ruff-pre-commit
  • repo = "local"
    : hooks defined in the current repository
  • repo = "meta"
    : config-checking hooks like
    check-hooks-apply
    ,
    check-useless-excludes
    , and
    identity
  • repo = "builtin"
    :
    prek
    's offline Rust-native hooks
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
[[repos.hooks]]
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
env
,
pass_filenames = false
, or
priority
.
Filtering patterns:
  • regex is the most portable choice:
    files = "\\.rs$"
  • prek
    also supports globs:
    files = { glob = "src/**/*.rs" }
  • use glob lists for multiple roots:
    exclude = { glob = ["target/**", "dist/**"] }
Scheduling:
  • smaller
    priority
    values run earlier
  • hooks with the same
    priority
    can run concurrently
  • top-level
    priorities
    can declare configuration-local aliases for numeric priorities
  • hook
    priority
    accepts either a non-negative integer or a declared alias
  • priority
    is evaluated within one config file, not across workspace projects
Useful
prek
-specific hook/config fields when editing TOML:
  • env
    for per-hook environment variables
  • priorities
    for readable, reusable priority aliases
  • priority
    for hook ordering and concurrency
  • minimum_prek_version
    for gating newer config features
  • orphan = true
    to isolate a nested workspace project from parent configs
对于新配置,优先使用
prek.toml
重要的仓库类型:
  • 远程仓库:常规钩子仓库,如
    https://github.com/astral-sh/ruff-pre-commit
  • repo = "local"
    :当前仓库中定义的钩子
  • repo = "meta"
    :配置检查钩子,如
    check-hooks-apply
    check-useless-excludes
    identity
  • repo = "builtin"
    prek
    的离线Rust原生钩子
最简示例:
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多行内联表。如果工具链中的编辑器或解析器尚不支持该语法,或者钩子包含多个字段(如
env
pass_filenames = false
priority
),请使用
[[repos.hooks]]
数组表格式。
过滤模式:
  • 正则表达式是最通用的选择:
    files = "\\.rs$"
  • prek
    也支持通配符:
    files = { glob = "src/**/*.rs" }
  • 多根目录使用通配符列表:
    exclude = { glob = ["target/**", "dist/**"] }
调度规则:
  • priority
    值越小,运行顺序越靠前
  • 相同
    priority
    的钩子可以并发运行
  • 顶层
    priorities
    可以为数值优先级声明配置本地别名
  • 钩子的
    priority
    接受非负整数或已声明的别名
  • priority
    仅在单个配置文件内生效,不会跨工作区项目
编辑TOML时,
prek
特有的实用钩子/配置字段:
  • env
    :为单个钩子设置环境变量
  • priorities
    :创建可读性强、可复用的优先级别名
  • priority
    :控制钩子的运行顺序和并发
  • minimum_prek_version
    :限制仅支持较新配置特性的prek版本
  • orphan = true
    :将嵌套工作区项目与父配置隔离

Default workflow

默认工作流

When adopting
prek
in a repository:
  1. Check whether the repo already has
    .pre-commit-config.yaml
    or
    .pre-commit-config.yml
    .
  2. If it does, usually keep that config and switch the commands from
    pre-commit
    to
    prek
    .
  3. If migrating from
    pre-commit
    , reinstall the Git shims with
    prek install -f
    .
  4. If it does not, prefer creating
    prek.toml
    for a fresh setup.
  5. Install
    prek
    .
  6. Validate the config with
    prek validate-config
    .
  7. Install the Git shims and prepare hook environments with
    prek install --prepare-hooks
    .
  8. Run everything once with
    prek run --all-files
    .
  9. For monorepos, consider nested configs,
    .prekignore
    , and
    orphan: true
    .
If the user explicitly wants maximum upstream portability, stay with
.pre-commit-config.yaml
and avoid
prek
-only keys.
在仓库中采用
prek
的步骤:
  1. 检查仓库是否已有
    .pre-commit-config.yaml
    .pre-commit-config.yml
    文件。
  2. 如果存在,通常保留该配置,只需将命令从
    pre-commit
    切换为
    prek
  3. 如果从
    pre-commit
    迁移,使用
    prek install -f
    重新安装Git垫片。
  4. 如果不存在,优先创建
    prek.toml
    进行全新设置。
  5. 安装
    prek
  6. 使用
    prek validate-config
    验证配置。
  7. 使用
    prek install --prepare-hooks
    安装Git垫片并准备钩子环境。
  8. 使用
    prek run --all-files
    运行所有钩子一次。
  9. 对于单体仓库,考虑使用嵌套配置、
    .prekignore
    orphan: true
如果用户明确要求与上游最大程度兼容,请继续使用
.pre-commit-config.yaml
,避免使用
prek
专属配置项。

Install and run

安装与运行

Common install methods:
  • uv tool install prek
  • brew install prek
  • mise use prek
  • cargo binstall prek
  • cargo install --locked prek
常见安装方式:
  • uv tool install prek
  • brew install prek
  • mise use prek
  • cargo binstall prek
  • cargo install --locked prek

Command guide

命令指南

  • prek install
    : install Git hook shims into the repo's effective hooks directory
  • prek prepare-hooks
    : prepare hook environments without installing Git shims
  • prek install --prepare-hooks
    : install shims and prepare environments in one step
  • prek run
    : run hooks for the current staged file selection
  • prek run --all-files
    : run hooks across the whole repository
  • prek run <hook-id>
    : run only one hook
  • prek list
    : list discovered hooks and projects
  • prek validate-config
    : validate
    prek.toml
    or
    .pre-commit-config.yaml
  • prek auto-update
    : update pinned hook revisions
  • prek util yaml-to-toml
    : convert an existing YAML config to
    prek.toml
  • prek util identify <path>
    : inspect file tags when
    types
    ,
    types_or
    , or
    exclude_types
    do not match as expected
Useful quality-of-life commands and options mentioned in the docs:
  • prek run --dry-run
  • prek run --directory <dir>
  • prek run --last-commit
  • prek run --skip <hook-or-project>
  • prek -C <dir> ...
For debugging:
  • prek run -vvv
  • PREK_NO_FAST_PATH=1 prek run
    : compare builtin fast-path behavior against the standard execution path
  • Check the Environment Variable Reference for
    PREK_*
    controls such as
    PREK_HOME
    ,
    PREK_SKIP
    , and concurrency limits.
  • prek install
    :将Git钩子垫片安装到仓库的有效钩子目录
  • prek prepare-hooks
    :准备钩子环境但不安装Git垫片
  • 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
    :更新固定的钩子版本号
  • prek util yaml-to-toml
    :将现有YAML配置转换为
    prek.toml
  • prek util identify <path>
    :当
    types
    types_or
    exclude_types
    匹配不符合预期时,检查文件标签
文档中提到的实用便捷命令和选项:
  • prek run --dry-run
  • prek run --directory <dir>
  • prek run --last-commit
  • prek 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
has two important builtin paths:
  • automatic fast path for supported hooks from
    https://github.com/pre-commit/pre-commit-hooks
  • explicit
    repo: builtin
    for offline, zero-setup built-in hooks
Reach for
repo: builtin
when speed, no-network setup, or minimal bootstrapping matters more than upstream
pre-commit
compatibility.
Builtin hooks called out by the docs include:
  • trailing-whitespace
  • check-added-large-files
  • check-case-conflict
  • check-illegal-windows-names
  • end-of-file-fixer
  • file-contents-sorter
  • fix-byte-order-marker
  • check-json
  • check-json5
  • pretty-format-json
  • check-toml
  • check-vcs-permalinks
  • check-yaml
  • check-xml
  • mixed-line-ending
  • check-symlinks
  • destroyed-symlinks
  • check-merge-conflict
  • detect-private-key
  • no-commit-to-branch
  • check-shebang-scripts-are-executable
  • check-executables-have-shebangs
prek
有两个重要的内置路径:
  • 针对
    https://github.com/pre-commit/pre-commit-hooks
    中支持的钩子的自动快速路径
  • 用于离线、零配置内置钩子的显式
    repo: builtin
当速度、无网络设置或最小化引导流程比与上游
pre-commit
兼容性更重要时,选择
repo: builtin
文档中重点提到的内置钩子包括:
  • trailing-whitespace
  • check-added-large-files
  • check-case-conflict
  • check-illegal-windows-names
  • end-of-file-fixer
  • file-contents-sorter
  • fix-byte-order-marker
  • check-json
  • check-json5
  • pretty-format-json
  • check-toml
  • check-vcs-permalinks
  • check-yaml
  • check-xml
  • mixed-line-ending
  • check-symlinks
  • destroyed-symlinks
  • check-merge-conflict
  • detect-private-key
  • no-commit-to-branch
  • check-shebang-scripts-are-executable
  • check-executables-have-shebangs

Practical guidance for agents

面向Agent的实用指南

  • Prefer
    prek.toml
    for new setups.
  • Prefer existing
    .pre-commit-config.yaml
    for migrations unless the user asks for TOML.
  • Reach for workspace mode in monorepos instead of forcing one giant root-only config.
  • Consider
    repo: builtin
    when offline or zero-setup hooks are useful.
  • If the repo already uses
    pre-commit-hooks
    , remember that
    prek
    can use built-in Rust implementations for some common hooks.
  • Start with a small default hook set, then add language-specific hooks the project already uses.
  • Use
    prek util yaml-to-toml
    instead of hand-converting YAML when migrating.
  • Before promising parity for a specific hook language, verify it in Language Support.
  • 全新设置优先使用
    prek.toml
  • 迁移时优先保留现有
    .pre-commit-config.yaml
    ,除非用户要求使用TOML。
  • 在单体仓库中使用工作区模式,而非强制使用单一的根配置。
  • 当需要离线或零配置钩子时,考虑使用
    repo: builtin
  • 如果仓库已使用
    pre-commit-hooks
    ,请记住
    prek
    可为部分常见钩子使用内置Rust实现。
  • 从一个小型默认钩子集合开始,再添加项目已在使用的特定语言钩子。
  • 迁移时使用
    prek util yaml-to-toml
    而非手动转换YAML。
  • 在承诺支持特定钩子语言之前,请在语言支持中进行验证。