commit提交规范

采用 Angular 的 commit 规范。不写 commit message 不允许提交代码。

commit 基本格式

分为三部分:headerbodyfooter
header是必须的,bodyfooter 可以省略。

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

<type>(<scope>): <subject>

header 包含三个字段: type(必须)、scope(可选)和 subject(必须)

type

用于说明本次提交的类型,包含下面几种情况:

  • feat:新增feature
  • fix:修复 bug
  • perf: 提高代码性能的更改
  • docs: 仅仅修改了文档。如:README
  • style: 仅仅修改了代码样式,并不改变代码逻辑。如:缩进、空格和逗号等。
  • refactor: 代码重构。没有增加新功能,也没有修改 bug。
  • test: 测试用例相关。
  • build: 构建流程或者依赖库的变动。
  • revert: 回滚到上一个版本
  • deps: 升级依赖
scope

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

修改文件的范围(包括但不限于 doc, middleware, core, config, plugin)

subject

此次 commit 的简短描述。建议不超过 50 个字符。

常用表述语:

  • add 添加
  • change 改变
  • update 更新
  • remove 移动
  • delete 删除

注意: 使用第一人称现在时,比如使用 change 而不是 changed 或 changes。

body

针对本次 commit 的详细描述,可以多行。需要描述的信息包括:

  • 代码变动的动机
  • 与以前行为的对比

footer 的内容只会在以下两种情况的时候使用:

不兼容的变动

如果是和之前不兼容的变动,应该以 BREAKING CHANGE 为开头。比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 哪些被改变了
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'
}
After:
scope: {
myAttr: '@',
myBind: '@',
myExpression: '&'
}
// 改变的原因
The removed `inject` wasn't generaly useful for directives so there should be no code using it.

关闭 issue

如果此次改动是为了关闭某个 issue,则可以在 footer 中关闭。例如:

1
Closes #234

或者关闭多个 issue:

1
Closes #123, #245, #992

revert

如果 commit 用于撤销以前的 commit,则必须以 revert: 开头,后面跟着被撤销 Commit 的 Header。

1
2
revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

body 的格式是固定的:This reverts commit <hash>hash 是被撤销的 commit 的 SHA 标识符。

栗子

  1. 解决 bug:
    1
    2
    3
    4
    5
    6
    7
    8
    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

2.修改标点符号

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

  1. 新增功能
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    feat($compile): simplify isolate scope bindings
    Changed the isolate scope binding options to:
    - @attr - attribute binding (including interpolation)
    - =model - by-directional model binding
    - &expr - expression execution binding
    This change simplifies the terminology as well as
    number of choices available to the developer. It
    also supports local name aliasing from the parent.
    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'
    }
    After:
    scope: {
    myAttr: '@',
    myBind: '@'
    }
    The removed `inject` wasn't generaly useful for directives so there should be no code using it.

Git分支规范

  • 基本原则:master为保护分支,不直接在master上进行代码修改和提交。
  • 分支命名规范:
    • 分支版本命名规则:分支类型分支发布时间 分支功能。比如:feature_20170401_fairy_flower
    • 分支类型包括:featurebugfixrefactor 三种类型,即新功能开发bug 修复代码重构
    • 时间使用年月日进行命名,不足 2 位补 0
    • 分支功能命名使用 snake case 命名法,即下划线命名。

commit 工具流

commitizen

全局安装 commitizen

1
npm i -g commitizen

然后,在项目目录里,运行下面的命令,使其支持 Angular 的 Commit message 格式。

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

以后凡是用到 git commit 的地方都用 git cz。然后就会出现生成规范的 git message 的选项:

git-cz.jpeg

validate-commit-msg

validate-commit-msg 用来检查 commit message 是否符合规范。

1、安装 validate-commit-msg 依赖:

1
npm i validate-commit-msg -S

2、使用 githooks 来校验:

安装 Husky

Husky 继承了 Git 下所有的钩子,在触发钩子的时候,Husky 可以阻止不合法的 commit, push 等等。

1
npm i Husky -S

配置参数:

1
2
3
4
5
6
7
// package.json
{
"script": {
"commitmsg": "validate-commit-msg"
}
}

之后提交代码的时候可以用 git cz 来提交,有一个整体的流程来提醒你用规范的 commit message 来说明本次的提交信息。不过我建议还是用 git commit -m "message" 来提交,这样可以强制自己记住提交规范,不用依赖外部帮助,便于自己养成良好的习惯。

生成 change log

如果你的提交信息都符合 Angular 的规范,那么这些信息就会出现在 change log 里面。

只有 typefeatfixcommit 会出现在 change log 中,其他的不会出现,当然你也可以自己自定义。

如何生成 change log

安装依赖 npm i conventional-changelog -S

在 package.json 中配置下面的命令:

1
2
3
4
5
{
"script": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
}
}

然后运行命令:npm run changelogCHANGELOG.md 文件就会出现在你的项目根目录下面。

参考

支持原创