欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > Git 钩子:特定操作脚本

Git 钩子:特定操作脚本

2025/3/31 5:08:06 来源:https://blog.csdn.net/weixin_43718346/article/details/146474721  浏览:    关键词:Git 钩子:特定操作脚本

Git 钩子

  • 特定 Git 操作发生时自动触发脚本

  • 可以从提交规范、代码质量、自动化流程、分支管理、安全性检查等多个方面进行配置,帮助团队提高开发效率和代码质量;

本地

记录提交检验
  • commit-msg

  • 修改:\test\.git\hooks\commit-msg.sample\test\.git\hooks\commit-msg

  • 设置可执行权限:chmod +x \test\.git\hooks\commit-msg

#!/bin/bash
# 获取提交信息文件路径
commit_msg_file=$1
commit_msg=$(cat "$commit_msg_file")
# 定义提交信息格式的正则表达式
commit_regex="^(revert: )?(create|fixed|added|chore|refactor|hotfix|revert|update|build|test|save)($.+$)? - .{1,50}$"
​
warn() {echo -e "\033[41;37m $1 \033[0m \033[1;31m $2 \033[0m"
}
​
red() {echo -e "\033[31m$1\033[0m"
}
​
green() {echo -e "\033[32m$1\033[0m"
}
​
yellow() {echo -e "\033[33m$1\033[0m"
}
​
cyan() {echo -e "\033[34m$1\033[0m"
}
​
link() {echo -e "\033[4;34m$1\033[0m"
}
​
# 验证提交信息是否符合格式
if [[ ! $commit_msg =~ $commit_regex ]]; then# 输出错误信息echowarn "ERROR","无效的 commit 提交格式"echoecho -e "\033[31m请遵循以下正确的格式提交。例如:\033[0m"echo -e "\033[4;31mgit commit -m \"feat: 完成详情\"\033[0m"echoecho -e "\033[32m以下是一些示例:\033[0m"echoecho "+--------+-------------------+---------------------------+"printf "| %s\t | %-16s\t | %-24s\t |\n" "索引" "注释" "contect"echo "+--------+-------------------+---------------------------+"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "0" "新建项目" "create - 创建 XXX"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "1" "修复异常" "fixed - XXX bug"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "2" "新增特性" "added - 特性 XXX"printf "| %-4s\t | %-20s\t | %-28s\t |\n" "3" "构建新流程" "chore - 同步分支"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "4" "模块重构" "refactor - XXX"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "5" "紧急修复" "hotfix - XXX bug"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "6" "版本回滚" "revert - V1.0.1.0"printf "| %-4s\t | %-20s\t | %-28s\t |\n" "7" "更新文档" "update - 更新记录"printf "| %-4s\t | %-20s\t | %-28s\t |\n" "8" "项目打包" "build - 运行环境"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "9" "调试测试" "test - 测试 XXX"printf "| %-4s\t | %-20s\t | %-24s\t |\n" "10" "保存进度" "save - 暂存 XXXX"echo "+--------+-------------------+---------------------------+"echored "描述:文字描述不得超过 50 个字符,推荐以动词开头:创建、修复、完成、修改、增加、更新等。"echoecho -e "更多详情请参考:"link "https://blog.csdn.net/weixin_43718346"echoexit 1
fi
​
#echo "+--------+-----------------------+-------------------------------+"
#printf "| %-8s | %s\t\t\t | %s\t\t\t |\n" "索引" "注释" "contect"
#echo "+--------+-----------------------+-------------------------------+"
#printf "| %-6s | %s\t\t | %s\t |\n" "0" "新建项目" "create - 创建 XXX 项目"
#printf "| %-6s | %s\t\t | %s\t\t |\n" "1" "修复 bug" "fixed - 修复 XXX bug"
#printf "| %-6s | %s\t\t | %s\t\t |\n" "2" "新增特性" "added - 新增特性 XXX"
#printf "| %-6s | %s\t\t | %s\t |\n" "3" "改变构建流程" "chore - 同步[分支名称]"
#printf "| %-6s | %s\t\t | %s\t\t |\n" "4" "重构内容" "refactor - 重构 XXX"
#printf "| %-6s | %s\t\t | %s\t |\n" "5" "紧急修复" "hotfix - 紧急修复 XXX"
#printf "| %-6s | %s\t\t | %s\t |\n" "6" "版本回滚" "revert - 回滚 XXX 版本"
#printf "| %-6s | %s\t\t | %s\t\t |\n" "7" "更新文档" "update - 更新文档"
#printf "| %-6s | %s\t\t | %s\t\t |\n" "8" "打包项目" "build - 打包 XXX"
#printf "| %-6s | %s\t\t | %s\t\t |\n" "9" "测试文件" "test - 测试 XXX"
#printf "| %-6s | %s\t | %s\t\t |\n" "10" "暂存文件(保存进度)" "save - 暂存 XXXX"
#echo "+--------+-----------------------+-------------------------------+"
​
exit 0

服务器

提交检验
  • pre-receive

  • 修改:\test\.git\hooks\pre-receive.sample\test\.git\hooks\pre-receive

  • 设置可执行权限:chmod +x \test\.git\hooks\pre-receive

#!/bin/bash
​
warn() {echo -e "\033[41;37m $1 \033[0m \033[1;31m $2 \033[0m"
}
​
red() {echo -e "\033[31m$1\033[0m"
}
​
green() {echo -e "\033[32m$1\033[0m"
}
​
yellow() {echo -e "\033[33m$1\033[0m"
}
​
cyan() {echo -e "\033[34m$1\033[0m"
}
​
link() {echo -e "\033[4;34m$1\033[0m"
}
​
while read oldrev newrev refname; do
​# 获取推送的用户名username=$(git log -1 --pretty=format:%an $newrev)# # 定义允许操作的用户列表# allowed_users=("user1" "user2" "user3")# if [[ ! " ${allowed_users[@]} " =~ " ${username} " ]]; then# echo "错误: 用户 '$username' 没有操作权限。"# exit 1# fi# # 获取推送的邮箱# email=$(git log -1 --pretty=format:%ae $new_value)# # 定义邮箱规则的正则表达式(必须以 @hanslaser.com 结尾)# email_re="@hanslaser\.com$"# # 验证邮箱是否符合规则# if [[ ! $email =~ $email_re ]]; then# echo "错误: 邮箱 '$email' 必须以 @hanslaser.com 结尾。"# exit 1# fi# # 定义命名规则的正则表达式(英文名+数字工号,且工号不超过6位)# regex="^[a-zA-Z]+[0-9]{1,6}$"# # 验证用户名是否符合规则# if [[ ! $username =~ $regex ]]; then# echo "错误: 用户名 '$username' 必须为英文名+数字工号,且工号不超过6位。"# exit 1# fi# if [[ $refname != "refs/heads/main" ]]; then# echo "错误: 禁止创建新的分支!"# exit 1# fidone
exit 0

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词