Commit message in AngularJS format

Posted by Max on April 12, 2016
Commit message in AngularJS format

1. 前言

代码管理时,我每次提交的信息都比较散乱,后期查看历史时往往不能很快找到自己想要的信息。从commitlogsfromlastnight网站处可以看到各路网友各式各样的commit message,可以感受下。读了阮一峰老师的文章后有一些领悟,学习&记录。

2. AngularJS规范

2.1 目标

这也是格式化的Commit message的优势。
  1. 查询历史时,提供更简明的信息 只看首行即可清楚每次提交的大概:
    $ git log HEAD --pretty=format:%s
    
  2. 过滤某一类型的提交信息,快速查找 比如只查看新增的功能:
    $ git log HEAD --grep feature
    
    或者过滤掉某些不重要的提交(空行、注释等):
    $ git bisect skip $(git rev-list --grep irrelevant HEAD)
    
  3. 自动生成CHANGELOG CHANGELOG是发布新版本时,用来说明与上一个版本差异的文档,一般包含new features, bug fixes, breaking changes等信息。</p>

2.2 格式

每个Commit message包括三个部分,HeaderBodyFooter,以空行分隔。 注:任何一行都不要超过72个字符,避免自动换行影响美观。

2.2.1 Revert

有一种特殊情况,即如果当前commit用于撤销之前的某个 commit,则必须以'revert:'开头,后面跟着被撤销commit的Header。示例:
revert: feat(pencil): add 'graphiteWidth' option

This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
注:Body部分的格式是固定的,必须写成This reverts commit <hash>.。其中的 hash 是被撤销 commit 的 SHA 标识符。 如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 CHANGELOG 里面。如果两者在不同的发布,那么当前 commit,会出现在 CHANGELOG 的Reverts小标题下面。

2.2.2 Header

每个Header也包含三个部分,type(必)、scope(选)、subject(必)。
type
type标明commit的类型,分为如下7个:
  • feat (feature)
  • fix (bug fix)
  • docs (documentation)
  • style (formatting, missing semi colons, …)
  • refactor
  • test (when adding missing tests)
  • chore (maintain)
scope
scope标明commit的影响范围,可以是\$location, \$browser, \$compile, \$rootScope, ngHref, ngClick, ngView等,视情况而定。
subject
subject是commit的简要描述,要求
  • 动宾短语(祈使语气,一般现在时)
  • 首字母小写
  • 结尾不加句号

2.2.3 Body

Body是对本次commit的详细描述,可以分成多行。有两个注意点:
  • subject一样,使用祈使语气,一般现在时态。
  • 说明代码变动的动机,以及与以前行为的对比。

2.2.4 Footer

Footer用于两种情形,不兼容变动Breaking changes和Issue关联Referencing issues
Breaking changes
如果当前代码与上一个版本不兼容,则 Footer 部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法。示例:
BREAKING CHANGE: isolate scope bindings definition has changed and
the inject option for the directive controller injection was removed.

To migrate the code follow the example below:

Before:

scope: {
myAttr: 'attribute',
myBind: 'bind',
myExpression: 'expression',
myEval: 'evaluate',
myAccessor: 'accessor'
}

After:

scope: {
myAttr: '@',
myBind: '@',
myExpression: '&amp;',
// myEval - usually not useful, but in cases where the expression is assignable, you can use '='
myAccessor: '=' // in directive's template change myAccessor() to myAccessor
}

The removed `inject` wasn't generaly useful for directives so there should be no code using it.
Referencing issues
如果本次commit与某个Issue相关,或者是关闭某个BUG Issue,可以在Footer中将其关联起来。示例:
Closes #123, #245, #992

2.3 示例

feat($browser): onUrlChange event (popstate/hashchange/polling)

Added new event to $browser:
- forward popstate event if available
- forward hashchange event if popstate not available
- do polling when neither popstate nor hashchange available

Breaks $browser.onHashChange, which was removed (use onUrlChange instead)

fix($compile): couple of unit tests for IE9

Older IEs serialize html uppercased, but IE9 does not...
Would be better to expect case insensitive, unfortunately jasmine does
not allow to user regexps for throw expectations.

Closes #392
Breaks foo.bar api, foo.baz should be used instead

feat(directive): ng:disabled, ng:checked, ng:multiple, ng:readonly, ng:selected

New directives for proper binding these attributes in older browsers (IE).
Added coresponding description, live examples and e2e tests.

Closes #351

style($location): add couple of missing semi colons

docs(guide): updated fixed docs from Google Docs

Couple of typos fixed:
- indentation
- batchLogbatchLog -> batchLog
- start periodic checking
- missing brace

3. 参考

  1. 《Commit message 和 Change log 编写指南》阮一峰的网络日志
  2. 《Git Commit Message Conventions》google文档
</content:encoded> </item>