如何在提交时限制文件大小?

时间:2016-09-19 14:57:59

标签: linux git github filesize

我完全了解this question从技术上讲这是重复的,但答案只提供推送的解决方案,这对我的要求来说太晚了。 / em>的

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

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

4 个答案:

答案 0 :(得分:10)

此预提交挂钩将执行文件大小检查:

<强>的.git /钩/预提交

#!/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

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

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

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

答案 1 :(得分:2)

您需要在pre-commit eis script中实施您已经找到的hook

从文档中我们了解到 预提交挂钩

  

不带参数,在获取建议的提交日志消息并进行提交之前调用。退出此脚本的非零状态会导致git commit命令在创建提交之前中止。

基本上,调用钩子来检查是否允许用户提交他的更改。

eis上由other post创建的脚本变为

#!/bin/bash
# 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 10M
if [ -z "$filesizelimit" ]; then
        filesizelimit=10000000
fi

# You specify a warning limit
filesizewarning=500000

# 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 HEAD | 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
elif [ $filesize -gt $filesizewarning ]; then
        echo "WARNING ! A file size is bigger that $filesizewarning"
fi
exit 0

答案 2 :(得分:0)

有一个普通的预提交钩子。您可以编写脚本来检查文件大小,然后接受或拒绝提交。然而,Git为用户提供了从命令行类型“git help hooks”绕过检查的能力,以获取更多信息。以下是预提交挂钩的相关信息。

预提交

此挂钩由git commit调用,可以使用--no-verify选项绕过。它不需要参数,并在获取建议的提交日志消息和进行提交之前被调用。从此脚本退出非零状态会导致git commit中止。

答案 3 :(得分:0)

@Leon脚本的一个简短,特定于bash的版本,它以人类可读的格式打印文件大小。 --diff-filter=d选项需要更新的git:

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

status=0

bytesToHuman() {
  b=${1:-0}; d=''; s=0; S=({,K,M,G,T,P,E,Z,Y}B)
  while ((b > 1000)); do
    d="$(printf ".%01d" $((b % 1000 * 10 / 1000)))"
    b=$((b / 1000))
    let s++
  done
  echo "$b$d${S[$s]}"
}

# Iterate over the zero-delimited list of staged files.
while IFS= read -r -d '' file ; do
  hash=$(git ls-files -s "$file" | cut -d ' ' -f 2)
  size=$(git cat-file -s "$hash")

  if (( $size > $hard_limit )); then
    echo "Error: Cannot commit '$file' because it is $(bytesToHuman $size), which exceeds the hard size limit of $(bytesToHuman $hard_limit)."
    status=1
  elif (( $size > $soft_limit )); then
    echo "Warning: '$file' is $(bytesToHuman $size), which exceeds the soft size limit of $(bytesToHuman $soft_limit). Please double check that you intended to commit this file."
  fi
done < <(git diff -z --staged --name-only --diff-filter=d)
exit $status

与其他答案一样,该内容必须以执行权限保存为.git/hooks/pre-commit

示例输出:

Error: Cannot commit 'foo' because it is 117.9MB, which exceeds the hard size limit of 10.0MB.