多语言 Git 提交前检查框架
pre-commit 用 .pre-commit-config.yaml 管理 Git hooks,在提交前运行格式化、lint、类型检查等。每个 hook 在自己的隔离环境中运行,避免污染项目依赖。
pre-commit install
pre-commit install --hook-type commit-msg
pre-commit run --all-files
pre-commit run ruff --all-files
pre-commit autoupdate
pre-commit clean
pre-commit try-repo ../my-hooks --all-files
.pre-commit-config.yaml:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: ruff
args: [--fix]
--all-files。rev 不固定会让不同时间安装出不同行为;定期 autoupdate 并审查。git add 再提交。commit-msg 钩子要单独 --hook-type 安装;只装默认 hook 不会生效。lefthook — 另一种 Git hooks 管理器commitlint — 提交信息检查git — 提交操作