这个配置文件是用于 pre-commit 工具的钩子配置,目的是在代码提交(commit)前自动执行一系列代码检查和格式化任务。以下是该配置文件的详细解释:
fail_fast: false
- 作用:设置
fail_fast
为false
,表示即使某个钩子(hook)执行失败,pre-commit 仍会继续执行后续的钩子。如果设置为true
,则会在第一个失败的钩子后立即停止。 - 意义:允许一次性收集所有问题,而不是在第一个问题后中断流程。
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- 仓库说明:这是 pre-commit 官方维护的通用钩子集合,版本锁定为
v4.3.0
。 - 钩子详解:
- check-byte-order-marker:检查文件开头是否有无效的字节顺序标记(BOM),通常用于 UTF-8 文件。
- check-case-conflict:检查文件名大小写冲突(例如
file.txt
和File.txt
在区分大小写的文件系统中会被视为不同文件)。 - check-merge-conflict:检查 Git 合并冲突标记(如
<<<<<<<
,=======
,>>>>>>>
)。 - check-symlinks:检查符号链接是否有效。
- check-yaml:验证 YAML 文件语法是否正确。
- end-of-file-fixer:确保文件以换行符结尾。
- mixed-line-ending:检查文件是否混合使用不同类型的换行符(如
\n
和\r\n
)。 - trailing-whitespace:删除行尾的多余空格。
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- 仓库说明:这是 Python 代码格式化工具 Black,版本锁定为
22.10.0
。 - 钩子作用:在提交前自动格式化 Python 代码,确保代码风格统一(如缩进、括号、行长度等)。
- repo: local
hooks:
- id: cargo-fmt
name: cargo fmt
description: Format files with rustfmt.
entry: bash -c 'cargo fmt -- --check'
language: rust
files: \.rs$
args: []
- id: cargo-deny
name: cargo deny check
description: Check cargo dependencies
entry: bash -c 'cargo deny check -d'
language: rust
files: \.rs$
args: []
- id: typos
name: typos
description: check typo
entry: bash -c 'typos'
language: rust
files: \.*
pass_filenames: false
- id: cargo-check
name: cargo check
description: Check the package for errors.
entry: bash -c 'cargo check --all'
language: rust
files: \.rs$
pass_filenames: false
- id: cargo-clippy
name: cargo clippy
description: Lint rust sources
entry: bash -c 'cargo clippy --all-targets --all-features --tests --benches -- -D warnings'
language: rust
files: \.rs$
pass_filenames: false
- id: cargo-test
name: cargo test
description: unit test for the project
entry: bash -c 'cargo nextest run --all-features'
language: rust
files: \.rs$
pass_filenames: false
- 仓库说明:
local
表示这些钩子是本地定义的,不依赖外部仓库。 - 钩子详解:
- 作用:使用
rustfmt
格式化 Rust 代码。 - 命令:
cargo fmt -- --check
仅检查格式是否规范(不实际修改文件)。 - 触发条件:针对
.rs
结尾的文件。
- 作用:检查 Cargo 依赖项的安全性和许可合规性。
- 命令:
cargo deny check -d
运行依赖项检查。 - 触发条件:针对
.rs
结尾的文件。
- 作用:检查文本中的拼写错误。
- 命令:
typos
检查所有文件(files: \.*$
表示匹配所有文件)。 - 特点:
pass_filenames: false
表示不将文件名传递给typos
(由typos
自行扫描所有文件)。
- 作用:检查 Rust 项目的编译错误。
- 命令:
cargo check --all
检查所有目标(binaries, libraries 等)。 - 触发条件:针对
.rs
结尾的文件。
- 作用:使用 Clippy 对 Rust 代码进行静态分析(linting)。
- 命令:
cargo clippy --all-targets --all-features --tests --benches -- -D warnings
--all-targets
:检查所有目标(binaries, libraries, tests, benches)。--all-features
:启用所有 Cargo features。-D warnings
:将 Clippy 的警告视为错误。
- 触发条件:针对
.rs
结尾的文件。
- 作用:运行 Rust 项目的单元测试。
- 命令:
cargo nextest run --all-features
使用 nextest 测试框架运行所有测试。 - 特点:
--all-features
启用所有 Cargo features。 - 触发条件:针对
.rs
结尾的文件。
- 适用场景:此配置适用于 Rust 项目,结合了通用代码检查、Python 格式化和 Rust 项目专用的工具链。
- 核心功能:
- 代码质量:通过
rustfmt
、clippy
、cargo check
确保代码格式和逻辑正确。 - 依赖安全:通过
cargo deny
检查依赖项的安全性。 - 测试保障:通过
cargo test
运行所有测试。 - 拼写检查:通过
typos
检查文档和代码中的拼写错误。 - 通用检查:通过 pre-commit-hooks 检查文件一致性、YAML 格式等。
- 代码质量:通过
- 安装 pre-commit:
pip install pre-commit
- 初始化配置:
pre-commit install
- 手动运行所有钩子:
pre-commit run --all-files
- 调试失败的钩子:
- 如果某个钩子失败,可以根据提示修复问题,或临时跳过钩子(不推荐长期使用)。
- Rust 工具链:确保已安装 Rust 工具链(
rustc
、cargo
)及依赖的工具(如rustfmt
、clippy
、cargo-deny
、typos
)。 - nextest 替代测试工具:
cargo-test
使用了nextest
而非默认的cargo test
,需提前安装:cargo install cargo-nextest
- 版本兼容性:配置中使用的工具版本(如
[email protected]
)需与项目环境兼容。
如果有其他问题,欢迎随时提问!