# pre-commit配置,首次使用需执行以下命令确保生效,可在代码提交前拦截并检查
# pip install pre-commit # 安装pre-commit工具
# pre-commit install # 安装git hook,请在代码仓根目录执行
repos:
# ===================== 通用基础钩子(所有语言) =====================
# pre-commit官方检查工具
#- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://gitcode.com/gh_mirrors/pr/pre-commit-hooks # 国内网络可使用gitcode镜像仓加速下载
rev: v6.0.0
hooks:
- id: trailing-whitespace # 移除行尾空白字符
- id: end-of-file-fixer # 确保文件以换行符结尾
- id: check-yaml # 校验 YAML 文件语法
- id: check-json # 校验 JSON 文件语法
- id: check-xml # 校验 XML 文件语法
- id: check-merge-conflict # 检查未解决的合并冲突标记
- id: check-added-large-files # 拦截超大文件提交(默认500KB)
# args: ["--maxkb", "500"] # 支持自定义修改
- id: detect-private-key # 检测私钥/敏感信息泄露
# detect-secrets:由 Yelp 开发,用于 在 Git 仓库中检测敏感信息,可作为gitleaks的备选
#- repo: https://github.com/Yelp/detect-secrets
- repo: https://gitcode.com/gh_mirrors/de/detect-secrets # 使用gitcode镜像加速
rev: v1.5.0
hooks:
- id: detect-secrets
# ===================== C/C++ 语言 =====================
# clang-format: C/C++最主流的代码格式化工具
- repo: https://gitcode.com/lyontang/mirrors-clang-format # 源:https://github.com/pre-commit/mirrors-clang-format,使用gitcode镜像仓加速下载
rev: v20.1.7
hooks:
- id: clang-format # C/C++ 代码格式化
files: ^src/.*\.(c|cpp|cc|cxx|h|hpp|hxx)$ # 只扫描src目录下的特定类型文件
types_or: [ c, c++ ] # 参考:https://pre-commit.com/#filtering-files-with-types
args: [ "--style=File", "--verbose" ] # 主流风格:LLVM/Google/File(读取根目录.clang-format配置)
# clang-tidy:静态分析工具
- repo: https://gitcode.com/lyontang/cpp-linter-hooks # 源:https://github.com/cpp-linter/cpp-linter-hooks,使用gitcode镜像仓加速下载
rev: v1.1.3
hooks:
- id: clang-tidy
files: ^src/.*\.(c|cpp|cc|cxx|h|hpp|hxx)$ # 只扫描src目录下的特定类型文件
args: ["--checks=.clang-tidy", "--version=21", "--p=build"]
# --checks=.clang-tidy 表示读取根目录的.clang-tidy配置,没有则使用默认规则检查
# --p=build 读取build目录下的compile_commands.json