跳过正文
Background Image
  1. Posts/

程序员自我修养之Git提交信息和分支创建规范(工具篇)

·1268 字·3 分钟· ·
沈显鹏
作者
沈显鹏
目录

Git 提交信息和 Git 分支命名规范是团队协作中非常重要的一部分,它们能够使代码库更加规范、易于维护和理解。

我们需要通过工具来帮助实现Git提交信息和分支创建规范,本篇将介绍如何使用 Commit Check 这个工具来验证提交信息、分支命名、提交用户名字、提交用户邮箱等是否符合规范。

更多关于Git提交信息和分支创建规范可以参看我之前发布的文章《程序员自我修养之Git提交信息和分支创建规范》,这里不再赘述。

Commit Check 简介
#

Commit Check 是一个可以检查 Git 提交信息,分支命名、提交者用户名、提交者邮箱等等。它是 Yet Another Commit Checker 的开源平替版。

Commit Check 的配置
#

使用默认设置
#

如果没有进行自定义设置,Commit Check 会使用默认设置。具体设置在此

默认设置中,提交信息遵循约定式提交,分支命名见分支模型详细信息

使用自定义配置
#

你可以在你的 Git 仓库下创建一个配置文件 .commit-check.yml 来自定义设置,例如:

checks:
  - check: message
    regex: '^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-\.]+\))?(!)?: ([\w ])+([\s\S]*)|(Merge).*|(fixup!.*)'
    error: "The commit message should be structured as follows:\n\n
    <type>[optional scope]: <description>\n
    [optional body]\n
    [optional footer(s)]\n\n
    More details please refer to https://www.conventionalcommits.org"
    suggest: please check your commit message whether matches above regex
  - check: branch
    regex: ^(bugfix|feature|release|hotfix|task)\/.+|(master)|(main)|(HEAD)|(PR-.+)
    error: "Branches must begin with these types: bugfix/ feature/ release/ hotfix/ task/"
    suggest: run command `git checkout -b type/branch_name`
  - check: author_name
    regex: ^[A-Za-z ,.\'-]+$|.*(\[bot])
    error: The committer name seems invalid
    suggest: run command `git config user.name "Your Name"`
  - check: author_email
    regex: ^\S+@\S+\.\S+$
    error: The committer email seems invalid
    suggest: run command `git config user.email yourname@example.com`

你可以根据自己的需要来修改 regex, error, suggest 的值。

Commit Check 使用
#

Commit Check 支持多种使用方式

以 GitHub Action 来运行
#

例如创建一个 GitHub Action workflow 文件 .github/workflows/commit-check.yml

name: Commit Check

on:
  push:
  pull_request:
    branches: 'main'

jobs:
  commit-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: commit-check-action@v1
        with:
          message: true
          branch: true
          author-name: true
          author-email: true
          dry-run: true
          job-summary: true

详细请参考 https://github.com/commit-check-action

以 pre-commit hook 运行
#

首选需要安装了 pre-commit

然后将如下设置添加到你的 .pre-commit-config.yaml 文件中。

-   repo: https://github.com/commit-check
    rev: the tag or revision
    hooks: # support hooks
    -   id: check-message
    -   id: check-branch
    -   id: check-author-name
    -   id: check-author-email

以命令行来运行
#

可以通过 pip 先安装

pip install commit-check

然后运行 commit-check --help 命令就可以查看如何使用了,具体可以参见文档

以 Git Hooks 来运行
#

要配置 Git Hooks ,你需要在 Git 存储库的 .git/hooks/ 目录中创建一个新的脚本文件。

例如 .git/hooks/pre-push,文件内容如下:

#!/bin/sh
commit-check --message --branch --author-name --author-email

并修改为可执行权限 chmod +x .git/hooks/pre-push,然后当你运行 git push 命令时,这个 push hook 会自动执行。

Commit Check 执行失败示例
#

检查提交信息失败

Commit rejected by Commit-Check.

  (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)
   / ._. \      / ._. \      / ._. \      / ._. \      / ._. \
 __\( C )/__  __\( H )/__  __\( E )/__  __\( C )/__  __\( K )/__
(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)
   || E ||      || R ||      || R ||      || O ||      || R ||
 _.' '-' '._  _.' '-' '._  _.' '-' '._  _.' '-' '._  _.' '-' '._
(.-./`\.-.)(.-./`\.-.)(.-./`\.-.)(.-./`\.-.)(.-./`\.-.)
 ``````````
Invalid commit message => test
It doesn't match regex: ^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-\.]+\))?(!)?: ([\w ])+([\s\S]*)

The commit message should be structured as follows:

<type>[optional scope]: <description>
[optional body]
[optional footer(s)]

More details please refer to https://www.conventionalcommits.org
Suggest: please check your commit message whether matches above regex

检查分支命名失败

Commit rejected by Commit-Check.

  (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)    (c).-.(c)
   / ._. \      / ._. \      / ._. \      / ._. \      / ._. \
 __\( C )/__  __\( H )/__  __\( E )/__  __\( C )/__  __\( K )/__
(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)(_.-/'-'\-._)
   || E ||      || R ||      || R ||      || O ||      || R ||
 _.' '-' '._  _.' '-' '._  _.' '-' '._  _.' '-' '._  _.' '-' '._
(.-./`\.-.)(.-./`\.-.)(.-./`\.-.)(.-./`\.-.)(.-./`\.-.)
 ``````````
Commit rejected.

Invalid branch name => test
It doesn't match regex: ^(bugfix|feature|release|hotfix|task)\/.+|(master)|(main)|(HEAD)|(PR-.+)

Branches must begin with these types: bugfix/ feature/ release/ hotfix/ task/
Suggest: run command `git checkout -b type/branch_name`

以上就是 Commit Check 的使用介绍了,更多新信息请参考 https://github.com/commit-check


转载本站文章请注明作者和出处,请勿用于任何商业用途。欢迎关注公众号「DevOps攻城狮」

相关文章

Error: Permission denied (publickey)
·276 字·1 分钟
本文介绍了如何在配置多个 SSH Git Key 时解决 “Permission denied (publickey)” 错误,确保 GitHub 和 Bitbucket 的 SSH 连接正常工作。
如何创建 GPG 密钥并添加到 GitHub
·307 字·1 分钟
本文介绍如何生成 GPG 密钥、导出公钥并将其添加到 GitHub,用于提交签名验证。
靠谱:在不删除和重建 GitHub 仓库的情况下与父(Fork)仓库分离(Unfork)
·693 字·2 分钟
本文介绍了如何通过 GitHub Support 实现与父仓库的分离,避免删除和重建仓库带来的数据丢失问题,帮助开发者更好地管理 Fork 的仓库。
Git 常用命令备忘录
·869 字·2 分钟
本文总结了 Git 的常用命令和技巧,帮助开发者快速查找和使用 Git 命令,提高工作效率。
解决在 AIX 上 Git Clone 失败的两个问题
·1279 字·3 分钟
本文记录了在 AIX 上使用 Jenkins 进行 Git Clone 时遇到的两个问题及其解决方法,包括依赖库加载失败和 SSH 认证失败。
通过解除文件资源限制:解决在 AIX 使用 Git 下载大容量仓库失败问题
·482 字·1 分钟
在 AIX 系统中遇到 Git 下载大容量仓库时因文件大小限制导致失败,通过修改 ulimit 设置解决问题。