|
| 1 | +# ============================================================================= |
| 2 | +# coding-proxy: Pre-commit Hooks Configuration |
| 3 | +# ============================================================================= |
| 4 | +# 目的: 在代码提交前自动执行 Ruff lint/format 及通用卫生检查,作为 CI 的前置守护 |
| 5 | +# |
| 6 | +# 设计决策:使用 local hook(language: system + uv run)而非 astral-sh/ruff-pre-commit |
| 7 | +# - 遵循 Single Source of Truth 原则:Ruff 版本由 uv.lock 唯一管控,消除版本漂移 |
| 8 | +# - 与 CI 命令形态完全镜像(uv run ruff check/format) |
| 9 | +# - 符合 AGENTS.md 包管理规范(统一使用 uv) |
| 10 | +# |
| 11 | +# 安装:uv run pre-commit install |
| 12 | +# 手动全量检查:uv run pre-commit run --all-files |
| 13 | +# ============================================================================= |
| 14 | + |
| 15 | +minimum_pre_commit_version: "4.0.0" |
| 16 | + |
| 17 | +repos: |
| 18 | + # --------------------------------------------------------------------------- |
| 19 | + # 通用代码卫生检查(pre-commit-hooks 官方钩子集) |
| 20 | + # 轻量级、无额外依赖、执行极快 |
| 21 | + # --------------------------------------------------------------------------- |
| 22 | + - repo: https://github.com/pre-commit/pre-commit-hooks |
| 23 | + rev: v5.0.0 |
| 24 | + hooks: |
| 25 | + - id: trailing-whitespace # 去除行尾空白 |
| 26 | + - id: end-of-file-fixer # 确保文件以换行符结尾 |
| 27 | + - id: check-yaml |
| 28 | + args: ["--unsafe"] # 兼容 GitHub Actions ${{ }} 表达式语法 |
| 29 | + - id: check-toml # 校验 TOML 语法(pyproject.toml) |
| 30 | + - id: check-merge-conflict # 阻止提交含未解决 merge conflict 标记的文件 |
| 31 | + - id: check-added-large-files |
| 32 | + args: ["--maxkb=1024"] # 阈值 1MB(assets/dashboard-v0.2.3.png 为 944KB) |
| 33 | + |
| 34 | + # --------------------------------------------------------------------------- |
| 35 | + # Ruff: local hook,直接调用 uv 安装的 Ruff(与 CI 版本完全一致) |
| 36 | + # 前置条件:uv sync --dev 已执行 |
| 37 | + # --------------------------------------------------------------------------- |
| 38 | + - repo: local |
| 39 | + hooks: |
| 40 | + # 对应 CI: uv run ruff check . |
| 41 | + # --fix: 自动修复安全可修复问题(isort、UP 现代化等),降低开发摩擦 |
| 42 | + # --exit-non-zero-on-fix: 修复后返回非零码,触发 git 重新暂存已修复文件 |
| 43 | + - id: ruff-lint |
| 44 | + name: Ruff (lint + auto-fix) |
| 45 | + language: system |
| 46 | + entry: uv run ruff check --fix --exit-non-zero-on-fix |
| 47 | + types_or: [python, pyi] |
| 48 | + require_serial: false |
| 49 | + |
| 50 | + # 对应 CI: uv run ruff format --check . |
| 51 | + # 本地直接 format(自动修复格式),CI 保留 --check 模式作为最终防线 |
| 52 | + - id: ruff-format |
| 53 | + name: Ruff (format) |
| 54 | + language: system |
| 55 | + entry: uv run ruff format |
| 56 | + types_or: [python, pyi] |
| 57 | + require_serial: false |
0 commit comments