Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commitizen + husky 规范git提交信息 #40

Open
wohaofang opened this issue Apr 20, 2021 · 0 comments
Open

commitizen + husky 规范git提交信息 #40

wohaofang opened this issue Apr 20, 2021 · 0 comments
Assignees
Labels

Comments

@wohaofang
Copy link
Owner

背景

我们公司现在git提交的规范

但是程序校验能防止员工出错,所以添加如下校验

参考文章

安装

个人习惯针对于项目设置,所以直接在项目安装,而非全局配置,原理一样

npm i -D commitizen cz-conventional-changelog   
### package.json 

"config":{    "commitizen":{        "path":"node_modules/cz-conventional-changelog"    }}
--

commitlint校验

进行了上面的操作,其实对于一个自觉地人来说,已经够了,但是没有约束就代表了自由,自由就有人越界,我如果不按约束提交,照样玩的嗨起,那么怎么给这些自由加一些约束呢?

在项目更目录下建立配置文件 commitlint.config.js 或者 .commitlintrc.js

npm i -D @commitlint/config-conventional @commitlint/cli

module.exports = {
  extents: ['@commitlint/config-conventional'],
  rules: {
    'type-empty': [2, 'never'], // 提交不符合规范时,也可以提交,但是会有警告
    'subject-empty': [2, 'never'], // 提交不符合规范时,也可以提交,但是会有警告
    'subject-full-stop': [0, 'never'],
    'subject-case': [0, 'never'],
    'type-enum': [
      2,
      'always',
      [
        'build',
        'ci', // 与CI(持续集成服务)有关的改动
        'improvement',
        'perf', // 提高性能的改动
        'feat', // 新功能(feature)
        'fix', // 修补bug
        'docs', // 文档(documentation)
        'style', // 格式(不影响代码运行的变动)
        'refactor', // 重构(即不是新增功能,也不是修改bug的代码变动)
        'test', // 添加测试或者修改现有测试
        'revert', // 回滚
        'config', // 构建过程或辅助工具的变动
        'chore', // 其他改动
      ],
    ],
  },
}


配置后的效果

Husky限制

结合git hook来检验commit message,这样当你的提交不符合规范时就会阻止你提交

npm i -D husky

package.json

  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }

husky 还有很多自定义功能我这里没有使用,就不细写了

@wohaofang wohaofang self-assigned this Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant