git commit msg格式规范校验

1. commit规范

每个公司的规范都是不同的,这里讲下我常用的规范
type(模块): message

type

用于说明 commit的类别,只允许使用下面7个标识。

  • feat:新功能(feature)
  • fix:修补bug
  • hotfix:紧急修复bug
  • test:增加测试
  • docs:文档(documentation)
  • style: 格式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  • chore:构建过程或辅助工具的变动

举个例子,我新增了用户登录功能,那么提交信息应该这样写:

git commit -m "feat(user): add the user login"

2. 使用git hooks校验

.git/hooks目录下有很多钩子,我们可以根据需要自定义不同的内容,这里我们只需要修改commit-msg即可。

首先将commit-msg.sample 改为 commit-msg,也就是去掉后缀。
将里面的内容修改为下面内容

#!/bin/bash
MSG=`awk '{printf("%s",$0)}' $1`
if [[ $MSG =~ ^(feat|fix|test|refactor|docs|style|chroe)\(.*\):.*$ ]]
then
	echo -e "\033[32m commit success! \033[0m"
else
    echo -e "\033[31m Error: the commit message is irregular \033[m"
	echo -e "\033[31m Error: type must be one of [feat,fix,docs,style,refactor,test,chore] \033[m"
    echo -e "\033[31m eg: feat(user): add the user login \033[m"
	exit 1
fi

当我们提交不规范的commit信息时就会提醒用户,并终止此提交

2.1 全局hook

mkdir -p ~/.git_template/hooks
cp commit-msg ~/.git_template/hooks
git config --global init.templatedir ~/.git_template

3. gitlab ci 中校验

3.1 .gitlab-ci.yml

stages:
- lint

commit_msg:
  stage: lint
  script:
  - /bin/bash commit-msg.sh
  only:
  - pushes

3.2 commit-msg.sh

#! /bin/bash

# get commit msg
if [[ $CI_COMMIT_MESSAGE ]]; then
    msg=$CI_COMMIT_MESSAGE
else
    read msg < .git/COMMIT_EDITMSG
fi

echo -e "\033[33m The commit msg: \033[0m $msg"

# if the msg is merge then skip it
mergePattern='^Merge '
if [[ $msg =~ $mergeCommitPattern ]]; then
    echo -e "\033[32m skip the merge, commit success! \033[0m"
    exit 0
fi

# check the commit msg
maxLength=50
length=${#msg}
pattern='^(feat|fix|hotfix|test|refactor|docs|style|chroe)\(.*\):.*$'

if [[ $msg =~ $pattern ]]; then
    if [[ $length -gt $maxLength ]]; then
        echo -e "\033[31m Error: the msg over max length \033[m"
        exit 1
    fi
    echo -e "\033[32m commit success! \033[0m"
else
    echo -e "\033[31m Error: the commit message is irregular \033[m"
	echo -e "\033[31m Error: type must be one of [feat,fix,hotfix,docs,style,refactor,test,chore] \033[m"
    echo -e "\033[31m eg: feat(user): add the user login \033[m"
    exit 1
fi
Git Commit规范是一种约定俗成的方式,用于规范化提交信息的格式和内容,使得提交历史更加清晰、易读和易于管理。以下是一种常见的Git Commit规范: 1. 提交信息的结构:每个提交信息由三个部分组成:标题、正文和页脚。 标题:简明扼要地描述这次提交的内容,通常以动词开头,使用一般现在时,不超过50个字符。 正文:对提交的详细描述,可以包括为什么进行该提交、做了哪些修改等信息。 页脚:可包含与提交相关的其他附加信息,如关联的Issue编号、作者等。 2. 提交信息的格式:为了保持统一和易读性,可以采用以下格式: ``` <type>(<scope>): <subject> <body> <footer> ``` 其中,`<type>`代表提交的类型,如`feat`(新功能)、`fix`(修复bug)、`docs`(文档更新)等。 `<scope>`代表本次提交的范围,可以是具体的文件、模块或功能名称。 `<subject>`是对本次提交内容的简要描述。 `<body>`是对本次提交内容的详细描述。 `<footer>`是页脚部分,可以包含相关链接、引用等信息。 3. 示例: ``` feat(user): 添加用户注册功能 - 在用户界面增加注册表单 - 后端增加注册接口 Issue #123 ``` 通过遵循Git Commit规范,可以提高团队协作效率、减少沟通成本,并且更好地追踪和管理项目的提交历史。当然,具体的规范可以根据团队的实际需求和偏好进行调整和定制。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值