本文介绍: 当我们初始化一个项目之后,.git 目录下有一个 hooks 目录可以看到上图左侧有很多执行任务比如 precommit,代表在运行这些命令之后或之前,会进行一些校验检测来执行相应任务。与许多其他版本控制系统一样,Git 有一种方法可以在发生某些重要操作时,触发自定义脚本,即 Git Hook(Git 钩子)。对于配置单个仓库server hooks也是需要开启全局配置,否则会导致脚本在某个repo的相对路径下不会生效。该脚本的作用是规范gitlab提交信息

1. 前置条件

2. Git Hook

与许多其他版本控制系统一样,Git 有一种方法可以在发生某些重要操作时,触发自定义脚本,即 Git Hook(Git 钩子)。
在这里插入图片描述
我们初始化一个项目之后,.git 目录下有一个 hooks 目录,可以看到上图左侧有很多执行任务比如 precommit,代表在运行这些命令之后或之前,会进行一些校验和检测来执行相应任务

2.1 Git Hook 分为两部分:本地远程

在这里插入图片描述

2.1.1 本地 Git Hook,由提交合并操作触发:
2.1.2 远程 Git Hook运行网络操作上,例如接收推送提交

这里,我们主要关注本地 hook,比如说 premessageprepush,因此我们会借助这些工具实现规范化代码内容

3. 操作步骤

3.1 对所有的仓库配置server hooks

3.1.1 全局配置

参考地址git hooks all repo

gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"
gitlab-ctl reconfigure

在这里插入图片描述

3.1.2 编写脚本

创建目录custom_hooks

mkdir -p /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/

编写脚本

该脚本的作用是规范gitlab提交的信息
cat /var/opt/gitlab/gitaly/custom_hooks/prereceive.d/prereceive.sh

#!/bin/bash
while read oldrev newrev refname; do
    # 从标准输入读取每个引用的旧版本、新版本和引用名称
    commits=$(git rev-list --pretty=oneline $oldrev..$newrev)
    # 遍历每个提交信息
    while read commit; do
        # 提取提交信息的前缀
        prefix=$(echo "$commit" | awk '{print $2}' | awk -F ":" '{print $1}')
        # 检查前缀是否符合要求
        if [[ $prefix != "feat" && $prefix != "fix" && $prefix != "hotfix" ]]; then
            echo "Error: Invalid commit prefix in one or more commits:"
            echo "$commit"
            echo "Only commits with prefixes 'feat', 'fix', or 'hotfix' are allowed."
            exit 1
        fi
    done <<< "$commits"
done

给脚本权限

chown -R git.root /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d
chmod +x git.root /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/pre-receive.sh
3.1.3 测试

测试提交
在这里插入图片描述
回退commit
在这里插入图片描述
再次提交
在这里插入图片描述

3.2 对单个仓库配置server hooks

3.1.1 全局配置

参考地址git hooks single repo

gitaly['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks"
gitlab-ctl reconfigure

在这里插入图片描述

对于配置单个仓库的server hooks也是需要开启全局的配置,否则会导致脚本在某个repo的相对路径下不会生效

3.1.2 编写脚本

创建目录custom_hooks

# 进入到仓库在gitlab中的存储路径
cd /var/opt/gitlab/git-data/repositories/
# 根据在gitlab的`管理界面 - 项目`中查看项目的相对路径(是一个hash路径)
cd @hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git
# 创建custom_hooks目录
mkdir -p custom_hooks/pre-receive.d
chown -R git.root custom_hooks

编写脚本

该脚本的作用是禁用对该仓库做任何的push操作

cat pre-receive.sh

#!/bin/bash

while read oldrev newrev refname; do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)

    # 检查是否提交,如果有则拒绝
    if [ "$oldrev" != "0000000000000000000000000000000000000000" ]; then
        echo "Pushing to $branch is not allowed. All pushes are prohibited."
        exit 1
    fi
done

给脚本权限

# 进入到仓库在gitlab中的存储路径下
cd /var/opt/gitlab/git-data/repositories/
# 根据在gitlab的`管理界面 - 项目`中查看项目的相对路径(是一个hash路径)
cd @hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git
chown -R git.root custom_hooks/
chmod +x git.root custom_hooks/pre-receive.d/pre-receive.sh

在这里插入图片描述

3.1.3 测试

测试提交
在这里插入图片描述

原文地址:https://blog.csdn.net/weixin_44729138/article/details/134719504

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_15185.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注