使用Git Hooks规范化
先来明确一下最终要实现的效果:
当《提交描述信息》不符合 约定式提交规范 的时候,阻止当前的提交,并抛出对应的错误提示
而要实现这个目的,就需要先来了解一个概念,叫做 Git hooks(git 钩子 || git 回调方法)
也就是:git
在执行某个事件之前或之后进行一些其他额外的操作
而所期望的 阻止不合规的提交消息,那么就需要使用到 hooks
的钩子函数。
下面是整理出来的所有的 hooks
,大家可以进行一下参考,其中加粗的是常用到的 hooks
:
Git Hook | 调用时机 | 说明 |
---|---|---|
pre-applypatch | git am 执行前 | |
applypatch-msg | git am 执行前 | |
post-applypatch | git am 执行后 | 不影响git am 的结果 |
pre-commit | git commit 执行前 | 可以用git commit --no-verify 绕过 |
commit-msg | git commit 执行前 | 可以用git commit --no-verify 绕过 |
post-commit | git commit 执行后 | 不影响git commit 的结果 |
pre-merge-commit | git merge 执行前 | 可以用git merge --no-verify 绕过。 |
prepare-commit-msg | git commit 执行后,编辑器打开之前 | |
pre-rebase | git rebase 执行前 | |
post-checkout | git checkout 或git switch 执行后 | 如果不使用--no-checkout 参数,则在git clone 之后也会执行。 |
post-merge | git commit 执行后 | 在执行git pull 时也会被调用 |
pre-push | git push 执行前 | |
pre-receive | git-receive-pack 执行前 | |
update | ||
post-receive | git-receive-pack 执行后 | 不影响git-receive-pack 的结果 |
post-update | 当 git-receive-pack 对 git push 作出反应并更新仓库中的引用时 | |
push-to-checkout | 当``git-receive-pack对 git push做出反应并更新仓库中的引用时,以及当推送试图更新当前被签出的分支且 receive.denyCurrentBranch配置被设置为 updateInstead`时 | |
pre-auto-gc | git gc --auto 执行前 | |
post-rewrite | 执行git commit --amend 或git rebase 时 | |
sendemail-validate | git send-email 执行前 | |
fsmonitor-watchman | 配置core.fsmonitor 被设置为.git/hooks/fsmonitor-watchman 或.git/hooks/fsmonitor-watchmanv2 时 | |
p4-pre-submit | git-p4 submit 执行前 | 可以用git-p4 submit --no-verify 绕过 |
p4-prepare-changelist | git-p4 submit 执行后,编辑器启动前 | 可以用git-p4 submit --no-verify 绕过 |
p4-changelist | git-p4 submit 执行并编辑完changelist message 后 | 可以用git-p4 submit --no-verify 绕过 |
p4-post-changelist | git-p4 submit 执行后 | |
post-index-change | 索引被写入到read-cache.c do_write_locked_index 后 |
PS:详细的 HOOKS介绍
可点击这里查看
整体的 hooks
非常多,当时其中用的比较多的其实只有两个:
Git Hook | 调用时机 | 说明 |
---|---|---|
pre-commit | git commit 执行前它不接受任何参数,并且在获取提交日志消息并进行提交之前被调用。脚本 git commit 以非零状态退出会导致命令在创建提交之前中止。 | 可以用git commit --no-verify 绕过 |
commit-msg | git commit 执行前可用于将消息规范化为某种项目标准格式。 还可用于在检查消息文件后拒绝提交。 | 可以用git commit --no-verify 绕过 |
简单来说这两个钩子:
commit-msg
:可以用来规范化标准格式,并且可以按需指定是否要拒绝本次提交pre-commit
:会在提交前被调用,并且可以按需指定是否要拒绝本次提交
而接下来关键,就在这两个钩子上面。
使用 husky + commitlint 检查提交描述是否符合规范要求
了解了 git hooks
的概念,使用 git hooks
来去校验提交信息。
要完成这么个目标,那么需要使用两个工具:
-
commitlint:用于检查提交信息
-
husky:是
git hooks
工具
注意:npm
需要在 7.x 以上版本!!!!!
commitlint
-
安装依赖:
npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
-
创建
commitlint.config.js
文件echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
-
打开
commitlint.config.js
, 增加配置项( config-conventional 默认配置点击可查看 ):module.exports = { // 继承的规则 extends: ['@commitlint/config-conventional'], // 定义规则类型 rules: { // type 类型定义,表示 git 提交的 type 必须在以下类型范围内 'type-enum': [ 2, 'always', [ 'feat', // 新功能 feature 'fix', // 修复 bug 'docs', // 文档注释 'style', // 代码格式(不影响代码运行的变动) 'refactor', // 重构(既不增加新功能,也不是修复bug) 'perf', // 性能优化 'test', // 增加测试 'chore', // 构建过程或辅助工具的变动 'revert', // 回退 'build' // 打包 ] ], // subject 大小写不做校验 'subject-case': [0] } }
注意:确保保存为 UTF-8
的编码格式,否则可能会出现以下错误:
husky
-
安装依赖:
npm install husky@7.0.1 --save-dev
-
启动
hooks
, 生成.husky
文件夹npx husky install
-
在
package.json
中生成prepare
指令( 需要 npm > 7.0 版本 )npm set-script prepare "husky install"
-
执行
prepare
指令npm run prepare
-
执行成功,提示
-
添加
commitlint
的hook
到husky
中,并指令在commit-msg
的hooks
下执行npx --no-install commitlint --edit "$1"
指令npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
-
此时的
.husky
的文件结构
不符合规范的 commit 将不再可提交:
PS F:\xxxxxxxxxxxxxxxxxxxxx\imooc-admin> git commit -m "测试"
⧗ input: 测试
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
husky - commit-msg hook exited with code 1 (error)
就已经可以处理好了 强制规范化的提交要求,到现在 不符合规范的提交信息,将不可在被提交!
pre-commit 检测提交时代码规范
通过 husky
监测 pre-commit
钩子,在该钩子下执行 npx eslint --ext .js,.vue src
指令来去进行相关检测:
-
执行
npx husky add .husky/pre-commit "npx eslint --ext .js,.vue src"
添加commit
时的hook
(npx eslint --ext .js,.vue src
会在执行到该 hook 时运行) -
该操作会生成对应文件
pre-commit
:
-
关闭
VSCode
的自动保存操作 -
修改一处代码,使其不符合
ESLint
校验规则 -
执行 提交操作 会发现,抛出一系列的错误,代码无法提交
PS F:\xxxxxxxxxxxxxxxxxxx\imooc-admin> git commit -m 'test' F:\xxxxxxxxxxxxxxxx\imooc-admin\src\views\Home.vue 13:9 error Strings must use singlequote quotes ✖ 1 problem (1 error, 0 warnings) 1 error and 0 warnings potentially fixable with the `--fix` option. husky - pre-commit hook exited with code 1 (error)
-
想要提交代码,必须处理完成所有的错误信息
通过 pre-commit
检测到了代码的提交规范问题。
lint-staged 自动修复格式错误
lint-staged 可以让你当前的代码检查 只检查本次修改更新的代码,并在出现错误的时候,自动修复并且推送
lint-staged 无需单独安装,生成项目时,vue-cli
已经帮助安装过了,所以我们直接使用就可以了
-
修改
package.json
配置"lint-staged": { "src/**/*.{js,vue}": [ "eslint --fix", "git add" ] }
-
如上配置,每次它只会在你本地
commit
之前,校验你提交的内容是否符合你本地配置的eslint
规则(这个见文档 ESLint ),校验会出现两种结果:- 如果符合规则:则会提交成功。
- 如果不符合规则:它会自动执行
eslint --fix
尝试帮你自动修复,如果修复成功则会帮你把修复好的代码提交,如果失败,则会提示你错误,让你修好这个错误之后才能允许你提交代码。
-
修改
.husky/pre-commit
文件#!/bin/sh . "$(dirname "$0")/_/husky.sh" npx lint-staged
-
再次执行提交代码
-
发现 暂存区中 不符合
ESlint
的内容,被自动修复
总结
代码格式规范:
对于 代码格式规范 而言,可以通过 ESLint
+ Prettier
+ VSCode 配置
配合进行了处理。
最终达到了在保存代码时,自动规范化代码格式的目的。
git
提交规范:
对于 git
提交规范 而言我们使用了 husky
来监测 Git hooks
钩子,并且通过以下插件完成了对应的配置:
- 约定式提交规范
- commitizen:git 提交规范化工具
- commitlint:用于检查提交信息
pre-commit
:git hooks
钩子- lint-staged:只检查本次修改更新的代码,并在出现错误的时候,自动修复并且推送
评论区