前端工程化(二):规范Git说明规范
上一张我们利用husky进行提交之前的一些脚本操作,但是我们对于commit message并没有做限制,如果提交的commit message不规范的话,很容易无法清晰明了的指导某次提交的目的。所以我们需要利用一些工具来校验和自动化填写大部分消息规范。
首先我们先来了解一下commit message的规范:
1.commit message format(信息域)
commit message一般分为三个部分Header,Body 和 Footer
<type>(<scope>): <subject>
// 空一行
<body>
// 空一行
<footer>
其中,Header 是必需的,Body 和 Footer 可以省略
Example:
PS D:\git\pythonPractice> git log
commit 58a7a966acb9aa2fffc0e02c9ce3be64b8949991 (HEAD -> master)
Author: Zhiwei Tian <hebeitianzhiwei@outlook.com>
Date: Fri Aug 17 17:38:36 2018 +0800
feat(serve): add grpc server
1.1 HEAD
1.1.1 type
type
用于说明commit
的类别,正常我们使用下面这几种
feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style: 格式(不影响代码运行的变动)
refactor:重构(即不是新增功能,也不是修改bug的代码变动)
test:增加测试
chore:构建过程或辅助工具的变动
1.1.2 scope
scope
用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。
1.1.3 subject
subject
是 commit 目的的简短描述,不超过50个字符。
1.2 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
1.3 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
2.commitlint
首先安装一下两个依赖:
npm install --save-dev @commitlint/config-conventional @commitlint/cli
然后在项目根目录新建一个commitlint.config.js
,内容如下
module.exports = {
extends: ["@commitlint/config-conventional"],
};
此时提交的话,并不会校验commit message,我们需要利用husky
在git钩子下进行验证:
yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'
git会在commit-msg这个钩子下进行校验我们的commit message。
3.commitizen
这个工具可以让我们自动提示在提交时所有必需的提交字段,不需要等到commitlint校验然后拒绝此次提交。
我们使用局部安装相关依赖:
npm install --save-dev commitizen
这时候我们可以使用git cz
来提交代码,这时候就回出现以下的界面:
我们可以用键盘操作,选择相关信息。但是这些功能必需执行git cz
才行,如果我们使用git commit
就可以跳过这些步骤,所以我们需要强制使用git cz
指令去提交,我们利用husky
在prepare-commit-msg
,添加以下内容:
exec < /dev/tty && npx cz --hook || true
4.自定义commitizen
cz-customizable可以让我们自定义自己的代码提交规范。
至此我们就完成了自己的commit message规范了。