前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >git钩子和lefthook

git钩子和lefthook

原创
作者头像
码农小辉
发布2022-09-06 15:51:41
1K0
发布2022-09-06 15:51:41
举报
文章被收录于专栏:用户1381554的专栏

TOC

git钩子和lefthook

背景

在项目开发过程中,我们经常需要规范化代码配置流程,此时需要一种工具,开发人员在使用git过程中按照要求的规范进行提交代码和各种代码检测或其他附加处理逻辑。

git钩子

什么是git钩子

通过设置钩子可以让开发人员在提交代码仓库的各个阶段进行一些自定义处理。钩子又分为git客户端钩子、git服务端钩子。例如git客户端在进行代码合并、提交的时候可以通过客户端钩子进行拦截,先执行完钩子设置的逻辑后再进行真正的代码合并、提交逻辑。服务端钩子可以在代码推送到仓库之后之后触发。

常见的git客户端钩子

  • pre-commit 钩子在键入提交信息前运行。 它用于检查即将提交的快照。
  • prepare-commit-msg 钩子在启动提交信息编辑器之前,默认信息被创建之后运行。
  • commit-msg 钩子接收一个参数,此参数即上文提到的,存有当前提交信息的临时文件的路径。
  • post-commit 钩子在整个提交过程完成后运行。
  • pre-rebase 钩子运行于变基之前,以非零值退出可以中止变基的过程。
  • post-rewrite 钩子被那些会替换提交记录的命令调用,比如 git commit --amendgit rebase(不过不包括 git filter-branch)。
  • pre-push 钩子会在 git push 运行期间, 更新了远程引用但尚未传送对象时被调用。

常见的git服务端钩子

服务端钩子需要在提供git服务端进行配置。

  • pre-receive处理来自客户端的推送操作时,最先被调用的脚本是 pre-receive
  • update 脚本和 pre-receive 脚本十分类似,不同之处在于它会为每一个准备更新的分支各运行一次。
  • post-receive 挂钩在整个过程完结以后运行,可以用来更新其他系统服务或者通知用户。

lefthook

项目地址

lefthook是由go语言开发的适用于 Node.jsRuby 或任何其他类型项目的快速且强大的 Git 钩子管理器。 代码仓库地址

  • 快速地。它是用 Go 编写的。可以并行运行命令。
  • 强大的。通过配置中的几行,您可以仅检查pre-push挂钩上更改的文件。
  • 简单的。它是一个无依赖的二进制文件,可以在任何环境中工作。

依赖关系

例子

代码语言:yaml
复制
# 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常用的一些例子

代码语言:yaml
复制
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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • git钩子和lefthook
    • 背景
      • git钩子
        • 什么是git钩子
        • 常见的git客户端钩子
        • 常见的git服务端钩子
      • lefthook
        • 项目地址
        • 依赖关系
        • 例子
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档