Skip to content

commitlint

commitlint 是一个 git commit 信息校验工具。 使用 commitlint 来校验提交时 commit message 是否符合 conventional commit 规范。

安装依赖:

bash
ni -D @commitlint/{cli,config-conventional}

yarn add -D @commitlint/cli @commitlint/config-conventional

生成配置文件:

bash
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

或者 在根目录下添加 commitlint.config.js.commitlintrc.js 文件:

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

.husky 下新增配置文件 commit-msg:

bash
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1

验证是否生效:

bash
npx commitlint --from HEAD~1 --to HEAD --verbose

如果遇到报 ES Module 相关错误,将 commitlint.config.js文件后缀改为 cjs 即可。

提示

注意: 冒号是英文冒号 :, 冒号前边是type, 必须是[build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]这些中的一个, 然后冒号后边空一格在写上本次提交的具体message内容

commitlint.config.js

js
// feat:新增功能 | New features
// fix: Bug 修复 | Fix Bug
// docs:文档更新 | Document update
// style:不影响程序逻辑的代码修改(修改空白字符,格式缩进,补全缺失的分号等,没有改变代码逻辑) | Code modification that does not affect the program logic (modify the blank characters, format indent, complete the missing semicolons, etc., without changing the code logic)
// refactor:重构代码(既没有新增功能,也没有修复 bug) | Refactoring code (neither new feature nor bug fix)
// perf:性能, 体验优化 | Performance, experience optimization
// test:新增测试用例或是更新现有测试 | Add the new test cases or update the existing tests
// build:主要目的是修改项目构建系统(例如 glup,webpack,rollup 的配置等)的提交 | The main purpose is to modify the submission of the project building system (such as glup, webpack, rollup configuration, etc.)
// workflow:主要目的是修改项目继续集成流程(例如 Travis,Jenkins,GitLab CI,Circle等)的提交 | The main purpose is to modify the submission of project continued integration processes (e. g. Travis, Jenkins, GitLab CI, Circle, etc.)
// chore:不属于以上类型的其他类,比如构建流程, 依赖管理 | Other classes that do not belong to the above types, such as building processes, dependency management
// revert:回滚某个更早之前的提交 | Roll back some earlier previous submission
// version: 改变package.json 版本 | Change the package.json version

module.exports = {
  ignores: [commit => commit.includes('init')],
  extends: ['@commitlint/config-conventional'],
  rules: {
    'body-leading-blank': [2, 'always'],
    'footer-leading-blank': [1, 'always'],
    'header-max-length': [2, 'always', 108],
    'subject-empty': [2, 'never'],
    'type-empty': [2, 'never'],
    'subject-case': [0],
    'type-enum': [
      2,
      'always',
      [
        'feat',
        'fix',
        'perf',
        'style',
        'docs',
        'test',
        'refactor',
        'build',
        'type',
        'chore',
        'revert',
        'workflow',
        'release',
        'version',
      ],
    ],
  },
};

Git 贡献提交规范

  • feat 增加新功能
  • fix 修复问题/BUG
  • style 代码风格相关无影响运行结果的
  • perf 优化/性能提升
  • refactor 重构
  • revert 撤销修改
  • test 测试相关
  • docs 文档/注释
  • chore 依赖更新/脚手架配置修改等
  • workflow 工作流改进
  • ci 持续集成
  • types 类型定义文件更改
  • wip 开发中

Contributors

作者:Long Mo
字数统计:697 字
阅读时长:3 分钟
Long Mo
文章作者:Long Mo
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Longmo Docs