|
1 | 1 | # git-hook-commit-msg
|
2 | 2 |
|
3 | | -检查git commit message 的 git hook 脚本 |
| 3 | +检查git commit message 的 git hook 脚本。 |
| 4 | + |
| 5 | +基本上使用Angular 规范,受 https://github.com/kentcdodds/validate-commit-msg 启发,并有多处借鉴。 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +以下内容,引用阮一峰老师的博文 |
| 10 | + |
| 11 | +http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html |
| 12 | + |
| 13 | +后续再替换为自己编辑的内容: |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## 一、Commit message 的作用 |
| 18 | + |
| 19 | +格式化的Commit message,有几个好处。 |
| 20 | + |
| 21 | +**(1)提供更多的历史信息,方便快速浏览。** |
| 22 | + |
| 23 | +比如,下面的命令显示上次发布后的变动,每个commit占据一行。你只看行首,就知道某次 commit 的目的。 |
| 24 | + |
| 25 | +> ``` |
| 26 | +> $ git log <last tag> HEAD --pretty=format:%s |
| 27 | +> ``` |
| 28 | +
|
| 29 | +**(2)可以过滤某些commit(比如文档改动),便于快速查找信息。** |
| 30 | +
|
| 31 | +比如,下面的命令仅仅显示本次发布新增加的功能。 |
| 32 | +
|
| 33 | +> ``` |
| 34 | +> $ git log <last release> HEAD --grep feature |
| 35 | +> |
| 36 | +> ``` |
| 37 | +
|
| 38 | +**(3)可以直接从commit生成Change log。** |
| 39 | +
|
| 40 | +
|
| 41 | +
|
| 42 | +## 二、Commit message 的格式 |
| 43 | +
|
| 44 | +每次提交,Commit message 都包括三个部分:Header,Body 和 Footer。 |
| 45 | +
|
| 46 | +> ``` |
| 47 | +> <type>(<scope>): <subject> |
| 48 | +> // 空一行 |
| 49 | +> <body> |
| 50 | +> // 空一行 |
| 51 | +> <footer> |
| 52 | +> |
| 53 | +> ``` |
| 54 | +
|
| 55 | +其中,Header 是必需的,Body 和 Footer 可以省略。 |
| 56 | +
|
| 57 | +不管是哪一个部分,任何一行都不得超过72个字符(或100个字符)。这是为了避免自动换行影响美观。 |
| 58 | +
|
| 59 | +### 2.1 Header |
| 60 | +
|
| 61 | +Header部分只有一行,包括三个字段:`type`(必需)、`scope`(可选)和`subject`(必需)。 |
| 62 | +
|
| 63 | +**(1)type** |
| 64 | +
|
| 65 | +`type`用于说明 commit 的类别,只允许使用下面7个标识。 |
| 66 | +
|
| 67 | +> * feat:新功能(feature) |
| 68 | +> * fix:修补bug |
| 69 | +> * docs:文档(documentation) |
| 70 | +> * style: 格式(不影响代码运行的变动) |
| 71 | +> * refactor:重构(即不是新增功能,也不是修改bug的代码变动) |
| 72 | +> * test:增加测试 |
| 73 | +> * chore:构建过程或辅助工具的变动 |
| 74 | +
|
| 75 | +如果`type`为`feat`和`fix`,则该 commit 将肯定出现在 Change log 之中。其他情况(`docs`、`chore`、`style`、`refactor`、`test`)由你决定,要不要放入 Change log,建议是不要。 |
| 76 | +
|
| 77 | +**(2)scope** |
| 78 | +
|
| 79 | +`scope`用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。 |
| 80 | +
|
| 81 | +**(3)subject** |
| 82 | +
|
| 83 | +`subject`是 commit 目的的简短描述,不超过50个字符。 |
| 84 | +
|
| 85 | +> * 以动词开头,使用第一人称现在时,比如`change`,而不是`changed`或`changes` |
| 86 | +> * 第一个字母小写 |
| 87 | +> * 结尾不加句号(`.`) |
| 88 | +
|
| 89 | +### 2.2 Body |
| 90 | +
|
| 91 | +Body 部分是对本次 commit 的详细描述,可以分成多行。下面是一个范例。 |
| 92 | +
|
| 93 | +> ``` |
| 94 | +> More detailed explanatory text, if necessary. Wrap it to |
| 95 | +> about 72 characters or so. |
| 96 | +> |
| 97 | +> Further paragraphs come after blank lines. |
| 98 | +> |
| 99 | +> - Bullet points are okay, too |
| 100 | +> - Use a hanging indent |
| 101 | +> |
| 102 | +> ``` |
| 103 | +
|
| 104 | +有两个注意点。 |
| 105 | +
|
| 106 | +(1)使用第一人称现在时,比如使用`change`而不是`changed`或`changes`。 |
| 107 | +
|
| 108 | +(2)应该说明代码变动的动机,以及与以前行为的对比。 |
| 109 | +
|
| 110 | +### 2.3 Footer |
| 111 | +
|
| 112 | +Footer 部分只用于两种情况。 |
| 113 | +
|
| 114 | +**(1)不兼容变动** |
| 115 | +
|
| 116 | +如果当前代码与上一个版本不兼容,则 Footer 部分以`BREAKING CHANGE`开头,后面是对变动的描述、以及变动理由和迁移方法。 |
| 117 | +
|
| 118 | +> ``` |
| 119 | +> BREAKING CHANGE: isolate scope bindings definition has changed. |
| 120 | +> |
| 121 | +> To migrate the code follow the example below: |
| 122 | +> |
| 123 | +> Before: |
| 124 | +> |
| 125 | +> scope: { |
| 126 | +> myAttr: 'attribute', |
| 127 | +> } |
| 128 | +> |
| 129 | +> After: |
| 130 | +> |
| 131 | +> scope: { |
| 132 | +> myAttr: '@', |
| 133 | +> } |
| 134 | +> |
| 135 | +> The removed `inject` wasn't generaly useful for directives so there should be no code using it. |
| 136 | +> |
| 137 | +> ``` |
| 138 | +
|
| 139 | +**(2)关闭 Issue** |
| 140 | +
|
| 141 | +如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue 。 |
| 142 | +
|
| 143 | +> ``` |
| 144 | +> Closes #234 |
| 145 | +> |
| 146 | +> ``` |
| 147 | +
|
| 148 | +也可以一次关闭多个 issue 。 |
| 149 | +
|
| 150 | +> ``` |
| 151 | +> Closes #123, #245, #992 |
| 152 | +> |
| 153 | +> ``` |
| 154 | +
|
| 155 | +### 2.4 Revert |
| 156 | +
|
| 157 | +还有一种特殊情况,如果当前 commit 用于撤销以前的 commit,则必须以`revert:`开头,后面跟着被撤销 Commit 的 Header。 |
| 158 | +
|
| 159 | +> ``` |
| 160 | +> revert: feat(pencil): add 'graphiteWidth' option |
| 161 | +> |
| 162 | +> This reverts commit 667ecc1654a317a13331b17617d973392f415f02. |
| 163 | +> |
| 164 | +> ``` |
| 165 | +
|
| 166 | +Body部分的格式是固定的,必须写成`This reverts commit <hash>.`,其中的`hash`是被撤销 commit 的 SHA 标识符。 |
| 167 | +
|
| 168 | +如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的`Reverts`小标题下面。 |
0 commit comments