# pre-commit-hooks **Repository Path**: litebmc/pre-commit-hooks ## Basic Information - **Project Name**: pre-commit-hooks - **Description**: litebmc 所有代码仓的统一 pre-commit hooks 仓库。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2026-06-04 - **Last Updated**: 2026-06-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # litebmc pre-commit-hooks litebmc 所有代码仓的统一 pre-commit hooks 仓库。 ## 快速开始 ```bash # 1. 安装 pre-commit 框架(由 lbkit 统一提供) pip3 install lbkit # 2. 在项目根目录创建 .pre-commit-config.yaml cat > .pre-commit-config.yaml << 'EOF' --- repos: - repo: https://gitee.com/litebmc/pre-commit-hooks.git rev: master hooks: - id: check-merge-conflict - id: check-trailing-whitespace - id: check-end-of-file - id: check-mixed-line-ending - id: check-large-files - id: check-yaml - id: check-json - id: check-no-commit-to-branch - id: check-conventional-commit # --- 选择项目需要的 hook --- # - id: check-debug-statements # - id: clang-format # - id: black # - id: isort # - id: flake8 # - id: add-signoff-and-change-id EOF # 3. 安装 git hooks pre-commit install pre-commit install -t commit-msg # 4. 完成!后续每次 git commit 自动触发检查 ``` > **依赖说明**:所有 hook 依赖(clang-format、black、isort、flake8、pyyaml 等)均由 pre-commit 通过 `additional_dependencies` 自动安装到隔离的虚拟环境中,**无需手动安装任何系统级工具或 Python 包**。首次运行时会自动下载依赖,后续命中缓存。 ## Hook 列表 ### 通用文件检查 | Hook | 说明 | 依赖 | |------|------|------| | `check-merge-conflict` | 禁止提交含合并冲突标记的文件 | 无(bash) | | `check-trailing-whitespace` | 检查行尾空白符 | 无(bash) | | `check-end-of-file` | 文件以换行符结尾 | 无(bash) | | `check-mixed-line-ending` | 统一 LF 换行 | 无(bash) | | `check-large-files` | 禁止提交 >500KB 文件 | 无(bash) | | `check-no-commit-to-branch` | 禁止直推 master/main | 无(bash) | | `check-debug-statements` | Python 残留调试语句 | 无(bash) | | `check-yaml` | YAML 语法校验 | pyyaml(自动安装) | | `check-json` | JSON 语法校验 | python3 | ### 代码格式化 | Hook | 说明 | 依赖 | |------|------|------| | `clang-format` | C/C++ 代码格式化 | clang-format(自动安装) | | `black` | Python 代码格式化 | black(自动安装) | | `isort` | Python import 排序 | isort(自动安装) | | `flake8` | Python 代码风格 | flake8 + bugbear + comprehensions(自动安装) | ### 提交规范 | Hook | 说明 | 依赖 | |------|------|------| | `check-conventional-commit` | Conventional Commits 校验 | python3 | | `add-signoff-and-change-id` | 自动追加 Signed-off-by 和 Change-Id | python3 | ## 各仓库配置示例 ### C/C++ 项目(lb_base) ```yaml # .pre-commit-config.yaml repos: - repo: https://gitee.com/litebmc/pre-commit-hooks rev: v1.0.0 hooks: - id: check-merge-conflict - id: check-trailing-whitespace - id: check-end-of-file - id: check-mixed-line-ending - id: check-large-files - id: check-yaml - id: check-json - id: check-no-commit-to-branch - id: clang-format - id: black - id: check-conventional-commit ``` ### Python 项目(lbkit) ```yaml # .pre-commit-config.yaml repos: - repo: https://gitee.com/litebmc/pre-commit-hooks rev: v1.0.0 hooks: - id: check-merge-conflict - id: check-debug-statements - id: check-trailing-whitespace - id: check-end-of-file - id: check-mixed-line-ending - id: check-large-files - id: check-yaml - id: check-json - id: check-no-commit-to-branch - id: black - id: isort - id: flake8 - id: check-conventional-commit ``` ## 常见操作 ```bash # 手动检查所有文件 pre-commit run --all-files # 只运行指定 hook pre-commit run clang-format --all-files # 跳过所有 hooks git commit --no-verify -m "wip" # 更新 hooks 到最新版本 pre-commit autoupdate # 卸载 hooks pre-commit uninstall pre-commit uninstall -t commit-msg # 清理缓存(hooks 定义变更后) pre-commit clean ```