要求对git
的message
作限制,要求要以html
['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'revert', 'types']
:vue
作开头标记git
git
存在hook
,在每一个步骤后面都会执行对应的🪝github
所以咱们能够考虑经过git hook
去完成这项校验npm
实际上,在平时的开发过程当中,就已经有于此相关的内容了,就是不知道同窗们留意到没有json
好比在Vue
项目中,若是在建立项目中,选择了eslint
,并选择了保存并格式化bash
在进行git commit
时,代码会自动作一次格式化。markdown
究其缘由,其实是来自Vue
的默认配置app
那么Vue
是怎么作的,实际上,翻开文档,咱们可以找到相关内容ide
其实是尤🌧️溪 fork 了 husky
,稍作改造造成了yorkie
可是咱们能够看到,yorkie
已经好久没有更新了,而且没有文档,想要直接使用yorkie
是比较困难的
所以咱们回到最初的库,husky
husky
自己也是一个久负盛名的库,专一于git hook
那么就让咱们来集成它,完成咱们的需求
npx husky-init && npm install # npm
npx husky-init && yarn # Yarn 1
yarn dlx husky-init --yarn2 && yarn # Yarn 2
复制代码
集成之后,项目依赖会添加husky
,且项目根目录下会添加.husky
文件夹
.husky
文件夹下,默认添加了一个pre-commit
的hook
文件
咱们能够先将Vue
的默认配置转移进去
pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm run lint
git add .
复制代码
而后移除package.json
中的gitHooks
属性
npx husky add [fileName]
# 若是没有其余config上的变更,能够理解为照着以下写法写
# npx husky add .husky/hooks名称
复制代码
举个🌰
npx husky add .husky/commit-msg
复制代码
可能有些旁友不知道有哪些hook能够添加,我这里教你们怎么看
直接查看.git
文件夹中hooks内容
commit-msg
npx husky add .husky/commit-msg
复制代码
commit-msg
内容:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
MSG=`awk '{printf("%s",$0)}' $1`
echo $MSG
if [[ $MSG =~ ^(feat|fix|docs|style|refactor|perf|test|build|ci|revert|types):.*$ ]]
then
echo '成功'
else
echo '失败'
fi
复制代码
由于上述行为的hook是在项目中作的
换句话说是在开发成员的本地电脑上作的
一旦开发人员不爽,把hook删了,那就无从约束
因此更保险的方式,应该是由git平台上去作检测
能够在hook文件的最后添加
exit 1
复制代码
会停止本次git行为
commitlint
,见名知意,commit内容的lint
咱们能够使用其中的风格和工具作拓展
好比咱们这里选择集成以下工具和风格
@commitlint/cli
@commitlint/config-conventional
复制代码
若是须要达成上述需求,咱们能够这么作
commitlint.config.js
文件module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'revert', 'types'],
],
},
}
复制代码
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx commitlint --edit $1
复制代码