0%

代码提交注释(commit)与产品变更历史(ChangeLog)的推荐写法

代码提交注释

目前没有统一的强制的代码提交注释规范,社区有一些规范,这里着重介绍Angular规范https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines

Commit message目的

  • 清晰明了,说明本次提交的目的
  • 自动生成CHANGELOG.md
  • 识别不重要的提交
  • 为浏览提交历史时提供更明确的信息

Angular规范标准格式

1
2
3
4
5
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>

总体要求,每行字符不得超过72个或100个,避免自动换行

关键词解释

type:说明本次commit的类别,只允许使用

  • feature:一项新功能
  • fix:一个错误修复
  • docs:仅文档更改
  • style:不影响代码含义的更改(空格,格式,缺少分号等)
  • perf:优化或改善性能
  • refactor:重构
  • test:增加测试
  • chore:构建过程或者辅助工具变化
  • revert:用于撤销以前的 commit后面跟着被撤销 Commit 的首行信息,Body部分的格式是固定的,必须写成This reverts commit <hash>.,其中的hash是被撤销 commit 的 SHA 标识符。
  • deps:或者build,依赖的外部资源变化,待定
1
2
3
revert: feat(pencil): add 'graphiteWidth' option

This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

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

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

注意<type>(<scope>): <subject>只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)

1
2
3
以动词开头,使用第一人称现在时,比如change,而不是changed或changes
第一个字母小写
结尾不加句号(.)

body:对本次 commit 的详细描述,可以分成多行

1
2
3
4
5
6
7
8
9
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
- 使用第一人称现在时,比如使用change而不是changed或changes
- 说明代码变动的动机,以及与以前行为的对比

footer:只在两种情况出现,不兼容的变动,关闭issue

不兼容变动描述以BREAKING CHANGE开头,后面是变动描述和变动理由及其迁移方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.

关闭issue则以Closes开头

1
Closes #123, #245, #992

标准commit的辅助工具

Commitizenhttps://github.com/commitizen/cz-cli

validate-commit-msghttps://github.com/kentcdodds/validate-commit-msg

根据标准commit生成ChangeLog的辅助工具

conventional-changeloghttps://github.com/ajoslin/conventional-changelog