如何在提交时限制文件大小? [英] How to limit file size on commit?

查看:302
本文介绍了如何在提交时限制文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全知道这个问题,这在技术上这个答案只是提供了一个推送的解决方案,这对我的需求来说已经太晚了。



是有提供限制提交时的文件大小的选项吗?



例如:大于500K的文件大小会产生警告。文件大小超过10M会停止提交。

解决方案

这个预先提交的钩子将执行文件大小检查:



.git / hooks / pre-commit
$ b

 #!/ bin / sh 
hard_limit = $(git config hooks.filesizehardlimit)
soft_limit = $(git config hooks.filesizesoftlimit)
:$ {hard_limit: = 10000000}
:$ {soft_limit:= 500000}

list_new_or_modified_files()
{
git diff --staged --name-status | sed -e' / ^ D / d; / ^ d /! s /。\ s\ + //'
}

unmunge()
{
local result =$ {1#\}
result =$ {result%\}
env echo -e$ result
}

check_file_size()
{
n = 0
读取-r munged_filename

f =$(unmunge$ munged_filename)
h = $(git ls-files -s$ f | cut -d''-f 2)
s = $(git cat-file -s$ h)
if [$ s-gt $ hard_limit]
then
env echo -E 1>& 2错误:超出硬限制大小($ hard_limit):$ munged_filename($ s)
n = $((n + 1))
elif [ $ s-gt $ soft_limit]
然后
env echo -E 1>& 2警告:软尺寸限制($ soft_limit)超出:$ munged_filename($ s)
fi
完成

[$ n -eq 0]
}

list_new_or_modified_files | check_file_size

上面的脚本必须保存为 .git / hooks / pre-commit 启用了执行权限( chmod + x .git / hooks / pre-commit )。

默认的软(警告)和硬(错误)大小限制设置为500,000和10,000,000字节,但可以通过 hooks.filesizes软限制和<$ c $覆盖c> hooks.filesizehardlimit 设置分别为:

  $ git config hooks.filesizesoftlimit 100000 
$ git config hooks.filesizehardlimit 4000000


I'm fully aware of this question which technically makes this a duplicate but the answers only offer a solution on push, which would be too late for my requirements.

Is there an option to limit the file size when committing?

For example: file sizes above 500K would produce a warning. File sizes above 10M would stop the commit.

解决方案

This pre-commit hook will do the file size check:

.git/hooks/pre-commit

#!/bin/sh
hard_limit=$(git config hooks.filesizehardlimit)
soft_limit=$(git config hooks.filesizesoftlimit)
: ${hard_limit:=10000000}
: ${soft_limit:=500000}

list_new_or_modified_files()
{
    git diff --staged --name-status|sed -e '/^D/ d; /^D/! s/.\s\+//'
}

unmunge()
{
    local result="${1#\"}"
    result="${result%\"}"
    env echo -e "$result"
}

check_file_size()
{
    n=0
    while read -r munged_filename
    do
        f="$(unmunge "$munged_filename")"
        h=$(git ls-files -s "$f"|cut -d' ' -f 2)
        s=$(git cat-file -s "$h")
        if [ "$s" -gt $hard_limit ]
        then
            env echo -E 1>&2 "ERROR: hard size limit ($hard_limit) exceeded: $munged_filename ($s)"
            n=$((n+1))
        elif [ "$s" -gt $soft_limit ]
        then
            env echo -E 1>&2 "WARNING: soft size limit ($soft_limit) exceeded: $munged_filename ($s)"
        fi
    done

    [ $n -eq 0 ]
}

list_new_or_modified_files | check_file_size

Above script must be saved as .git/hooks/pre-commit with execution permissions enabled (chmod +x .git/hooks/pre-commit).

The default soft (warning) and hard (error) size limits are set to 500,000 and 10,000,000 bytes but can be overriden through the hooks.filesizesoftlimit and hooks.filesizehardlimit settings respectively:

$ git config hooks.filesizesoftlimit 100000
$ git config hooks.filesizehardlimit 4000000

这篇关于如何在提交时限制文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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