简介

为了更好的维护commit的提交日志,更规范的展示对应的修改信息及变更体系,参考taro的changelog日志管理体系,引用conventional-changelog日志生成(taro);
日志管理带着问题去一步一步的展开

  1. 日志的规范如何定义?
  2. 如何强制约定开发者按照规范添加提交记录?
  3. changelog如何根据git commit 的提交记录生成?
  4. 是否有提交的日志提示功能?

日志的规范如何定义?

这些规则是从AngularJS提交约定中采用的。 Angular Style Commit Message Conventions

目的(Goals)

  1. 允许通过脚本生成CHANGELOG.md
  2. 允许忽略git bisect的提交(不重要的提交,例如格式化)
  3. 在生成的changelog文件中提供更有效的提交信息

生成CHANGELOG.md

我们在展示日志中使用以下三个部分:新功能(feat),错误修复(fix),重大更改()。发布时,此列表可以由脚本生成。以及相关提交的链接(对应commit提交id号链接的日志)。当然,在通过自动化工具生成之后,可以更改后在上传。
具体的生成对应log日志,使用了git的命令为(目前此操作可以用”conventional-changelog-cli”生成),如果对具体的生成原理感兴趣可以参考
格式化出到当前最后一次提交的所有日志的头

  1. git log HEAD —pretty=format:%s

过滤出已feature头的日志

  1. git log HEAD —grep feature

    识别不重要的提交

  2. git bisect skip $(git rev-list —grep irrelevant HEAD)


提交文档规范的约定 (Format of the commit message)

观察一下规范包括的属性: type、scope、 subject、 BLANK LINE(body的前空行及后空行)、footer; 看到的样式基本就是符合这个模版, 严格控制具体的展示。接下来我们就来具体分析不同的类型有那些属性及对应值:

⚠️ 提交消息的任何一行都不能超过100个字符!这使得消息在github以及各种git工具中更易于阅读。

  1. <type>(<scope>): <subject>
  2. <BLANK LINE>
  3. <body>
  4. <BLANK LINE>
  5. <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.

  1. 重大变化的备注: 可以以应为 Breaking changes开头, 所有重大变更都必须在页脚中提及变更说明,理由和迁移说明

参考一下提交模版

  1. 模版1:
  2. feat($browser): onUrlChange event (popstate/hashchange/polling)
  3. Added new event to $browser:
  4. - forward popstate event if available
  5. - forward hashchange event if popstate not available
  6. - do polling when neither popstate nor hashchange available
  7. Breaks $browser.onHashChange, which was removed (use onUrlChange instead)
  8. 模版2:
  9. fix($compile): couple of unit tests for IE9
  10. Older IEs serialize html uppercased, but IE9 does not...
  11. Would be better to expect case insensitive, unfortunately jasmine does
  12. not allow to user regexps for throw expectations.
  13. Closes #392
  14. Breaks foo.bar api, foo.baz should be used instead
  15. 模版3:
  16. feat(directive): ng:disabled, ng:checked, ng:multiple, ng:readonly, ng:selected
  17. New directives for proper binding these attributes in older browsers (IE).
  18. Added coresponding description, live examples and e2e tests.
  19. Closes #351
  20. 模版4:
  21. style($location): add couple of missing semi colons
  22. 模版5:
  23. docs(guide): updated fixed docs from Google Docs
  24. Couple of typos fixed:
  25. - indentation
  26. - batchLogbatchLog -> batchLog
  27. - start periodic checking
  28. - missing brace
  29. 模版6:
  30. feat($compile): simplify isolate scope bindings
  31. Changed the isolate scope binding options to:
  32. - @attr - attribute binding (including interpolation)
  33. - =model - by-directional model binding
  34. - &expr - expression execution binding
  35. This change simplifies the terminology as well as
  36. number of choices available to the developer. It
  37. also supports local name aliasing from the parent.
  38. BREAKING CHANGE: isolate scope bindings definition has changed and
  39. the inject option for the directive controller injection was removed.
  40. To migrate the code follow the example below:
  41. Before:
  42. scope: {
  43. myAttr: 'attribute',
  44. myBind: 'bind',
  45. myExpression: 'expression',
  46. myEval: 'evaluate',
  47. myAccessor: 'accessor'
  48. }
  49. After:
  50. scope: {
  51. myAttr: '@',
  52. myBind: '@',
  53. myExpression: '&',
  54. // myEval - usually not useful, but in cases where the expression is assignable, you can use '='
  55. myAccessor: '=' // in directive's template change myAccessor() to myAccessor
  56. }
  57. 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

使用配置

  1. # Install commitlint cli and conventional config
  2. npm install --save-dev @commitlint/{config-conventional,cli}
  3. # For Windows:
  4. npm install --save-dev @commitlint/config-conventional @commitlint/cli
  5. # 配置 commitlint的文件,去添加 conventional 的配置: commitlint.config.js
  6. module.exports = {extends: ['@commitlint/config-conventional']}

源码

  1. module.exports = {
  2. parserPreset: 'conventional-changelog-conventionalcommits',
  3. rules: {
  4. 'body-leading-blank': [1, 'always'],
  5. 'body-max-line-length': [2, 'always', 100],
  6. 'footer-leading-blank': [1, 'always'],
  7. 'footer-max-line-length': [2, 'always', 100],
  8. 'header-max-length': [2, 'always', 100],
  9. 'scope-case': [2, 'always', 'lower-case'],
  10. 'subject-case': [
  11. 2,
  12. 'never',
  13. ['sentence-case', 'start-case', 'pascal-case', 'upper-case']
  14. ],
  15. 'subject-empty': [2, 'never'],
  16. 'subject-full-stop': [2, 'never', '.'],
  17. 'type-case': [2, 'always', 'lower-case'],
  18. 'type-empty': [2, 'never'],
  19. 'type-enum': [
  20. 2,
  21. 'always',
  22. [
  23. 'build',
  24. 'chore',
  25. 'ci',
  26. 'docs',
  27. 'feat',
  28. 'fix',
  29. 'perf',
  30. 'refactor',
  31. 'revert',
  32. 'style',
  33. 'test'
  34. ]
  35. ]
  36. }
  37. };

2. @commitlint/config-angular

使用及配置

  1. 1. 安装
  2. npm install --save-dev @commitlint/config-angular @commitlint/cli
  3. 2. commitlint.config.js文件配置
  4. module.exports = {extends: ['@commitlint/config-angular']};

源码

  1. const typeEnum = require('@commitlint/config-angular-type-enum');
  2. module.exports = {
  3. rules: {
  4. 'body-leading-blank': [1, 'always'],
  5. 'footer-leading-blank': [1, 'always'],
  6. 'header-max-length': [2, 'always', 72],
  7. 'scope-case': [2, 'always', 'lower-case'],
  8. 'subject-case': [
  9. 2,
  10. 'never',
  11. ['sentence-case', 'start-case', 'pascal-case', 'upper-case']
  12. ],
  13. 'subject-empty': [2, 'never'],
  14. 'subject-full-stop': [2, 'never', '.'],
  15. 'type-case': [2, 'always', 'lower-case'],
  16. 'type-empty': [2, 'never'],
  17. 'type-enum': typeEnum.rules['type-enum']
  18. }
  19. };

依赖的工具

  1. @commitlint/cli: commit验证工具
  2. @commitlint/prompt-cli: 用于对规范通过提交的友好提示,完成最终的日志的生成
  3. commitizen adapter: 用于对规范通过提交的友好提示,完成最终的日志的生成,此中形式采用git cz

如果需要在commit提交的时候强制约定必须要符合 @commitlint/config-conventional 的提交规则,可以使用Husky的’commit-msg’挂钩。
在项目中安装npm install husky —save-dev 或 yarn add -D husky。
之后,您可以创建一个.huskyrc文件或将以下代码添加到package.json中:

  1. {
  2. "commitlint": {
  3. "extends": [
  4. "@commitlint/config-conventional"(需要的依赖包添加)
  5. ]
  6. },
  7. "husky": {
  8. "hooks": {
  9. "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  10. }
  11. }
  12. }

changelog如何根据git commit 的提交记录生成?

将规范的commit提交记录生成为对应的changelog。我们就要请出 conventional-changelog-cli。

conventional-changelog-cli: 从git元数据生成变更日志

  1. $ npm install -g conventional-changelog-cli
  2. $ conventional-changelog -p angular -i CHANGELOG.md -s

命令行执行: 生成所有以前的变更日志,则可以执行

  1. conventional-changelog -p angular -i CHANGELOG.md -s -r 0

通过npm script命令行添加

  1. {
  2. "scripts": {
  3. "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
  4. }
  5. }

是否有提交的日志提示功能?

  1. commitizen adapter
  2. prompt

快速加入commit日志规范

  1. 生成一个新项目

    1. yarn create semantic-module my-module-name
  2. 或从现有项目中运行:

    1. cd ./my-module-name
    2. yarn create semantic-module
  1. 如果使用npm 5.2.0或更高版本,则还可以使用npx:

    1. npx create-semantic-module my-module-name
  2. 会提示你生成对应的模版,比如采用@commitlint/config-conventional规范及@commitlint/prompt 提示方式

整理手动搭建步骤

  1. npm install —save-dev @commitlint/cli @commitlint/prompt @commitlint/config-conventional commitizen
  2. npm install husky —save-dev
  3. 项目中新建 commitlint.config.js文件 ```javascript / eslint-env node /

module.exports = { extends: [‘@commitlint/config-conventional’],

// Add your own rules. See http://marionebl.github.io/commitlint rules: { } };

  1. 4. 下载日志生成 npm install -g conventional-changelog-cli
  2. 5. 配置 package.json文件配置
  3. ```javascript
  4. {
  5. "scripts": {
  6. "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
  7. "commit": "git-cz"
  8. },
  9. // 如果安装的 @commitlint/prompt不是全局则需要配置
  10. "config": {
  11. "commitizen": {
  12. "path": "node_modules/@commitlint/prompt"
  13. }
  14. },
  15. "husky": {
  16. "hooks": {
  17. "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  18. }
  19. },
  20. // 如果有commitlint.config.js则可以不用配置这个
  21. "commitlint": {
  22. "extends": [
  23. "@commitlint/config-conventional"
  24. ]
  25. },
  26. }

与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 步骤