说明: 项目中需要约定提交信息规范,可使用husky和commitlint,对git的commit信息进行校验。
安装
yarn add husky --dev
yarn add pinst --dev # ONLY if your package is not private
开启Git Hooks
yarn husky install
要在安装后自动启用 Git Hooks,请编辑 package.json
// package.json
{
"private": true, // ← your package is private, you only need postinstall
"scripts": {
"postinstall": "husky install"
}
}
postinstall会在你yarn安装时自动执行
Hooks
# 命令创建hooks
npx husky add .husky/[gitHooks] [content]
commit-msg
# $1 .git/COMMIT_EDITMSG
npx husky add .husky/commit-msg npx --no-install commitlint --edit $1
创建commitlint.config.js 对commit校验
module.exports = { extends: ['@commitlint/config-conventional'] }
可扩展自定义规则,@commitlint/config-conventional默认的规则如下:
rules: {
'body-leading-blank': [1, 'always'],
'body-max-line-length': [2, 'always', 100],
'footer-leading-blank': [1, 'always'],
'footer-max-line-length': [2, 'always', 100],
'header-max-length': [2, 'always', 100],
'subject-case': [
2,
'never',
['sentence-case', 'start-case', 'pascal-case', 'upper-case'],
],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'type-enum': [
2,
'always',
[
'build',
'chore',
'ci',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test',
],
],
},
测试
git commit s