在 VS Code 中规范化 Git 提交消息并自动生成 CHANGELOG.md

发布于:2024-11-03 ⋅ 阅读:(135) ⋅ 点赞:(0)

1. 使用 Commitizen 规范化 Git 提交消息

首先,安装 Commitizen 和适用于 Angular 提交规范的适配器:

npm install -g commitizen
commitizen init cz-conventional-changelog --save-dev --save-exact

这样,提交信息将遵循 Angular 规范,比如 feat(新功能)、fix(修复)、docs(文档)、style(格式)、refactor(重构)、test(测试)、chore(杂务)等格式。

2. 使用 Husky 和 Commitlint 验证提交消息

安装 Husky 和 Commitlint:

npm install husky @commitlint/config-conventional @commitlint/cli --save-dev

在项目根目录创建 commitlint.config.js 配置文件:

// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional'],
};

接着,设置 Husky 钩子来验证提交消息:

npx husky install
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

3. 使用 standard-version 自动生成 CHANGELOG.md

standard-version 是一种用于版本管理的工具,可以基于提交历史自动生成和更新 CHANGELOG.md 文件。

安装 standard-version

npm install standard-version --save-dev

package.json 文件中添加脚本命令:

{
  "scripts": {
    "release": "standard-version"
  }
}

运行以下命令生成或更新 CHANGELOG.md

npm run release

每次运行 npm run releasestandard-version 会分析提交历史,根据提交类型(如 featfix)更新 CHANGELOG.md,并自动更新 package.json 中的版本号。

4. 配置 VS Code 提交流程

.vscode/settings.json 中设置如下选项,以在每次提交时自动使用 Commitizen:

{
  "git.enableCommitSigning": true,
  "git.postCommitCommand": "commitizen"
}

这样每次提交时,会自动引导用户使用 Commitizen 格式提交信息,从而保持提交信息的规范性。

总结

完成以上配置后,可以使用如下流程:

  1. 使用 git cz 替代 git commit 提交规范化的消息。
  2. 通过 npm run release 自动生成或更新 CHANGELOG.md 文件。

这样,Git 提交信息的规范性和变更日志自动化生成都可以在 VS Code 中高效实现。


网站公告

今日签到

点亮在社区的每一天
去签到