使用 gitlint 让 commit message 服从 conventional 规范

使用 gitlint 让 commit message 服从 conventional 规范

起因

看了阮一峰的文章 Commit message 和 Change log 编写指南,认同 git commit message 应该规范化的观点;但用来检查是否符合规范的工具,阮提到 commitzen,要装 node 环境,以及把 git commit 替换为 git cz,着实麻烦。

考虑到 git 的 hook 机制,可以基于 commitlint 配置,能继续用 git commit 命令;然而配置过程还是很繁琐,yml 和 package.json 的写法难以一下写对。

相比之下, gitlint 则简单了许多:用 pip 而不是 nodejs 的 npm 安装;编写 .gitlint 的说明文档一看就懂。尝试配置如下。

配置

安装 gitlint:

pip install gitlint

找到一个 repo,要求至少有一次 commit,例如新建的项目:

mkdir -p ~/work/test/hhh
cd $_
git init
touch README
git add .
git commit -m "Initial commit"

添加 .gitlint 文件,内容:

[general]
# You HAVE to add the rule here to enable it, only configuring (such as below)
# does NOT enable it.
contrib=contrib-title-conventional-commits,CT1
ignore=B6

添加 gitlint 到 git 的 hook 中:

gitlint install-hook

接下来执行 git commit 的时候,都会按照 CT1 规则检查 commit 信息中的 title 是否符合 conventionalcommits 规范。更多 gitlint 配置规则见官方文档

posted @ 2021-01-14 14:58  ChrisZZ  阅读(1752)  评论(0编辑  收藏  举报