git commit 规范及约束设置

业界通用规范

AngularJS 在 github上 的提交记录被业内许多人认可,逐渐被大家引用

格式

1
type(scope):subject

  1. type(必须) : commit 的类别,只允许使用下面几个标识
  • feat : 新功能

  • fix : 修复bug

  • docs : 文档改变

  • style : 代码格式改变

  • refactor : 某个已有功能重构

  • perf : 性能优化

  • test : 增加测试

  • build : 改变了build工具 如 grunt换成了 npm

  • revert : 撤销上一次的 commit

  • chore : 构建过程或辅助工具的变动

    1. scope(可选) : 用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

    2. subject(必须) : commit 的简短描述,不超过50个字符.commitizen 是一个撰写合格 Commit message 的工具,遵循 Angular 的提交规范。

添加 git commit约束
一、向工程的.git目录下添加约束文件
二、cd 到工程中,含.git的目录下。
执行如下命令chmod +x .git/hooks/commit-msg,其中.git/hooks/commit-msg为约束文件路径。
三、约束文件具体内容为

#!/bin/bash

# 获取提交消息
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")

# 定义提交消息类型的正则表达式
# 以类型开头,类型可以是 feat, fix, docs, style, refactor, perf, test, build, revert, chore
TYPE_REGEX="^(feat|fix|docs|style|refactor|perf|test|build|revert|chore): "

# 检查提交消息类型是否符合规范
if ! [[ "$COMMIT_MSG" =~ $TYPE_REGEX ]]; then
    echo "Error: Commit message type is invalid."
    echo "Detail:"
    echo "Your commit message was: '$COMMIT_MSG'"
    echo "It should start with one of the following types followed by a colon and a space:"
    echo "feat, fix, docs, style, refactor, perf, test, build, revert, chore"
    echo "Example: 'feat: Add user authentication feature'"
    exit 1
fi

# 获取描述信息部分
DESCRIPTION=$(echo "$COMMIT_MSG" | sed -E "s/$TYPE_REGEX//")

# 检查描述信息字数是否在 2 到 50 个字符之间
if [ ${#DESCRIPTION} -lt 2 ] || [ ${#DESCRIPTION} -gt 50 ]; then
    echo "Error: Commit message description length is invalid."
    echo "Detail:"
    echo "Your commit message was: '$COMMIT_MSG'"
    echo "The description should be between 2 and 50 characters long."
    echo "Example: 'feat: Add user authentication feature'"
    exit 1
fi

exit 0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容