简介
为了更好的维护commit的提交日志,更规范的展示对应的修改信息及变更体系,参考taro的changelog日志管理体系,引用conventional-changelog日志生成(taro);
日志管理带着问题去一步一步的展开
- 日志的规范如何定义?
- 如何强制约定开发者按照规范添加提交记录?
- changelog如何根据git commit 的提交记录生成?
- 是否有提交的日志提示功能?
日志的规范如何定义?
这些规则是从AngularJS提交约定中采用的。 Angular Style Commit Message Conventions
目的(Goals)
- 允许通过脚本生成CHANGELOG.md
- 允许忽略git bisect的提交(不重要的提交,例如格式化)
- 在生成的changelog文件中提供更有效的提交信息
生成CHANGELOG.md
我们在展示日志中使用以下三个部分:新功能(feat),错误修复(fix),重大更改()。发布时,此列表可以由脚本生成。以及相关提交的链接(对应commit提交id号链接的日志)。当然,在通过自动化工具生成之后,可以更改后在上传。
具体的生成对应log日志,使用了git的命令为(目前此操作可以用”conventional-changelog-cli”生成),如果对具体的生成原理感兴趣可以参考
格式化出到当前最后一次提交的所有日志的头
- git log
HEAD —pretty=format:%s
过滤出已feature头的日志
git log
HEAD —grep feature 识别不重要的提交
git bisect skip $(git rev-list —grep irrelevant
HEAD)
提交文档规范的约定 (Format of the commit message)
观察一下规范包括的属性: type、scope、 subject、 BLANK LINE(body的前空行及后空行)、footer; 看到的样式基本就是符合这个模版, 严格控制具体的展示。接下来我们就来具体分析不同的类型有那些属性及对应值:
⚠️ 提交消息的任何一行都不能超过100个字符!这使得消息在github以及各种git工具中更易于阅读。
<type>(<scope>): <subject><BLANK LINE><body><BLANK LINE><footer>
1. text
主题行包含对更改的简洁描述。
- 动词优先
- 不要大写第一个字母
-
2.
允许的类型保函那些
feat (feature): 新加的功能点
- fix (bug fix): bug错误修复
- docs (documentation): 文档功能调整
- style (formatting, missing semi colons, …): css样式调整
- refactor: 重构(即不是新增功能,也不是修改bug的代码变动)
- test (when adding missing tests) : 测试功能新增
- chore (maintain): 构建过程或辅助工具的变动
- revert: 回滚到上一个版本
3.
范围可以是指定提交更改位置的任何内容。例如$ location,$ browser,$ compile,$ rootScope,ngHref,ngClick,ngView等…
4. 提交信息的正文
- 动词打头
- 为什么变更,由于什么情况导致了修改等等
5.
重大变化的备注: 可以以应为 Breaking changes开头, 所有重大变更都必须在页脚中提及变更说明,理由和迁移说明
参考一下提交模版
模版1: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 availableBreaks $browser.onHashChange, which was removed (use onUrlChange instead)模版2:fix($compile): couple of unit tests for IE9Older IEs serialize html uppercased, but IE9 does not...Would be better to expect case insensitive, unfortunately jasmine doesnot allow to user regexps for throw expectations.Closes #392Breaks foo.bar api, foo.baz should be used instead模版3:feat(directive): ng:disabled, ng:checked, ng:multiple, ng:readonly, ng:selectedNew directives for proper binding these attributes in older browsers (IE).Added coresponding description, live examples and e2e tests.Closes #351模版4:style($location): add couple of missing semi colons模版5:docs(guide): updated fixed docs from Google DocsCouple of typos fixed:- indentation- batchLogbatchLog -> batchLog- start periodic checking- missing brace模版6:feat($compile): simplify isolate scope bindingsChanged the isolate scope binding options to:- @attr - attribute binding (including interpolation)- =model - by-directional model binding- &expr - expression execution bindingThis change simplifies the terminology as well asnumber of choices available to the developer. Italso supports local name aliasing from the parent.BREAKING CHANGE: isolate scope bindings definition has changed andthe 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: '&',// 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.
如何强制约定开发者按照规范添加提交记录?
引出对应的工具库, 讲解与commitlint配合使用的两个规范库,以下两个库会对以上changelog的配置规范做配置,通过commitlint在提交的时候做验证,也方便每次提交的时候成规范,具体的配置规范参考conventional-config
1. @commitlint/config-conventional
使用配置
# Install commitlint cli and conventional confignpm install --save-dev @commitlint/{config-conventional,cli}# For Windows:npm install --save-dev @commitlint/config-conventional @commitlint/cli# 配置 commitlint的文件,去添加 conventional 的配置: commitlint.config.jsmodule.exports = {extends: ['@commitlint/config-conventional']}
源码
module.exports = {parserPreset: 'conventional-changelog-conventionalcommits',rules: {'body-leading-blank': [1, 'always'],'body-max-line-length': [2, 'always', 100],'footer-leading-blank': [1, 'always'],'footer-max-line-length': [2, 'always', 100],'header-max-length': [2, 'always', 100],'scope-case': [2, 'always', 'lower-case'],'subject-case': [2,'never',['sentence-case', 'start-case', 'pascal-case', 'upper-case']],'subject-empty': [2, 'never'],'subject-full-stop': [2, 'never', '.'],'type-case': [2, 'always', 'lower-case'],'type-empty': [2, 'never'],'type-enum': [2,'always',['build','chore','ci','docs','feat','fix','perf','refactor','revert','style','test']]}};
2. @commitlint/config-angular
使用及配置
1. 安装npm install --save-dev @commitlint/config-angular @commitlint/cli2. commitlint.config.js文件配置module.exports = {extends: ['@commitlint/config-angular']};
源码
const typeEnum = require('@commitlint/config-angular-type-enum');module.exports = {rules: {'body-leading-blank': [1, 'always'],'footer-leading-blank': [1, 'always'],'header-max-length': [2, 'always', 72],'scope-case': [2, 'always', 'lower-case'],'subject-case': [2,'never',['sentence-case', 'start-case', 'pascal-case', 'upper-case']],'subject-empty': [2, 'never'],'subject-full-stop': [2, 'never', '.'],'type-case': [2, 'always', 'lower-case'],'type-empty': [2, 'never'],'type-enum': typeEnum.rules['type-enum']}};
依赖的工具
- @commitlint/cli: commit验证工具
- @commitlint/prompt-cli: 用于对规范通过提交的友好提示,完成最终的日志的生成
- commitizen adapter: 用于对规范通过提交的友好提示,完成最终的日志的生成,此中形式采用git cz
如果需要在commit提交的时候强制约定必须要符合 @commitlint/config-conventional 的提交规则,可以使用Husky的’commit-msg’挂钩。
在项目中安装npm install husky —save-dev 或 yarn add -D husky。
之后,您可以创建一个.huskyrc文件或将以下代码添加到package.json中:
{"commitlint": {"extends": ["@commitlint/config-conventional"(需要的依赖包添加)]},"husky": {"hooks": {"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}}}
changelog如何根据git commit 的提交记录生成?
将规范的commit提交记录生成为对应的changelog。我们就要请出 conventional-changelog-cli。
conventional-changelog-cli: 从git元数据生成变更日志
$ npm install -g conventional-changelog-cli$ conventional-changelog -p angular -i CHANGELOG.md -s
命令行执行: 生成所有以前的变更日志,则可以执行
conventional-changelog -p angular -i CHANGELOG.md -s -r 0
通过npm script命令行添加
{"scripts": {"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"}}
是否有提交的日志提示功能?
快速加入commit日志规范
生成一个新项目
yarn create semantic-module my-module-name
或从现有项目中运行:
cd ./my-module-nameyarn create semantic-module
如果使用npm 5.2.0或更高版本,则还可以使用npx:
npx create-semantic-module my-module-name
会提示你生成对应的模版,比如采用@commitlint/config-conventional规范及@commitlint/prompt 提示方式
整理手动搭建步骤
- npm install —save-dev @commitlint/cli @commitlint/prompt @commitlint/config-conventional commitizen
- npm install husky —save-dev
- 项目中新建 commitlint.config.js文件 ```javascript / eslint-env node /
module.exports = { extends: [‘@commitlint/config-conventional’],
// Add your own rules. See http://marionebl.github.io/commitlint rules: { } };
4. 下载日志生成 npm install -g conventional-changelog-cli5. 配置 package.json文件配置```javascript{"scripts": {"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md","commit": "git-cz"},// 如果安装的 @commitlint/prompt不是全局则需要配置"config": {"commitizen": {"path": "node_modules/@commitlint/prompt"}},"husky": {"hooks": {"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}},// 如果有commitlint.config.js则可以不用配置这个"commitlint": {"extends": ["@commitlint/config-conventional"]},}
与auto-changelog比较
https://www.npmjs.com/package/auto-changelog
参考
[1] 自动发布 semantic-release
[2] commitlint的使用规范
[2] conventional-changelog日志生成(taro)
[3] 统一制定changeLog的规范,参考 Angular Style Commit Message Conventions
[4] 新版本changelog 步骤
