commit 提交规范

509 阅读2分钟

commit 格式

<type>(<scope>) : <subject>
<空行>
<body>
<空行>
<footer>
  • type:本次提交的类别,必填
  • scope:影响范围,可以不填
  • subject:提交的标题,一句话概括提交的内容
  • body:详细描述提交的内容,可以不填
  • footer:放置写备注啥的,如果是 bug ,可以把bug id放入

Header

Header部分只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)

type

type用于说明 commit 的类别,只允许使用下面标识。

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档(documentation)
  • style: 格式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  • test:增加测试
  • chore:构建过程或辅助工具的变动
  • merge:合并分支
  • perf:优化相关,比如提升性能、体验
  • revert:回滚到上一个版本
  • build:构建

如果 typefeatfix,则该 commit 将肯定出现在 Change log 之中。

scope

scope 用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

subject

subject 是 commit 目的的简短描述,不超过50个字符。

Body

Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例。

More detailed explanatory text, if necessary.  Wrap it to 
about 72 characters or so. 

Further paragraphs come after blank lines.

- Bullet points are okay, too
- Use a hanging indent

Footer

Footer 部分只用于两种情况。

  1. 不兼容变动

    如果当前代码与上一个版本不兼容,则 Footer 部分以 BREAKING CHANGE 开头,后面是对变动的描述、以及变动理由和迁移方法。

    BREAKING CHANGE: isolate scope bindings definition has changed.
    
      To migrate the code follow the example below:
    
      Before:
    
      scope: {
        myAttr: 'attribute',
      }
    
      After:
    
      scope: {
        myAttr: '@',  
      }
    
      The removed `inject` wasn't generaly useful for directives so   there should be no code using it.
    
  2. 关闭 Issue

    如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue 。

    Closes #234
    

    也可以一次关闭多个 issue 。

    Closes #123, #245, #992
    

工具

Commitizen

  1. 安装

    $ npm install -g commitizen
    
  2. 在项目目录里运行下面命令

    $ commitizen init cz-conventional-changelog --save --save-exact
    

以后,凡是用到git commit命令,一律改为使用git cz。这时,就会出现选项,用来生成符合格式的 Commit message。

commitlint & husky

  1. 安装

    $ npm i @commitlint/cli @commitlint/config-conventional husky -D
    
  2. 配置 commitlint.config.js 在项目根目录下新建 commitlint.config.js, 内容如下:

    module.exports = {
      extends: ['@commitlint/config-conventional'],
      rules: {
        'subject-case': [0, 'never'],
        'type-enum': [
          2,
          'always',
          [
            "docs", // Adds or alters documentation. 仅仅修改了文档,比如README, CHANGELOG, CONTRIBUTE等等
            "chore", // Other changes that don't modify src or test files. 改变构建流程、或者增加依赖库、工具等
            "feat", // Adds a new feature. 新增feature
            "fix", // Solves a bug. 修复bug
            "merge", // Merge branch ? of ?.
            "perf", // Improves performance. 优化相关,比如提升性能、体验
            "refactor", // Rewrites code without feature, performance or bug changes. 代码重构,没有加新功能或者修复bug
            "revert", // Reverts a previous commit. 回滚到上一个版本
            "style", // Improves formatting, white-space. 仅仅修改了空格、格式缩进、逗号等等,不改变代码逻辑
            "test", // Adds or modifies tests. 测试用例,包括单元测试、集成测试等
            "build" // 构建
          ]
        ]
      }
    };
    
  3. 修改package.json 文件夹

    在package.json 中添加husky 配置

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

conventional-changelog

conventional-changelog 就是生成 Change log 的工具,运行下面的命令即可。

$ npm install -g conventional-changelog-cli
$ cd my-project
$ conventional-changelog -p angular -i CHANGELOG.md -w

上面命令不会覆盖以前的 Change log,只会在 CHANGELOG.md 的头部加上自从上次发布以来的变动。

如果你想生成所有发布的 Change log,要改为运行下面的命令。

$ conventional-changelog -p angular -i CHANGELOG.md -w -r 0

为了方便使用,可以将其写入package.json的scripts字段。

{
  "scripts": {
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0"
  }
}

以后,直接运行下面的命令即可。

$ npm run changelog