目前最快的多语言 Git 钩子管理器
适用于 Node.js、Ruby 和许多其他类型项目的 Git 钩子管理器。
📖 阅读介绍文章
使用 Go (>= 1.21)
go install github.com/evilmartians/lefthook@latest
使用 NPM
npm install lefthook --save-dev
使用 Ruby
gem install lefthook
安装指南 提供了更多安装 lefthook 的方法: apt, brew, winget, 以及其他。
配置您的钩子,安装一次,然后就可以忘记它了:依赖底层的魔法。
# Configure your hooks
vim lefthook.yml
# Install them to the git project
lefthook install
# Enjoy your work with git
git add -A && git commit -m '...'
提高您的速度。 示例
pre-push:
parallel: true
pre-commit:
commands:
frontend-linter:
run: yarn eslint {staged_files}
backend-linter:
run: bundle exec rubocop --force-exclusion {all_files}
frontend-style:
files: git diff --name-only HEAD @{push}
run: yarn stylelint {files}
如果您想过滤文件列表。您可以在这里找到更多 glob 模式的例子。
pre-commit:
commands:
backend-linter:
glob: "*.rb" # glob filter
exclude: "application.rb|routes.rb" # regexp filter
run: bundle exec rubocop --force-exclusion {all_files}
如果您想在相对路径中执行命令
pre-commit:
commands:
backend-linter:
root: "api/" # Careful to have only trailing slash
glob: "*.rb" # glob filter
run: bundle exec rubocop {all_files}
如果单行命令不够用,您可以执行文件。 示例。
commit-msg:
scripts:
"template_checker":
runner: bash
如果您想控制一组命令。 示例。
pre-push:
commands:
packages-audit:
tags: frontend security
run: yarn audit
gems-audit:
tags: backend security
run: bundle audit
如果您在 Docker 环境中。 示例。
pre-commit:
scripts:
"good_job.js":
runner: docker run -it --rm <container_id_or_name> {cmd}
如果您是前端/后端开发人员,并且想要跳过不必要的命令或在 Docker 中覆盖某些内容。 描述。
# lefthook-local.yml
pre-push:
exclude_tags:
- frontend
commands:
packages-audit:
skip: true
如果您想直接运行钩子组。
$ lefthook run pre-commit
如果您想直接运行特定的命令组。
fixer:
commands:
ruby-fixer:
run: bundle exec rubocop --force-exclusion --safe-auto-correct {staged_files}
js-fixer:
run: yarn eslint --fix {staged_files}
$ lefthook run fixer
如果您不想看到支持信息
skip_output:
- meta #(version and which hook running)
- success #(output from runners with exit code 0)