限制git存储库中的文件大小 [英] Limiting file size in git repository

查看:443
本文介绍了限制git存储库中的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在考虑将我的VCS(从颠覆)改为git。是否有可能限制git仓库中提交的文件大小?前面。 G。颠覆有一个钩子: http://www.davidgrant.ca/limit_size_of_subversion_commits_with_this_hook



根据我的经验,人们,特别是那些经验不足的人,有时会倾向于提交不应该进入VCS的文件(例如大文件系统映像)。

解决方案

由于我在一段时间内苦苦挣扎,即使有描述,我认为这也与其他人有关,我想我会发布如何实施 J16 SDiZ 的实现。



所以,我把它放在服务器端 update 钩子防止太大的文件被推送:

 #!/ bin / bash 

脚本限制推送到git存储库的大小。
#Git回购有大问题,我们不应该有真正需要这些

#eis / 02.02.2012

# - - 安全检查,不应从命令行运行
如果[-z$ GIT_DIR];那么
回显不要从命令行运行此脚本。 >& 2
echo(如果你愿意,你可以提供GIT_DIR然后运行>& 2
echo$ 0< ref>< oldrev>< newrev>)> ;& 2
exit 1
fi

#测试这个标签替换的工作方式,在某些Solaris环境下至少
testvariable =`echo -e\ t| sed's / \s //'`
if [$ testvariable!=];然后
回显环境检查失败 - 请联系git托管。 >& 2
exit 1
fi


#文件大小限制意味着通过'hooks.filesizelimit'设置
filesizelimit = $(git config hooks.filesizelimit)

#如果我们还没有配置文件大小限制,使用默认值大约100M
if [-z$ filesizelimit];然后
filesizelimit = 100000000
fi

#引用入站checkin可以在$ 3
refname = $ 3

#找到使用此命令,我们可以找到有关最大大小的文件的信息
#我们还规范了多余空白的行
largest_checkin_normalized = $(git ls-tree --full-tree -r -l $ refname | sort -k 4 -n -r | head -1 | sed's / ^ * //; s / * $ //; s / \s\ {1,\} / / g')

#基于此,我们可以找到我们感兴趣的
filesize =`echo $ largest_checkin_normalized | cut -d''-f4,4'

#实际比较
#要取消推送,我们以状态码1
#退出,这也是一个好主意如果[$ filesize -gt $ filesizelimit];关于拒绝原因的一些信息
;然后

#为了更加用户友好,我们还查找了违规文件的名称
filename =`echo $ largest_checkin_normalized | cut -d''-f5,5`​​

echo错误:尝试过大推。 >& 2
echo>& 2
echo文件大小限制为$ filesizelimit,并且您尝试推送大小为$ filesize的名为$ filename的文件。 >& 2
echo如果您确实需要这样做,请与配置小组联系。 >& 2
exit 1
fi

exit 0


I'm currently thinking of changing my VCS (from subversion) to git. Is it possible to limit the file size within a commit in a git repository? For e. g. subversion there is a hook: http://www.davidgrant.ca/limit_size_of_subversion_commits_with_this_hook

From my experience people, especially those who are inexperienced, sometimes tend to commit files which should not go into a VCS (e. g. big file system images).

解决方案

As I was struggling with it for a while, even with the description, and I think this is relevant for others too, I thought I'd post an implementation of how what J16 SDiZ described could be implemented.

So, my take on the server-side update hook preventing too big files to be pushed:

#!/bin/bash

# Script to limit the size of a push to git repository.
# Git repo has issues with big pushes, and we shouldn't have a real need for those
#
# eis/02.02.2012

# --- Safety check, should not be run from command line
if [ -z "$GIT_DIR" ]; then
        echo "Don't run this script from the command line." >&2
        echo " (if you want, you could supply GIT_DIR then run" >&2
        echo "  $0 <ref> <oldrev> <newrev>)" >&2
        exit 1
fi

# Test that tab replacement works, issue in some Solaris envs at least
testvariable=`echo -e "\t" | sed 's/\s//'`
if [ "$testvariable" != "" ]; then
        echo "Environment check failed - please contact git hosting." >&2
        exit 1
fi


# File size limit is meant to be configured through 'hooks.filesizelimit' setting
filesizelimit=$(git config hooks.filesizelimit)

# If we haven't configured a file size limit, use default value of about 100M
if [ -z "$filesizelimit" ]; then
        filesizelimit=100000000
fi

# Reference to incoming checkin can be found at $3
refname=$3

# With this command, we can find information about the file coming in that has biggest size
# We also normalize the line for excess whitespace
biggest_checkin_normalized=$(git ls-tree --full-tree -r -l $refname | sort -k 4 -n -r | head -1 | sed 's/^ *//;s/ *$//;s/\s\{1,\}/ /g' )

# Based on that, we can find what we are interested about
filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4`

# Actual comparison
# To cancel a push, we exit with status code 1
# It is also a good idea to print out some info about the cause of rejection
if [ $filesize -gt $filesizelimit ]; then

        # To be more user-friendly, we also look up the name of the offending file
        filename=`echo $biggest_checkin_normalized | cut -d ' ' -f5,5`

        echo "Error: Too large push attempted." >&2
        echo  >&2
        echo "File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize." >&2
        echo "Contact configuration team if you really need to do this." >&2
        exit 1
fi

exit 0

这篇关于限制git存储库中的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆