TOC
在项目开发过程中,我们经常需要规范化代码配置流程,此时需要一种工具,开发人员在使用git过程中按照要求的规范进行提交代码和各种代码检测或其他附加处理逻辑。
git
钩子git
钩子通过设置钩子可以让开发人员在提交代码仓库的各个阶段进行一些自定义处理。钩子又分为git客户端钩子、git服务端钩子。例如git客户端在进行代码合并、提交的时候可以通过客户端钩子
进行拦截,先执行完钩子设置的逻辑后再进行真正的代码合并、提交逻辑。服务端钩子
可以在代码推送到仓库之后之后触发。
git
客户端钩子pre-commit
钩子在键入提交信息前运行。 它用于检查即将提交的快照。prepare-commit-msg
钩子在启动提交信息编辑器之前,默认信息被创建之后运行。commit-msg
钩子接收一个参数,此参数即上文提到的,存有当前提交信息的临时文件的路径。post-commit
钩子在整个提交过程完成后运行。pre-rebase
钩子运行于变基之前,以非零值退出可以中止变基的过程。post-rewrite
钩子被那些会替换提交记录的命令调用,比如 git commit --amend
和 git rebase
(不过不包括 git filter-branch
)。 pre-push
钩子会在 git push
运行期间, 更新了远程引用但尚未传送对象时被调用。git
服务端钩子服务端钩子需要在提供git
服务端进行配置。
pre-receive
处理来自客户端的推送操作时,最先被调用的脚本是 pre-receive
。update
脚本和 pre-receive
脚本十分类似,不同之处在于它会为每一个准备更新的分支各运行一次。post-receive
挂钩在整个过程完结以后运行,可以用来更新其他系统服务或者通知用户。lefthook
是由go
语言开发的适用于 Node.js
、Ruby
或任何其他类型项目的快速且强大的 Git
钩子管理器。 代码仓库地址
Go
编写的。可以并行运行命令。pre-push
挂钩上更改的文件。# On `git push` lefthook will run spelling and links check for all of the changed files
pre-push:
parallel: true
commands:
spelling:
files: git diff --name-only HEAD @{push}
glob: "*.md"
run: npx yaspeller {files}
check-links:
files: git diff --name-only HEAD @{push}
glob: "*.md"
run: npx markdown-link-check {files}
pre-push
常用的一些例子
pre-push:
parallel: true
commands:
danger:
run: bundle exec rake danger_local
eslint:
tags: frontend style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '*.{js,vue}'
run: yarn run lint:eslint {files}
haml-lint:
tags: view haml style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '*.html.haml'
run: REVEAL_RUBOCOP_TODO=0 bundle exec haml-lint --config .haml-lint.yml {files}
markdownlint:
tags: documentation style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: 'doc/*.md'
run: yarn markdownlint {files}
yamllint:
tags: backend style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '*.{yml,yaml}'
run: scripts/lint-yaml.sh {files}
stylelint:
tags: stylesheet css style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '*.scss{,.css}'
run: yarn stylelint {files}
prettier:
tags: frontend style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '*.{js,vue,graphql}'
run: yarn run prettier --check {files}
rubocop:
tags: backend style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '*.{rb,rake}'
run: REVEAL_RUBOCOP_TODO=0 bundle exec rubocop --parallel --force-exclusion {files}
graphql_docs:
tags: documentation
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: '{app/graphql/**/*.rb,ee/app/graphql/**/*.rb}'
run: bundle exec rake gitlab:graphql:check_docs
vale: # Requires Vale: https://docs.gitlab.com/ee/development/documentation/testing.html#install-linters
tags: documentation style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: 'doc/*.md'
run: 'if [ $VALE_WARNINGS ]; then minWarnings=warning; else minWarnings=error; fi; if command -v vale > /dev/null 2>&1; then if ! vale --config .vale.ini --minAlertLevel $minWarnings {files}; then echo "ERROR: Fix any linting errors and make sure you are using the latest version of Vale."; exit 1; fi; else echo "ERROR: Vale not found. For more information, see https://docs.errata.ai/vale/install."; exit 1; fi'
gettext:
skip: true # This is disabled by default. You can enable this check by adding skip: false in lefhook-local.yml https://github.com/evilmartians/lefthook/blob/master/docs/full_guide.md#skipping-commands
tags: backend frontend view haml
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD | while read file;do git diff --unified=1 $(git merge-base origin/master HEAD)..HEAD $file | grep -Fqe '_(' && echo $file;done; true
glob: "*.{haml,rb,js,vue}"
run: bin/rake gettext:updated_check
docs-metadata: # See https://docs.gitlab.com/ee/development/documentation/#metadata
tags: documentation style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: 'doc/*.md'
run: scripts/lint-docs-metadata.sh {files}
docs-trailing_spaces: # Not enforced in CI/CD pipelines, but reduces the amount of required cleanup: https://gitlab.com/gitlab-org/technical-writing/-/blob/main/.gitlab/issue_templates/tw-monthly-tasks.md#remote-tasks
tags: documentation style
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: 'doc/*.md'
run: yarn markdownlint:no-trailing-spaces {files}
docs-deprecations:
tags: documentation
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: 'data/deprecations/*.yml'
run: echo "Changes to deprecation files detected. Checking deprecations..\n"; bundle exec rake gitlab:docs:check_deprecations
docs-removals:
tags: documentation
files: git diff --name-only --diff-filter=d $(git merge-base origin/master HEAD)..HEAD
glob: 'data/removals/*.yml'
run: echo "Changes to removals files detected. Checking removals..\n"; bundle exec rake gitlab:docs:check_removals
自定义例子,通过定义lefthook.yml
配置进行自定义hooks
注册。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有