# commitlint模板 **Repository Path**: vkrain/commitlint-template ## Basic Information - **Project Name**: commitlint模板 - **Description**: 帮助你理解git commit如何去约束提交信息 从终端交互创建提交信息,到提交信息校验的流程 commitlint/cli+commitizen+cz-git+husky - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-10-11 - **Last Updated**: 2024-10-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 约束git commit ## 前言 通过这篇文章,你将了解到git commit的约束规则,以及如何配置git commit的约束规则。 让你理清 - commitzen是用于提交信息生成的工具 - commitlint/cli是用于提交信息检查的工作 ## 一、Commitizen生成规范-配置交互 简单来说,`Commitizen` 是规范的核心,而 `commitizen/cli` 是帮助你执行这个规范的工具,这样说的原因是commitizen/cli的使用不依赖Commitizen,类似于vue 和vue cli的关系 **一句话理解关系** Commitizen + Commitizen Adapter == 支持自定义的Commitizen/cli交互界面 安装Commitizen 就有命令git-cz 安装Commitizen Adapter后执行git-cz就能终端交互 安装Commitizen/cli 就有命令commitizen-cli 01、只使用commitizen/cli不依赖Commitizen工作,只需要配置package.json ```js "scripts": { "commit": "commitizen-cli" // commitizen-cli工具进行交互 }, ``` 02、Commitizen + 适配器,生成规范,这个适配器会依赖Commitizen工作 ```bash # 安装cz-conventional-changelog npn install -D cz-conventional-changelog ``` package.json配置 ```json "scripts": { "commit": "git-cz" // commitizen工具进行交互 }, // 如果没有配置commitizen的适配器的path就会使用系统的编辑器 "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } } ``` 以上除了scripts需要配置,均可一键安装并且初始化配置 ``` npx commitizen init cz-conventional-changelog ``` ---- **结论** 为什么要区分Commitizen和Commitizen/cli,是因为使用场景,Commitizen/cli不依赖项目,可以当做工具来使用,使用npx commitizen-cli命令 这样你在没有Commitizen的情况下,可以直接帮助你创建规范提交,缺点是没法自定义 ```json "scripts": { "commit": "npx commitizen-cli", // commitizen-cli工具进行交互 }, ``` 如果适配器不适合,想高度自定义,就得使用cz-customizable,让键入信息,流程都自定义 ### 新的方案 将适配器改为[cz-git](https://cz-git.qbb.sh/zh/guide/introduction) ``` npm install commitizen -D npx commitizen init cz-git ``` ```json "scripts": { "commit": "git-cz" // commitizen工具进行交互 }, ``` ## 二、commitlint检查规范-配置规则 commitlint.config.js,制定规则检查这些提交信息, ``` `commitlint.config.js` 配置 `rules`: 定义了自定义规则:`@commitlint/cli` 会使用规则 `prompt`: 定义了交互式命令行界面的配置:`commitizen`会使用模板 // 继承的规则 extends: ["@commitlint/config-conventional"], ``` ### 2.1搭配cz-conventional-changelog的普通方式 ```js module.exports = { extends: ['@commitlint/config-angular'], rules: { // 自定义规则 }, }; ``` ### 2.2搭配cz-git使用的commitlint.config.js ```js // .commitlintrc.js /** @type {import('cz-git').UserConfig} */ module.exports = { // @commitlint/config-angular和rules二选一好 // extends: ['@commitlint/config-angular'], rules: { // @see: https://commitlint.js.org/#/reference-rules 'header-max-length': [2, 'always', 100], 'type-enum': [ 2, 'always', [ ':sparkles: feat', ':bug: fix', ':memo: docs', ':lipstick: style', ':recycle: refactor', ':zap: perf', ':white_check_mark: test', ':package: build', ':construction_worker: ci', ':wrench: chore', ':rewind: revert' ] ], 'subject-case': [0, 'always'], 'type-case': [0, 'always'], }, prompt: { alias: { fd: 'docs: fix typos' }, messages: { type: '选择你要提交的类型 :', scope: '选择一个提交范围(可选):', customScope: '请输入自定义的提交范围 :', subject: '填写简短精炼的变更描述 :\n', body: '填写更加详细的变更描述(可选)。使用 "|" 换行 :\n', breaking: '列举非兼容性重大的变更(可选)。使用 "|" 换行 :\n', footerPrefixesSelect: '选择关联issue前缀(可选):', customFooterPrefix: '输入自定义issue前缀 :', footer: '列举关联issue (可选) 例如: #31, #I3244 :\n', confirmCommit: '是否提交或修改commit ?' }, types: [ { value: 'feat', name: 'feat: ✨ 新增功能 | A new feature', emoji: ":sparkles:" }, { value: 'fix', name: 'fix: 🐛 修复缺陷 | A bug fix' }, { value: 'docs', name: 'docs: 📝 文档更新 | Documentation only changes' }, { value: 'style', name: 'style: 💄 代码格式 | Changes that do not affect the meaning of the code' }, { value: 'refactor', name: 'refactor: ♻️ 代码重构 | A code change that neither fixes a bug nor adds a feature' }, { value: 'perf', name: 'perf: ⚡️ 性能提升 | A code change that improves performance' }, { value: 'test', name: 'test: ✅ 测试相关 | Adding missing tests or correcting existing tests' }, { value: 'build', name: 'build: 📦️ 构建相关 | Changes that affect the build system or external dependencies' }, { value: 'ci', name: 'ci: 🎡 持续集成 | Changes to our CI configuration files and scripts' }, { value: 'revert', name: 'revert: ⏪️ 回退代码 | Revert to a commit' }, { value: 'chore', name: 'chore: 🔨 其他修改 | Other changes that do not modify src or test files' }, ], useEmoji: true, emojiAlign: 'left', useAI: false, aiNumber: 1, themeColorCode: '', scopes: [], allowCustomScopes: true, allowEmptyScopes: true, customScopesAlign: 'bottom', customScopesAlias: 'custom', emptyScopesAlias: 'empty', upperCaseSubject: false, markBreakingChangeMode: false, allowBreakingChanges: ['feat', 'fix'], breaklineNumber: 100, breaklineChar: '|', skipQuestions: [], issuePrefixes: [ // 如果使用 gitee 作为开发管理 { value: 'link', name: 'link: 链接 ISSUES 进行中' }, { value: 'closed', name: 'closed: 标记 ISSUES 已完成' } ], customIssuePrefixAlign: 'top', emptyIssuePrefixAlias: 'skip', customIssuePrefixAlias: 'custom', allowCustomIssuePrefix: true, allowEmptyIssuePrefix: true, confirmColorize: true, scopeOverrides: undefined, defaultBody: '', defaultIssues: '', defaultScope: '', defaultSubject: '' } } ``` ## 三、执行检查-husk执行 Husky配置**commit-msg**: ``` npx --no-install commitlint --edit $1 // 在新版的 husky 中 $HUSKY_GIT_PARAMS 变量已不再使用,取而代之是 $1 ```