1、安装工具
# 需要nodejs
$ npm install -g @commitlint/cli @commitlint/config-conventional
如果已安装go环境,则执行下面两个命令:
$ export GO111MODULE="on"
$ go get -u github.com/git-hooks/git-hooks
如果没有安装go环境,则到这里下载对应系统版本,解压后,并添加执行文件路径到PATH中。了解git-hooks。
2、在项目中执行:
$ git hooks install
Install applypatch-msg
Install commit-msg
Install post-applypatch
Install post-checkout
Install post-commit
Install post-merge
Install post-receive
Install pre-applypatch
Install pre-auto-gc
Install pre-commit
Install prepare-commit-msg
Install pre-rebase
Install pre-receive
Install update
Install pre-push
查看已安装的hook:
$ git hooks
Git hooks ARE installed in this repository.
user hooks
pre-commit
- check.sh
- check2.sh
commit-msg
- commitlint.sh
Contrib hooks
3、git-hooks配置:
如果仅对本项目生效, 则在项目的.git同级目录下,新建githooks目录,创建对应的hook目录,然后在对应的hook目录下编写自己的脚本,并赋予执行权限。如果对本用户有效,则在$HOME
目录下创建.githooks目录。
commitlint.sh的内容为:
#!/usr/bin/env bash
commitlint -e "$@"
4、配置commitlint, 编辑~/.commitlintrc.js
,添加自己的规则。
/* fix:bug 修复
* docs:文档更新
* style:不影响程序逻辑的代码修改(修改空白字符,格式缩进,补全缺失的分号等,没>有改变代码逻辑)
* refactor:重构代码(既没有新增功能,也没有修复 bug)
* perf:性能, 体验优化
* test:新增测试用例或是更新现有测试
* build:主要目的是修改项目构建系统(例如 glup,webpack,rollup 的配置等)的提交
* ci:主要目的是修改项目继续集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交
* chore:不属于以上类型的其他类型,比如构建流程, 依赖管理
* revert:回滚某个更早之前的提交
*/
module.exports = {
extends: ['/usr/local/lib/node_modules/@commitlint/config-conventional'],
rules: {
'type-enum': [2, 'always', [
"feat", "fix", "docs", "style", "refactor", "test", "chore", "revert"
]],
'subject-full-stop': [0, 'never'],
'subject-case': [0, 'never']
}
};
5、在项目中添加一个不符合规范的commit,则会commit失败;符合规范的commit则可以成功。
➜ $ git commit -am "xxx: should fail"
⧗ input: xxx: should fail
✖ type must be one of [feat, fix, docs, style, refactor, test, chore, revert] [type-enum]
✖ found 1 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
exit status 1