#!/bin/bash ########################################################################################## # This script was taken from a stackoverflow: # # http://stackoverflow.com/questions/7147699/limiting-file-size-in-git-repository # # It would seem that all software posed on StackOverflow by default # has a Creative Common license: # # http://creativecommons.org/licenses/by-sa/2.5/ # # That license says: # You are free to: # # Share — copy and redistribute the material in any medium or format # Adapt — remix, transform, and build upon the material # for any purpose, even commercially. # # The licensor cannot revoke these freedoms as long as you follow the license terms. # ########################################################################################## # 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. # # This is called for a generic driver script pre-receive that passes # in a single update ref as: # # # # as commandline arguments. # # Reference to incoming SHA1 is found in $2 (not the ref being updated) newrev=$2 #echo "newrev=$newrev" # 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 (or set the default to 49M so that we # will not cause a warning with github) filesizelimitmb=$(git config hooks.filesizelimitmb) if [ -z "$filesizelimitmb" ]; then filesizelimitmb=49 fi #echo "filesizelimitmb=$filesizelimitmb" filesizelimit=$(expr $filesizelimitmb \* 1024 \* 1024) #echo "filesizelimit=$filesizelimit" # 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 $newrev \ | sort -k 4 -n -r | head -1 \ | sed 's/^ *//;s/ *$//;s/\s\{1,\}/ /g' ) #echo "biggest_checkin_normalized:\n$biggest_checkin_normalized\n" # Based on that, we can find what we are interested about filesize=`echo $biggest_checkin_normalized | cut -d ' ' -f4,4` #echo "filesize=$filesize" # 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: Trying to push file that is larger than allowed filesizelimitmb=${filesizelimitmb}M" >&2 echo >&2 echo "File size limit is $filesizelimit, and you tried to push file named $filename of size $filesize." >&2 echo >&2 echo "Contact the repository administrators for details (see repo attribute config hooks.filesizelimitmb)!" >&2 exit 1 fi exit 0