前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Git】:Commit规范 + CHANGELOG生成

【Git】:Commit规范 + CHANGELOG生成

作者头像
WEBJ2EE
发布2020-12-02 12:29:45
4.1K0
发布2020-12-02 12:29:45
举报
文章被收录于专栏:WebJ2EEWebJ2EE
代码语言:javascript
复制
目录
1. 这种开发日常,你有过么?
2. 业界怎么做?
    2.1. 规范化 git commit 的 message
    2.2. 交互式 commit message 生成
    2.3. 强制校验 commit message 格式
    2.4. 自动生成 CHANGELOG
3. 综合示例

1. 这种开发日常,你有过么?

2. 业界怎么做?

2.1. 规范化 git commit 的 message

从 git commit 的 message 开始进行规范化(主流:angular 规范),进而可以通过工具(例如:conventional-changelog)把关键信息找出来,并自动生成到 CHANGELOG 中。

规范节选

Each commit message consists of a header, a body, and a footer.

代码语言:javascript
复制
<header>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
  • The header is mandatory and must conform to the Commit Message Header format.
  • The body is mandatory for all commits except for those of type "docs". When the body is present it must be at least 20 characters long and must conform to the Commit Message Body format.
  • The footer is optional. The Commit Message Footer format describes what the footer is used for and the structure it must have.
  • Any line of the commit message cannot be longer than 100 characters.
Commit Message Header
代码语言:javascript
复制
<type>(<scope>): <short summary>
  │       │             │
  │       │             └─⫸ Summary in present tense. Not capitalized. No period at the end.
  │       │
  │       └─⫸ Commit Scope: The scope should be the name of the component affected
  │                           (as perceived by the person reading the 
  │                          changelog generated from commit messages).
  │
  └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test

The<type> and <summary> fields are mandatory, the (<scope>) field is optional.

Type

Must be one of the following:

  • build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
  • ci: Changes to our CI configuration files and scripts (example scopes: Circle, BrowserStack, SauceLabs)
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: 回滚某个更早之前的提交
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • test: Adding missing tests or correcting existing tests
  • chore: 不属于以上类型的其他类型(日常事务)
Scope

The scope should be the name of the npm package affected (as perceived by the person reading the changelog generated from commit messages).

规范示例

2.2. 交互式 commit message 生成

commitizen 是一款可以交互式建立提交信息的工具。它帮助我们从 type 开始一步步建立提交信息。

2.3. 强制校验 commit message 格式

在日常开发中,为保证小伙伴们都能按照规范书写 commit message,我们可以使用 commitlint + husky 的方式强制推行规范。原理是在实际的 git commit 提交到远程仓库之前使用 git 钩子来验证信息,将阻止不符合规则的信息提交到远程仓库。

  • commitlint 可以帮助检查我们的 commit message 是否符合格式规范。
  • huskey:Git hooks made easy.
    • You can use it to lint your commit messages, run tests, lint code, etc... when you commit or push. Husky supports all Git hooks.

2.4. 自动生成 CHANGELOG

如果你的所有 Commit 都符合规范,那么发布新版本时,可以使用工具(例如:conventional-changelog)自动生 CHANGELOG。

CHANGELOG 示例

3. 综合示例

  • 初始化 git 仓库、初始化 npm 项目
代码语言:javascript
复制
git init
npm init
  • 安装依赖
    • @commitlint/cli、@commitlint/config-conventional
      • commit message 格式校验工具,验证 message 格式是否符合规范。
    • husky
      • git钩子,用于在 commit 的时候,能调用 commitlint 校验 commit message
    • conventional-changelog-cli、conventional-changelog
      • 根据 git 提交历史,自动生成 CHANGELOG 工具。
代码语言:javascript
复制
npm install --save-dev @commitlint/cli @commitlint/config-conventional
npm install --save-dev conventional-changelog conventional-changelog-cli
npm install --save-dev husky
  • 配置 commitlint(commitlint.config.js)
代码语言:javascript
复制
module.exports = {
  extends: ['@commitlint/config-conventional']
};
  • 配置 git 钩子
代码语言:javascript
复制
{
  "husky": {
    "hooks": {
      "commit-msg": "npx commitlint -E HUSKY_GIT_PARAMS"
    }
  }
}
  • 配置 CHANGELOG 自动生成
代码语言:javascript
复制
  "scripts": {
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
  },
  • 执行几次提交
代码语言:javascript
复制
chore(init): init npm & git repo
fix(cli): exit CLI with 1 when received SIGINT
docs(readme): specify environment in code blocks
feat(core): use cz-conventional-changelog as default adapter
refactor!: drop support for Node 6

代码语言:javascript
复制
feat: allow provided config object to extend other configs

BREAKING CHANGE: `extends` key in config file is now used for extending other config files
代码语言:javascript
复制
refactor!: drop support for Node 6

BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.
  • 生成CHANGELOG
代码语言:javascript
复制
npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0

参考:

commit message 格式规范:angular 规范 https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines commit message 格式规范: https://www.conventionalcommits.org/en/v1.0.0/#specification 交互式 commit message 书写工具:commitizen https://github.com/commitizen/cz-cli commit message 校验工具:commitlint https://github.com/conventional-changelog/commitlint Git钩子辅助: https://typicode.github.io/husky/#/ CHANGELOG 生成工具:conventional-changelog https://github.com/conventional-changelog/conventional-changelog


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-11-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WebJ2EE 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Commit Message Header
    • Type
      • Scope
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档