Flutter代码提交规范插件commit_lint的使用
Flutter代码提交规范插件commit_lint的使用
什么是commit lint
Commit lint 检查你的提交信息是否符合常规提交格式。
通常情况下,这种格式大致如下所示:
type(scope?): subject # scope 是可选的;支持多个范围(当前分隔符选项为:"/"、"\" 和 ",")
真实的例子可能如下所示:
chore: run tests on travis ci
fix(server): send cors headers
feat(blog): add comment section
常见的类型包括:
- build
- ci
- chore
- docs
- feat
- fix
- perf
- refactor
- revert
- style
- test
受 TS commitlint 启发。
使用示例
安装commit_lint插件
首先,你需要在项目中安装 commit_lint
插件。你可以通过以下命令进行安装:
flutter pub add commit_lint
配置commit_lint规则
接下来,你需要配置commit_lint的规则。你可以在项目的根目录下创建一个名为 .commitlintrc.json
的文件,并添加以下内容:
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"subject-case": [2, "always", "sentence-case"]
}
}
在这个配置文件中,extends
属性指定了要使用的预定义规则集,这里我们使用了 @commitlint/config-conventional
规则集。rules
属性定义了自定义的规则,例如 subject-case
规则用于确保提交信息的主题部分始终以句子大小写形式出现。
创建Git Hook
为了确保每次提交时都会运行commit_lint检查,你需要创建一个Git Hook。你可以在项目的根目录下创建一个名为 prepare-commit-msg
的脚本文件,并添加以下内容:
#!/bin/bash
# 运行commit_lint检查
npx commitlint --edit $1
然后,将该脚本设置为可执行:
chmod +x prepare-commit-msg
提交代码
现在,当你尝试提交代码时,commit_lint会自动检查你的提交信息是否符合规定的格式。如果不符合,它将阻止你完成提交并提示你修改。
例如,如果你尝试提交一条不符合规则的信息,比如:
git commit -m "This is an invalid commit message"
commit_lint将会输出错误信息,并提示你修改提交信息。
符合规则的提交
为了确保提交信息符合规则,你可以按照以下格式进行提交:
git commit -m "feat(blog): add comment section"
更多关于Flutter代码提交规范插件commit_lint的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter代码提交规范插件commit_lint的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
commit_lint
是一个用于 Flutter 项目的代码提交规范检查工具,它可以帮助团队规范 Git 提交信息的格式,确保提交信息符合一定的规范要求。通常,它基于 Conventional Commits 规范,这使得提交信息更加清晰、可读,并且可以自动生成 CHANGELOG。
安装 commit_lint
-
在项目中添加
commit_lint
依赖:首先,你需要在项目的
pubspec.yaml
文件中添加commit_lint
依赖。dev_dependencies: commit_lint: ^1.0.0
然后运行
flutter pub get
来安装依赖。 -
配置
commit_lint
:在项目的根目录下创建一个
commit_lint.yaml
文件,用于配置提交信息的校验规则。types: - feat - fix - docs - style - refactor - test - chore - perf subject_min_length: 5 subject_max_length: 72 body_min_length: 10 body_max_length: 100
在这个配置文件中,你可以定义允许的提交类型 (
types
)、主题的最小和最大长度 (subject_min_length
和subject_max_length
) 以及正文的最小和最大长度 (body_min_length
和body_max_length
)。
使用 commit_lint
-
手动运行
commit_lint
:你可以在命令行中手动运行
commit_lint
来检查提交信息。flutter pub run commit_lint
这将检查项目中的提交信息是否符合规范。
-
与 Git Hooks 集成:
为了确保每次提交时自动进行校验,你可以将
commit_lint
集成到 Git 的commit-msg
hook 中。-
首先,在项目的
.git/hooks
目录下创建一个commit-msg
文件(如果不存在的话)。 -
然后在
commit-msg
文件中添加以下内容:#!/bin/sh flutter pub run commit_lint "$1"
-
确保
commit-msg
文件有可执行权限:chmod +x .git/hooks/commit-msg
这样,每次执行
git commit
时,commit_lint
都会自动检查提交信息是否符合规范。 -
提交信息格式
根据 commit_lint
的配置,提交信息应遵循以下格式:
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
- type: 提交的类型,如
feat
,fix
,docs
,style
,refactor
,test
,chore
,perf
等。 - scope: 可选的,表示影响的范围或模块。
- subject: 提交的简短描述,通常不超过 72 个字符。
- body: 可选的,提交的详细描述。
- footer: 可选的,通常用于关联 issue 或 breaking changes。
示例
feat(login): add user authentication
Add user authentication using Firebase. This feature allows users to sign in with their email and password.
Closes #123