pre-commit/pre-commit-hooks

GitHub: pre-commit/pre-commit-hooks

一组开箱即用的 Git pre-commit 钩子集合,覆盖代码质量、安全检测、文件规范和提交约束等多种场景,帮助团队在提交阶段自动拦截常见问题。

Stars: 6506 | Forks: 789

[![构建状态](https://static.pigsec.cn/wp-content/uploads/repos/2026/05/f408de9f6b022045.svg)](https://github.com/pre-commit/pre-commit-hooks/actions/workflows/main.yml) [![pre-commit.ci 状态](https://results.pre-commit.ci/badge/github/pre-commit/pre-commit-hooks/main.svg)](https://results.pre-commit.ci/latest/github/pre-commit/pre-commit-hooks/main) # pre-commit-hooks 一些开箱即用的 pre-commit hooks。 另见:https://github.com/pre-commit/pre-commit ### 配合 pre-commit 使用 pre-commit-hooks 将此内容添加到你的 `.pre-commit-config.yaml` 中 ``` - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 # Use the ref you want to point at hooks: - id: trailing-whitespace # - id: ... ``` ### 可用的 Hooks #### `check-added-large-files` 防止提交巨大的文件。 - 通过 `args: ['--maxkb=123']` 指定“过大”的标准(默认为 500kB)。 - 仅将检查限制在 Git 中标记为暂存待添加的文件。 - 如果安装了 `git-lfs`,将跳过 lfs 文件 (需要 `git-lfs>=2.2.1`) - `--enforce-all` - 检查所有列出的文件,而不仅是暂存待添加的文件。 #### `check-ast` 仅检查文件是否能被解析为有效的 Python。 #### `check-builtin-literals` 要求在初始化空的或零值的 Python 内置类型时使用字面量语法。 - 允许使用位置参数调用构造函数(例如,`list('abc')`)。 - 允许从 `builtins`(`__builtin__`)命名空间调用构造函数(`builtins.list()`)。 - 使用 `--ignore=type1,type2,…` 忽略特定内置类型的此要求。 - 使用 `--no-allow-dict-kwargs` 禁止 `dict` 关键字语法。 #### `check-case-conflict` 检查在 MacOS HFS+ 或 Windows FAT 等不区分大小写的文件系统上会发生名称冲突的文件。 #### `check-executables-have-shebangs` 检查非二进制的可执行文件是否具有正确的 shebang。 #### `check-illegal-windows-names` 检查无法在 Windows 上创建的文件。 #### `check-json` 尝试加载所有 JSON 文件以验证语法。 #### `check-merge-conflict` 检查包含合并冲突字符串的文件。 - `--assume-in-merge` - 允许在没有进行中的合并操作时运行该 hook #### `check-shebang-scripts-are-executable` 检查带有 shebang 的脚本是否具有可执行权限。 #### `check-symlinks` 检查不指向任何目标的符号链接。 #### `check-toml` 尝试加载所有 TOML 文件以验证语法。 #### `check-vcs-permalinks` 确保指向 VCS 网站的链接是永久链接(permalinks)。 - `--additional-github-domain DOMAIN` - 添加对指定域名的检查。 可以重复多次。例如,如果你的公司使用 GitHub Enterprise,你可以使用类似 `--additional-github-domain github.example.com` 的配置 #### `check-xml` 尝试加载所有 XML 文件以验证语法。 #### `check-yaml` 尝试加载所有 YAML 文件以验证语法。 - `--allow-multiple-documents` - 允许使用[多文档语法](http://www.yaml.org/spec/1.2/spec.html#YAML)的 YAML 文件 - `--unsafe` - 与其加载文件,不如仅解析其语法。 仅进行语法检查会启用扩展和原本被禁止的不安全构造。 使用此选项将失去对其他 YAML 实现的可移植性保证。 隐含 `--allow-multiple-documents`。 #### `debug-statements` 检查 Python 源码中的调试器导入(debugger imports)和 py37+ 的 `breakpoint()` 调用。 #### `destroyed-symlinks` 检测那些被更改为常规文件,且内容为该符号链接曾指向路径的符号链接。 这通常发生在 Windows 上,当用户克隆了一个包含符号链接的仓库, 但他们没有创建符号链接的权限时。 #### `detect-aws-credentials` 检查是否存在你通过 AWS CLI 设置的 AWS 密钥。 以下参数可用: - `--credentials-file CREDENTIALS_FILE` - 位于非标准位置的附加 AWS CLI 风格 配置文件,用于从中获取配置的凭证。可以重复多次。 - `--allow-missing-credentials` - 允许在未检测到凭证时 hook 通过。 #### `detect-private-key` 检查是否存在私钥。 #### `double-quote-string-fixer` 此 hook 将双引号字符串替换为单引号字符串。 #### `end-of-file-fixer` 确保文件以一个且仅以一个换行符结尾。 #### `file-contents-sorter` 对指定文件中的行进行排序(默认按字母顺序)。 你必须提供目标 [`files`](https://pre-commit.com/#config-files) 作为输入。 请注意,此 hook 会移除空行,并且不会保留任何注释。 所有换行符都将被转换为换行符(`\n`)。 以下参数可用: - `--ignore-case` - 将小写字符转换为大写字符进行处理。 - `--unique` - 确保每一行都是唯一的。 #### `fix-byte-order-marker` 移除 UTF-8 字节顺序标记(BOM) #### `forbid-new-submodules` 防止添加新的 git submodule。 这旨在作为辅助迁移远离 submodule 的工具。如果你想 完全禁止它们,请使用 `forbid-submodules` #### `forbid-submodules` 禁止仓库中存在任何 submodule。 #### `mixed-line-ending` 替换或检查混合的行尾结束符。 - `--fix={auto,crlf,lf,no}` - `auto` - 自动替换出现最频繁的行尾结束符。这是默认参数。 - `crlf`, `lf` - 强制将行尾结束符分别替换为 CRLF 和 LF。 - 此选项与 Git 设置“签入时使用 LF,签出时使用 CRLF”(check-in LF check-out CRLF)不兼容,因为 Git 会在 hook 被调用之后进行 smudge 处理。 - `no` - 检查是否存在任何混合的行尾结束符,而不修改任何文件。 #### `name-tests-test` 验证测试文件是否命名正确。 - `--pytest`(默认):确保测试文件匹配 `.*_test\.py` - `--pytest-test-first`:确保测试文件匹配 `test_.*\.py` - `--django` / `--unittest`:确保测试文件匹配 `test.*\.py` #### `no-commit-to-branch` 保护特定分支免受直接提交。 - 使用 `args: [--branch, staging, --branch, main]` 来设置分支。 如果未设置 branch 参数,`main` 和 `master` 默认都会受到保护。 - `-b` / `--branch` 可以指定多次以保护多个分支。 - `-p` / `--pattern` 可用于保护与提供的正则表达式匹配的分支 (例如 `--pattern, release/.*`)。可以指定多次。 请注意,`no-commit-to-branch` 默认配置为 [`always_run`](https://pre-commit.com/#config-always_run)。 因此,它将忽略任何 [`files`](https://pre-commit.com/#config-files)、 [`exclude`](https://pre-commit.com/#config-exclude)、[`types`](https://pre-commit.com/#config-types) 或 [`exclude_types`](https://pre-commit.com/#config-exclude_types) 的设置。 设置 [`always_run: false`](https://pre-commit.com/#config-always_run) 即可根据这些 文件过滤条件跳过此 hook。注意事项:在此配置下,空提交(`git commit --allow-empty`)将始终被此 hook 允许。 #### `pretty-format-json` 检查你所有的 JSON 文件是否格式美观。“Pretty” 在这里意味着键已排序并进行了缩进。你可以通过 以下命令行选项进行配置: - `--autofix` - 自动格式化 JSON 文件 - `--indent ...` - 控制缩进(可以是一个代表空格数的数字,或是一个空白字符串)。默认为 2 个空格。 - `--no-ensure-ascii` 保留 Unicode 字符,而不是将其转换为转义序列 - `--no-sort-keys` - 自动修复时,保留原始的键顺序(而不是对键进行排序) - `--top-keys comma,separated,keys` - 需要保持在映射顶部的键。 #### `requirements-txt-fixer` 对 requirements.txt 和 constraints.txt 中的条目进行排序,并移除 `pkg-resources==0.0.0` 的错误条目 #### `sort-simple-yaml` 对仅由顶级键组成的简单 YAML 文件进行排序, 保留注释和块。 请注意,`sort-simple-yaml` 默认不匹配任何 `files`,因为它强制要求 一种非常特定的格式。你必须通过设置 [`files`](https://pre-commit.com/#config-files) 来选择启用它,例如: ``` - id: sort-simple-yaml files: ^config/simple/ ``` #### `trailing-whitespace` 修剪行尾空白字符。 - 要保留 Markdown [硬换行](https://github.github.com/gfm/#hard-line-break) 请使用 `args: [--markdown-linebreak-ext=md]`(或你的 Markdown 文件使用的其他扩展名)。如果出于某种原因你想将所有文件视为 Markdown,请使用 `--markdown-linebreak-ext=*`。 - 默认情况下,此 hook 会修剪行尾的所有空白字符。 要指定自定义的需要修剪的字符集,请使用 `args: [--chars,""]`。 ### 已废弃 / 被替换的 hooks - `check-byte-order-marker`:请改用 fix-byte-order-marker - `fix-encoding-pragma`:请改用 [`pyupgrade`](https://github.com/asottile/pyupgrade) - `check-docstring-first`:存在根本性缺陷,已废弃且无替代品。 ### 作为独立包 如果你想使用这些 hooks,它们也可作为独立包使用。 只需执行 `pip install pre-commit-hooks`
标签:DNS解析, Git, pre-commit, Python, SOC Prime, 代码审查, 开发工具, 开源项目, 无后门, 格式化, 网络安全研究, 网络调试, 自动化, 语法检查, 逆向工具, 钩子, 错误基检测, 静态代码分析