git - 限制git存储库中的文件大小

标签 git version-control

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

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

最佳答案

因为我在它上面苦苦挣扎了一段时间,即使有了描述,我认为这也与其他人相关,所以我想我应该发布一个关于如何实现 J16 SDiZ described 的实现。可以实现。

所以,我对服务器端的看法 update钩子(Hook)防止太大的文件被推送:

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

注意它是commented此代码仅检查最新提交,因此需要调整此代码以迭代 $2 和 $3 之间的提交并对所有提交进行检查。

关于git - 限制git存储库中的文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7147699/

相关文章:

MySQL 模式源代码控制

git - 获取有关 SHA-1 提交对象的信息?

git - 如何为特定命令申请 `git --no-pager`?

Git - 如何更新旧提交

version-control - Team Foundation Server 源代码控制结构

linux - 更新时如何使分支更改反射(reflect)在主服务器上?

git - 管理 GIT 权限 - 多个存储库

git - 我怎样才能知道有多少 master 中的提交不在我的分支中?

git 子模块在 git status 中显示为文件

linux - Git Windows 多用户 - 组权限问题