husky和commitlint的使用

说明: 项目中需要约定提交信息规范,可使用 huskycommitlint,对 gitcommit 信息进行校验。
该插件针对,changelog 有很大的用处,因为生成 changelog 需要 commit 的规则规范,huskycommitlint 可以很好的配合

安装

1
2
yarn add husky --dev
yarn add pinst --dev # ONLY if your package is not private

开启 Git Hooks

1
yarn husky install

要在安装后自动启用 Git Hooks,请编辑 package.json

1
2
3
4
5
6
{
"private": true, // ← your package is private, you only need postinstall
"scripts": {
"postinstall": "husky install"
}
}

postinstall 会在你 yarn 安装时自动执行

Hooks

命令创建 hooks

1
npx husky add .husky/[gitHooks]  [content]

commit-msg

1
2
# $1 .git/COMMIT_EDITMSG
npx husky add .husky/commit-msg npx --no-install commitlint --edit $1

创建 commitlint.config.jscommit 校验

1
module.exports = { extends: ['@commitlint/config-conventional'] };

可扩展自定义规则,默认的规则如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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',
],
],
},

测试

1
git commit s
作者

wuxunyu

发布于

2022-03-13

更新于

2022-07-04

许可协议