git commit-msg
可以用来规范标准格式,并且可以按需要指定是否要拒绝本次提交
git pre-commit
会在提交前被调用,并且可以按需要指定是否要拒绝本次提交
使用husky + commitlint 检查提交代码
commitlint:用于检查提交信息
husky 是gits hooks工具
commitlint
1.安装依赖
npm install --save-dev @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4
2.创建 commitlint.config.js文件
echo "module.exports = {extends:['@commitlint/config-conventional']}" > commitlint.config.js
3.打开 commitlint.config.js 增加配置项
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]
}
}
husky
1.安装 依赖
npm install husky@7.0.1 --save-dev
2启动hooks,生成 .husky文件
npx husky install
3.在package.json中生成prepare指令
npm set-script prepare “husky install”
4.执行 prepare指令
npm run prepare
5.添加 commitlint的hook到husky中,并指令在 commit-msg的hooks下执行 npx --no-install commitlint --edit “$1”
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1" '