git拦截大于5M文件

📅 2026/7/25 4:19:16
git拦截大于5M文件
1)路径rootslm-server:/data/gitea/data/data/repositories/wbs/test_larget_file.git# pwd /data/gitea/data/data/repositories/wbs/test_larget_file.git2)执行下rootslm-server:/data/gitea/data/data/repositories/wbs/test_larget_file.git# vim hooks/pre-receive.d/check-file-size#!/bin/bash # 设置最大文件大小为 5MB (5 * 1024 * 1024 5242880 字节) MAX_FILE_SIZE5242880 ZERO_COMMIT0000000000000000000000000000000000000000 while read oldrev newrev refname; do # 处理分支删除跳过 if [ $newrev $ZERO_COMMIT ]; then continue fi # 确定要检查的提交范围 if [ $oldrev $ZERO_COMMIT ]; then # 新分支检查所有提交 range$newrev else # 已有分支检查新增的提交 range$oldrev..$newrev fi # 遍历范围内的每个提交 for commit in $(git rev-list $range); do # 获取该提交中的所有文件 files$(git diff-tree --no-commit-id --name-only -r $commit) for file in $files; do # 获取文件在提交中的大小字节 file_size$(git cat-file -s $commit:$file 2/dev/null || echo 0) if [ $file_size -gt $MAX_FILE_SIZE ]; then echo ❌ 错误提交 $commit 中的文件 $file 大小超过限制 ($((file_size/1024/1024))MB 5MB) 2 echo 提示请使用 Git LFS 管理大文件或从提交中移除该文件 2 exit 1 fi done done done exit 03)加权限rootslm-server:/data/gitea/data/data/repositories/wbs/test_larget_file.git# chmod x hooks/pre-receive.d/check-file-size rootslm-server:/data/gitea/data/data/repositories/wbs/test_larget_file.git# chown gitea:gitea hooks/pre-receive.d/check-file-size4)测试dd if/dev/zero oftest30m.bin bs1M count30 git add test10m.bin git commit -m test 30mb git push