我正在参加「掘金·启航计划」
通常在团队项目的开发流程中,作为组长或者说是项目负责人都需要为项目的后期做好充足的准备,例如在 Git 工作流中,我们需要统一团队中每个人的提交风格,因此要对Git Commit做一些定制化的需求。 接下来我们就通过commitzen、commitlint等工具来约束 commit 规范。
Commit 定制化
通过 commitzen 插件,可以帮助我们来实现 Git commit 得统一性,只不过它是一个比较基础的插件,社区还有更多功能增强的插件。例如:
- cz-customizable:可以自定义 Git commit,通过配置文件引导用户进行 commit 操作。
- commitizen-emoji:带有 emoji 表情的 Git Commit,但是不能自定义配置,只能使用固定的格式。
为了定制化,我们使用 cz-customizable 来配置属于我们自己项目的 Commit 规范。
安装
pnpm add -D cz-customizable
配置
-
在项目更目录下创建
.cz-config.js。如果你是 TypeScript 项目,使用 module 模式,创建.cz-config.cjsmodule.exports = { // 引导用户输入的提示信息 types: [ { value: ":rocket: initial", name: "🎉 initial: 初始化项目" }, { value: ":construction: wip", name: "🚧 wip: 工作进行中" }, { value: ":sparkles: feat", name: "✨ feat: 新增一个功能" }, { value: ":bug: fix", name: "🐛 fix: 修复一个Bug" }, { value: ":hammer: refactor", name: "🔨 refactor: 重构(既不修复bug也不添加特性的代码更改)", }, { value: ":pencil: docs", name: "📝 docs: 文档变更" }, { value: ":white_check_mark: test", name: "✅ test: 添加缺失的测试或更正现有的测试", }, { value: ":thought_balloon: chore", name: "🗯 chore: 构建过程或辅助工具的变动", }, { value: "revert", name: "⏪ revert: 代码回退" }, { value: ":zap: perf", name: "⚡️ perf: 提升性能" }, { value: ":lipstick: ui", name: "💄 ui: 更新UI和样式" }, { value: ":art: style", name: "🎨 style: 改进代码结构/代码格式" }, { value: ":truck: mv", name: "🚚 mv: 移动重命名文件" }, { value: ":fire: delte", name: "🔥 delte: 删除文件" }, { value: ":fire: up", name: "⬆️ up: 升级依赖" }, { value: ":fire: down", name: "⬇️ down: 降级依赖" }, { value: ":whale: docker", name: "🐳 ocker: docker相关" }, { value: ":bookmark: tag", name: "🔖 tag: 发行/版本标签" }, { value: ":ambulance: patch", name: "🚑 patch: 重要补丁" }, ], // 选择scope的提示信息 messages: { type: "请选择您要提交的类型:", scope: "请输入修改范围(可选):", // allowCustomScopes为true时使用 customScope: "请输入文件修改范围(可选):", subject: "请简要描述提交(必选):", body: "请输入详细描述,使用'|'换行(可选):", breaking: "列出任何突破性的变化(可选)", footer: "请输入要关闭的issue(可选)。例:#31,#34:", confirmCommit: "您确定要继续执行上面的提交吗?", }, scopes: [ "user", "login", "home", "order", "product", "cart", "address", "pay", "coupon", "search", "category", "detail", "other", ], // 跳过某些问题 skipQuestions: [], allowCustomScopes: true, allowBreakingChanges: ["feat", "fix"], subjectLimit: 100, }; -
在 package.json 中添加如下内容。
{ "script": { /* ...... */ + "commit": "cz-customizable" }, + "config": { + "cz-commitizen": { + "path": "cz-customizable" + } } } -
用法
git add ./src/login pnpm commit
拦截不符合规范的 Commit
目前为止,我们已经约束好啦 Commit 规范,但是开发者还是开始可以通过git commit命令提交约束之外的消息。这也使得我们的规范失去了意义。因此需要在开发者 commit 的时候对其提交消息做一次拦截验证,只有符合规范的才能被正确的提交到 Git 仓库,从而 push 到远端。而 husky 则是专门做这种操作的,它支持所有的 Git 钩子,在触发钩子之前做一些额外的事情,例如 commit 校验、运行测试等等。
因为我们定制的 commit 是以 emoji 开头的,而 commitlint 中并没有提供匹配 emoji 开头的校验包,因此需要接入别人已经写好的commitlint-config-gitmoji来完成 commit 检验。
- commitlint:检查你的提交消息是否符合常规的提交格式。
- husky:当你提交或推送时,使用它来整理你的提交信息、运行测试、lint 代码等。
- commitlint-config-gitmoji:当你的提交信息是以 emoji 开头的,通过高它来校验。
-
安装
pnpm add -D @commitlint/cli commitlint-config-gitmoji pnpm dlx husky-init && pnpm install执行成功后,会在项目更目录下生成
.husky的一个文件夹,里面包含一些可执行脚本。例如
pre-commit,表示在 commit 之前,需要做的一些事情。(例如:运行pnpm run test,如果失败,终止提交。) -
添加 commilint 的 husky 钩子。
pnpm dlx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"' -
配置
commitlint.config.js,在项目更目录下创建。module.exports = { extends: ["gitmoji"], rules: { "type-enum": [ 2, "always", [ "initial", "build", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test", "chore", "wip", "mv", "delete", "ui", "up", "down", "docker", ], ], }, }; -
测试效果:如果你通过
git commit提交不规范的信息就会被拦截。