一、理解Commitlint
Commitlint是一個用于規范化Git提交消息格式的工具。它基于Node.js,通過一系列的規則來檢查Git提交信息的格式,確保它們遵循預定義的標準。
1.1、Commitlint的核心功能
代碼規則檢查:Commitlint基于代碼規則進行檢查,這些規則可以自定義以適應項目的特定需求。例如,可以設置規則來要求所有的函數都有明確的參數列表等。
多種編程語言和框架支持:Commitlint支持多種編程語言和框架,如JavaScript、Python、React、Angular等,使其能夠適應各種項目需求。
可視化報告和警告:Commitlint提供可視化報告和警告,幫助團隊成員了解有關代碼問題的詳細信息。
1.2、Commitlint的工作流程
它在Git的commit-msg鉤子中運行,這意味著每次提交時,Commitlint都會自動檢查提交信息。
如果提交信息不符合規則,Commitlint會阻止提交并給出錯誤提示。
此外,Commitlint與Husky等工具可以配合使用,確保代碼在提交到版本控制系統之前經過檢查和驗證。
二、vue3 + ts 項目使用Commitlint
pnpm add @commitlint/cli @commitlint/config-conventional
pnpm add husky
.commitlintrc.js
module.exports = { extends: ['@commitlint/config-conventional'],
};
測試
提交成功
驗證通過
三、過程記錄
3.1、.commitlintrc.js
.commitlintrc.js
?是一個配置文件,用于 Commitlint,一個工具,用于幫助你在 Git 倉庫中強制執行一致的 commit message 格式。Commitlint 允許你定義規則,這些規則會在你嘗試提交一個新的 Git commit 時被檢查。
這個文件通常包含一個配置對象,該對象定義了你希望 Commitlint 遵循的規則。
以下是一個簡單的?.commitlintrc.js
?示例:
module.exports = { extends: ['@commitlint/config-conventional'], // 使用常規的 commitlint 配置文件 rules: { 'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert']], // 定義允許的提交類型 'scope-case': [2, 'always', 'lower-case'], // 強制提交范圍為小寫 'subject-case': [0], // 不強制主題大小寫(但通常建議使用 sentence-case) 'subject-empty': [2, 'never'], // 禁止空的提交主題 'subject-full-stop': [0, 'never'], // 不強制主題以句號結尾 'header-max-length': [2, 'always', 72], // 強制 header 最大長度為 72 個字符 'body-leading-blank': [1, 'always'], // 強制 body 前面有一個空行 'footer-leading-blank': [1, 'always'], // 強制 footer 前面有一個空行 'footer-max-line-length': [2, 'always', 80], // 強制 footer 的每行最大長度為 80 個字符 }, parserPreset: { parserOpts: { headerPattern: /^(\w*)(?:\(([\w$\.,\s]*?)\))?\s*:(.*)$/, // 自定義 header 的正則表達式 headerCorrespondence: ['type', 'scope', 'subject'], // 與 headerPattern 匹配的部分 }, },
};