大家好,我是前端小贺,心愿可能通过本人的学习输入给你带来帮忙。
背景
在一个残缺的工作流中,Git提交标准是肯定须要的。Git提交标准包含什么呢?
我感觉应该包含代码的质检以及commit message校验。
咱们应用husky来增加咱们上述的需要:
husky是利用Git hooks
(上面有常见的Git hooks
)在git
的钩子函数中(比方pre-commit
、commit-msg
)做一些事件。上面是官网的介绍:
Modern native Git hooks made easy
申明:咱们应用的husky的版本是7.0
及之后的版本
装置husky
好了,关上我的项目的根目录,执行`npm install husky --save-dev
,前提是生成了package.json
npm install husky --save-dev
咱们依照官网的文档,进行husky install
:
留神点:这里的npm
的版本必须为7.1
及之后的版本(https://github.com/npm/cli/re...)
npm set-script prepare "husky install"npm run prepare
增加eslint hook
咱们来配置eslint
校验
npx husky add .husky/pre-commit "npm run lint"git add .husky/pre-commit
上述咱们为什么可能配置npm run lint
呢,是因为咱们的package.json
文件中的script
中蕴含了lint
:
{ "scripts": { "lint": "vue-cli-service lint" }}
ok,至此咱们在commit
的时候就会校验代码了,咱们试一下:
git commit -m"验证"> webpack-vue@2.1.0 lint> vue-cli-service lint DONE No lint errors found!
如上,咱们提交的时候执行了lint
脚本。
增加commit-msg hook
接下来,咱们限度下commit messge标准。
咱们须要采纳npm社区中比拟成熟的两个依赖包@commitlint/cli
,@commitlint/config-conventional
。
- @commitlint/cli是用来在命令行中提醒用户信息的
- @commitlint/config-conventional是
commit message
标准
接下来咱们装置下两个包,并增加相干的配置文件:
npm install @commitlint/cli @commitlint/config-conventional --save-devecho "module.exports = { extends: ['@commitlint/config-conventional'] }" > commitlint.config.js
ok,至此咱们能够校验commit meassage
中的内容了,咱们测试一下:
git commit -m"1"> webpack-vue@2.1.0 lint> vue-cli-service lint --mode productionThe following files have been auto-fixed: src/utils/secret.js DONE All lint errors auto-fixed.npm ERR! cancelednpm ERR! A complete log of this run can be found in:npm ERR! /Users/hsm/.npm/_logs/2022-04-26T02_21_07_564Z-debug.loghusky - commit-msg hook exited with code 1 (error)
如上,咱们 commit message
轻易写了点货色,就校验住了。咱们批改下咱们的commit message
从新commit
下:
git commit -m"docs: 增加husky相干"> webpack-vue@2.1.0 lint> vue-cli-service lint --mode production DONE No lint errors found![committest b61be77] docs: 增加husky相干 1 file changed, 2 insertions(+)
Ok,胜利了~
提交标准
对于git commit message
提交标准,咱们能够参考阮一峰的Commit message 和 Change log 编写指南。
在VSCode
中,咱们采纳了Conventional Commits
插件来进行commit提交
Git hooks
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 执行前 |
最初
您的每一个点赞及评论都是对我保持写作最大的反对!
另外心愿各位朋友和我交换探讨,如有不对的中央,更心愿批评指正!
我是前端小贺,心愿可能通过本人的学习输入给你带来帮忙。