规范代码提交commitlint与git-hooks

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值