在多人协作的项目中,如果Git的提交说明精准,在后期协作以及Bug处理时会变得有据可查,项目的开发可以根据规范的提交说明快速生成开发日志,从而方便开发者或用户追踪项目的开发信息和功能特性。

本文主要内容:

  • 介绍符合Angular规范(需要翻墙)的提交说明
  • 介绍提交说明工具集cz(适配器、校验以及日志)的使用方法
  • 介绍供开发者快速生成项目cz工具集的Vue CLI 3插件@ziyi2/ui-cz

这里提供演示项目地址:cz-example。

Git的提交说明

Git每次提交代码的时候都需要手写提交说明(Commit message):

git commit -m "hello world"
复制代码

书写多行可以使用以下命令:

git commit
复制代码

此时会跳出一个文本编辑器,可以在文本编辑器中书写多行提交说明

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up to date with 'origin/master'.
#
# Changes to be committed:
#       new file:   package.json
#
G:/git-lab/cz/.git/COMMIT_EDITMSG [unix] (19:49 24/01/2019)
复制代码

如果没有规范的提交说明,很难阐述当前代码的提交性质(修复Bug、代码性能优化、新增功能或者发布版本等)。查看Vue项目的Git提交说明fix表明修复问题、feat表明新增功能...),它完全符合Angular规范:

手写符合规范的提交说明很难避免错误,可以借助工具来实现规范的提交说明

规范的Git提交说明

  • 提供更多的历史信息,方便快速浏览
  • 可以过滤某些commit,便于筛选代码review
  • 可以追踪commit生成更新日志
  • 可以关联issues

Git提交说明结构

Git提交说明可分为三个部分:HeaderBodyFooter

<Header> <Body> <Footer>
复制代码

Header

Header部分包括三个字段type(必需)、scope(可选)和subject(必需)。

<type>(<scope>): <subject>
复制代码

Vue源码的提交说明省略了scope

type

type用于说明 commit 的提交性质。

描述
feat 新增一个功能
fix 修复一个Bug
docs 文档变更
style 代码格式(不影响功能,例如空格、分号等格式修正)
refactor 代码重构
perf 改善性能
test 测试
build 变更项目构建或外部依赖(例如scopes: webpack、gulp、npm等)
ci 更改持续集成软件的配置文件和package中的scripts命令,例如scopes: Travis, Circle等
chore 变更构建流程或辅助工具
revert 代码回退

scope

scope说明commit影响的范围。scope依据项目而定,例如在业务项目中可以依据菜单或者功能模块划分,如果是组件库开发,则可以依据组件划分。

提示:scope可以省略。

subject

subjectcommit的简短描述。

Body

commit的详细描述,说明代码提交的详细说明。

Footer

如果代码的提交是不兼容变更关闭缺陷,则Footer必需,否则可以省略。

不兼容变更

当前代码与上一个版本不兼容,则FooterBREAKING CHANGE开头,后面是对变动的描述、以及变动的理由和迁移方法。

关闭缺陷

如果当前提交是针对特定的issue,那么可以在Footer部分填写需要关闭的单个 issue 或一系列issues。

Commitizen

commitizen/cz-cli是一个可以实现规范的提交说明的工具:

When you commit with Commitizen, you'll be prompted to fill out any required commit fields at commit time. No more waiting until later for a git commit hook to run and reject your commit (though that can still be helpful). No more digging through CONTRIBUTING.md to find what the preferred format is. Get instant feedback on your commit message formatting and be prompted for required fields.

提供选择的提交信息类别,快速生成提交说明。安装cz工具:

npm install -g commitizen
复制代码

Commitizen适配器

cz-conventional-changelog

如果需要在项目中使用commitizen生成符合AngularJS规范的提交说明,初始化cz-conventional-changelog适配器:

commitizen init cz-conventional-changelog --save --save-exact
复制代码

如果当前已经有其他适配器被使用,则会报以下错误,此时可以加上--force选项进行再次初始化

Error: A previous adapter is already configured. Use --force to override
复制代码

初始化命令主要进行了3件事情

  1. 在项目中安装cz-conventional-changelog 适配器依赖

  2. 将适配器依赖保存到package.jsondevDependencies字段信息

  3. package.json中新增config.commitizen字段信息,主要用于配置cz工具的适配器路径:

"devDependencies": {"cz-conventional-changelog": "^2.1.0"
},
"config": {"commitizen": {"path": "./node_modules/cz-conventional-changelog"}
}
复制代码

接下来可以使用cz的命令git cz代替git commit进行提交说明

代码提交到远程的Github后,可以在相应的项目中进行查看,例如(这里使用feat不是很合适,只是一个示例):

cz-customizable

如果想定制项目的提交说明,可以使用cz-customizable适配器:

Suitable for large teams working with multiple projects with their own commit scopes. When you specify the scopes in your .cz-config.js, cz-customizable allows you to select the pre-defined scopes. No more spelling mistakes embarrassing you when generating the changelog file.

安装适配器

npm install cz-customizable --save-dev

将之前符合Angular规范的cz-conventional-changelog适配器路径改成cz-customizable适配器路径:

"devDependencies": {"cz-customizable": "^5.3.0"
},
"config": {"commitizen": {"path": "node_modules/cz-customizable"}
}
复制代码

cz-customizable will first look for a file called .cz-config.js,alternatively add a config block in your package.json。

官方提供了一个.cz-config.js示例文件cz-config-EXAMPLE.js,如下所示:

'use strict';module.exports = {types: [{value: 'feat',     name: 'feat:     A new feature'},{value: 'fix',      name: 'fix:      A bug fix'},{value: 'docs',     name: 'docs:     Documentation only changes'},{value: 'style',    name: 'style:    Changes that do not affect the meaning of the code\n            (white-space, formatting, missing semi-colons, etc)'},{value: 'refactor', name: 'refactor: A code change that neither fixes a bug nor adds a feature'},{value: 'perf',     name: 'perf:     A code change that improves performance'},{value: 'test',     name: 'test:     Adding missing tests'},{value: 'chore',    name: 'chore:    Changes to the build process or auxiliary tools\n            and libraries such as documentation generation'},{value: 'revert',   name: 'revert:   Revert to a commit'},{value: 'WIP',      name: 'WIP:      Work in progress'}],scopes: [{name: 'accounts'},{name: 'admin'},{name: 'exampleScope'},{name: 'changeMe'}],// it needs to match the value for field type. Eg.: 'fix'/*scopeOverrides: {fix: [{name: 'merge'},{name: 'style'},{name: 'e2eTest'},{name: 'unitTest'}]},*/// override the messages, defaults are as followsmessages: {type: 'Select the type of change that you\'re committing:',scope: '\nDenote the SCOPE of this change (optional):',// used if allowCustomScopes is truecustomScope: 'Denote the SCOPE of this change:',subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',breaking: 'List any BREAKING CHANGES (optional):\n',footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',confirmCommit: 'Are you sure you want to proceed with the commit above?'},allowCustomScopes: true,allowBreakingChanges: ['feat', 'fix'],// limit subject lengthsubjectLimit: 100};
复制代码

这里对其进行汉化处理(只是为了说明定制说明的一个示例):

'use strict';module.exports = {types: [{value: '特性',     name: '特性:    一个新的特性'},{value: '修复',      name: '修复:    修复一个Bug'},{value: '文档',     name: '文档:    变更的只有文档'},{value: '格式',    name: '格式:    空格, 分号等格式修复'},{value: '重构', name: '重构:    代码重构,注意和特性、修复区分开'},{value: '性能',     name: '性能:    提升性能'},{value: '测试',     name: '测试:    添加一个测试'},{value: '工具',    name: '工具:    开发工具变动(构建、脚手架工具等)'},{value: '回滚',   name: '回滚:    代码回退'}],scopes: [{name: '模块1'},{name: '模块2'},{name: '模块3'},{name: '模块4'}],// it needs to match the value for field type. Eg.: 'fix'/*scopeOverrides: {fix: [{name: 'merge'},{name: 'style'},{name: 'e2eTest'},{name: 'unitTest'}]},*/// override the messages, defaults are as followsmessages: {type: '选择一种你的提交类型:',scope: '选择一个scope (可选):',// used if allowCustomScopes is truecustomScope: 'Denote the SCOPE of this change:',subject: '短说明:\n',body: '长说明,使用"|"换行(可选):\n',breaking: '非兼容性说明 (可选):\n',footer: '关联关闭的issue,例如:#31, #34(可选):\n',confirmCommit: '确定提交说明?'},allowCustomScopes: true,allowBreakingChanges: ['特性', '修复'],// limit subject lengthsubjectLimit: 100};
复制代码

再次使用git cz命令进行提交说明

从上图可以看出此时的提交说明选项已经汉化,继续填写提交说明

把代码提交到远程看看效果:

Commitizen校验

commitlint

校验提交说明是否符合规范,安装校验工具commitlint:

npm install --save-dev @commitlint/cli
复制代码

@commitlint/config-conventional

安装符合Angular风格的校验规则

npm install --save-dev @commitlint/config-conventional
复制代码

在项目中新建commitlint.config.js文件并设置校验规则:

module.exports = {extends: ['@commitlint/config-conventional']
};
复制代码

安装huksy(git钩子工具)

npm install husky --save-dev
复制代码

在package.json中配置git commit提交时的校验钩子:

"husky": {"hooks": {"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}
}
复制代码

需要注意,使用该校验规则不能对.cz-config.js进行不符合Angular规范的定制处理,例如之前的汉化,此时需要将.cz-config.js的文件按照官方示例文件cz-config-EXAMPLE.js进行符合Angular风格的改动。

执行错误的提交说明:

执行符合Angular规范的提交说明:

commitlint需要配置一份校验规则,@commitlint/config-conventional就是符合Angular规范的一份校验规则。

commitlint-config-cz

如果是使用cz-customizable适配器做了破坏Angular风格的提交说明配置,那么不能使用**@commitlint/config-conventional**规则进行提交说明校验,可以使用commitlint-config-cz对定制化提交说明进行校验。

安装校验规则:

npm install commitlint-config-cz --save-dev
复制代码

然后加入commitlint校验规则配置:

module.exports = {extends: ['cz']
};
复制代码

这里推荐使用**@commitlint/config-conventional**校验规则,如果想使用cz-customizable适配器,那么定制化的配置不要破坏Angular规范即可。

validate-commit-msg

除了使用commitlint校验工具,也可以使用validate-commit-msg校验工具对cz提交说明是否符合Angular规范进行校验。

Commitizen日志

如果使用了cz工具集,配套conventional-changelog可以快速生成开发日志:

npm install conventional-changelog -D
复制代码

pacage.json中加入生成日志命令:

"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
复制代码

You could follow the following workflow

  • Make changes
  • Commit those changes
  • Pull all the tags
  • Run the npm version [patch|minor|major] command
  • Push

执行npm run version后可查看生产的日志CHANGELOG.md。

注意要使用正确的Headertype,否则生成的日志会不准确,这里只是一个示例,生成的日志不是很严格。

Vue CLI 3 插件

如果对于上述所说的配置感到繁琐,这里提供一个Vue CLI 3的插件,如果开发的项目由Vue CLI 3系统生成,可以使用插件@ziyi2/ui-cz一键生成:

vue add @ziyi2/ui-cz
复制代码

该插件采用了cz-customizable定制化提交说明的适配器、@commitlint/config-conventional校验规则以及conventional-changelog日志生成器。

总结

呼吁大家书写规范的提交说明,代码说明不规范,项目成员泪两行。演示项目地址:cz-example。

链接

  • Angular规范

  • cz-cli - cz工具

  • cz-customizable - cz适配器

  • @commitlint/config-conventional - cz适配器

  • commitlint - cz校验工具

  • commitlint-config-cz - cz校验工具的校验规则

  • validate-commit-msg - cz校验工具

  • conventional-changelog - cz日志生成器

  • commit_msg - git钩子文档

Cz工具集使用介绍 - 规范Git提交说明相关推荐

  1. xperf工具集的介绍

    转自 iteam的共享空间 的日志 xperf 是Windows Performance Tools Kit中的一个工具,这一系列强大的工具包主要针对操作系统启动阶段和关闭阶段进行底层的日志捕捉和性能 ...

  2. Linux下autoTools工具集使用介绍

    Author : iStone E-mail : liul.stone@gmail.com Date : 2015-09-19 15:16:38 一 使用autoTools工具集 1.1 什么是aut ...

  3. 0day工具集(资源)介绍-0day漏洞利用原理(2)

    往期文章: 漏洞概述-0day漏洞利用原理(0)_luozhonghua2000的博客-CSDN博客 二进制概述-0day漏洞利用原理(1)_luozhonghua2000的博客-CSDN博客 上篇已 ...

  4. 持续集成工具集之四 Jenkins+Maven+Git+Tomcat 项目构建和自动部署

    上面安装和配置好Jenkins之后,就可以开始配置构建项目了 新建 因为需要构建的项目是maven项目,所以这里填好项目的名称之后选择"构建一个maven项目",然后点左下的ok ...

  5. SAP BW BEx工具集简单介绍【AV+PPT】

    比较长.部门内部交流视频,水平有限,敬请原谅,附送PPT. SAP BW BEx View more presentations from erickdu888. 转载于:https://www.cn ...

  6. git提交规范,规范自己的提交标准

    为了规范我的git提交内容,提交的时候commit -m "备注的信息",但是每个人的备注信息千奇百怪,为了统一,我们进行了git的规范. 首先要全局安装commitizen np ...

  7. [转]《吐血整理》系列-顶级程序员工具集

    你知道的越多,你不知道的越多 点赞再看,养成习惯 GitHub上已经开源 https://github.com/JavaFamily 有一线大厂面试点脑图.个人联系方式,欢迎Star和指教 前言 这期 ...

  8. 《吐血整理》顶级程序员工具集

    前言 这期是被人才群交流里,还有很多之前网友评论强行顶出来的一期,就是让我介绍自己常用的一些工具给他们安利一下,我一听很高兴呀,帅丙我这么乐于奉献的人是吧. 主要是能水一篇文章就很开心,不过写下来发现 ...

  9. sanitizer工具集

    sanitizer工具集的介绍 Sanitizers是谷歌发起的开源工具集,包括了Address Sanitizer, undefined behavior Sanitizer, Thread San ...

最新文章

  1. 【组队学习】【34期】零基础学python编程思维
  2. SQL-十步完全理解 SQL
  3. python tkinter
  4. 电脑技巧 ADSL如何远程盗号
  5. 下行文格式图片_下行文标准模版
  6. 2020朝花夕拾-不务正业的大学生做了什么比赛?
  7. 利用win10自带的系统配置禁止开机启动项和程序
  8. 使用关键词快速搜索商品代码
  9. 都掏出来了,大学四年珍藏的26个宝藏网站,全部整理好给大家!!!
  10. python一对一辅导教程:PyGeM Tutorials 解析 1
  11. CF乱码问题解决方案
  12. AutoCad软件界面乱码
  13. win10修改docker镜像的存储位置
  14. 《Python编程从入门到实践》_第二章_变量和简单数据类型
  15. 华为宿舍租金涨价 数千员工群情激昂
  16. 硕士论文答辩需要注意哪些问题?
  17. 静息态下大脑的动态模块化指纹
  18. [exceptions]如何排查can not find symbol的编译错误
  19. Tikz作图教程:动手 VS 动脑? 97行代码 VS 6行代码?
  20. Nano-X的详细介绍

热门文章

  1. RT-Thread 4.1.0 特性解析之LIBC与POSIX
  2. 唯冠和苹果的官司打得热闹
  3. python爬取微博评论点赞数_python 爬虫 爬微博 分析 数据
  4. 电商中spu和sku是什么?怎么去设计表结构?
  5. 2022年茶艺师(中级)考试模拟100题模拟考试平台操作
  6. 全球域名后缀注册量排行榜!
  7. ContentObserver去实现拒收短信或短信黑名单等功能
  8. 腾讯招聘信息 爬取案例
  9. 关于汇编指令ldr和str的理解
  10. oracle字符乱码 老熊,【转载】Hint的常见错误使用方式