Search code examples
linuxbashsvnhookfilesize

SVN pre-commit hook Max size file


I have a problem with pre-commit hook. The first half of the script works, but the second half that checks max size doesn't. Anyone have an idea what the problem is?

#!/bin/bash

REPOS=$1
TXN=$2
MAX_SIZE=10
AWK=/bin/awk
SVNLOOK="/usr/bin/svnlook";

#Put all banned extensions formats in variable FILTER
FILTER=".(iso|exe)$"

# Figure out what directories have changed using svnlook.
FILES=`${SVNLOOK} changed ${REPOS} -t ${TXN} | ${AWK} '{ print $2 }'` > /dev/null

for FILE in $FILES; do

#Get the base Filename to extract its extension
NAME=`basename "$FILE"`

#Get the extension of the current file
EXTENSION=`echo "$NAME" | cut -d'.' -f2-`

#Checks if it contains the restricted format
if [[ "$FILTER" == *"$EXTENSION"* ]]; then
    echo "Your commit has been blocked because you are trying to commit a restricted file." 1>&2
    echo "Please contact SVN Admin. -- Thank you" 1>&2
    exit 1

fi

#check file size

filesize=$($SVNLOOK cat -t $TXN $REPOS $f|wc -c)

if [ "$filesize" -gt "$MAX_SIZE" ]; then 
    echo "File $f is too large(must <=$MAX_SIZE)" 1>&2
    exit 1
fi

done
exit 0

OK, I made a little script to check filesize only

#!/bin/bash

REPOS=$1
TXN=$2
MAX_SIZE=10
AWK=/bin/awk
SVNLOOK="/usr/bin/svnlook";


#check file size

filesize=`${SVNLOOK} changed ${REPOS} -t ${TXN} | wc -c`

if [ "$filesize" -gt "$MAX_SIZE" ]; then 
    echo "File $filesize  is too large" 1>&2
    exit 1
fi
echo "File $filesize" 1>&2
exit 0

and when I try co commit file that have 25,5 MB, in output see only 20

File 20 is too large

Why its only 20 ? I think output must be on like 26826864 Bytes

Ok last wariuan of this script is

#!/bin/bash

REPOS=$1
TXN=$2
MAX_SIZE=10
svnlook="/usr/bin/svnlook";

size=$($svnlook filesize -t $TXN $REPOS $file)

if [[ "$size" -gt "$MAX_SIZE" ]] ; then 
    echo "File is too large" 1>&2
    exit 1
fi
exit 0

But it still didnt work. Can someone write the correct variant of this please?

In that case, as I understand correctly, scrypt must look like below. But it still doesn't work, maybe someone knows what the problem is? Then I can add a file of whatever size.

#!/bin/bash

REPOS=$1
TXN=$2
maxsize=-1
svnlook="/usr/bin/svnlook";

svnlook -t $TXN changed | while read status file
do
    [[ $status == "D" ]] && continue  # Skip Deletions
    [[ $file == */ ]] && continue     # Skip directories
    size=$(svnlook filesize -t $TXN $REPOS $file)
    if [ $size -gt $maxsize ]]
    then
        echo "File '$file' too large to commit" >&2
        exit 2
    fi
done
exit 0

Solution

  • This wariant is working for me :)

    #!/bin/bash
    REPOS=$1
    TXN=$2
    MAX_SIZE=20971520
    svnlook=/usr/bin/svnlook
    AWK=/bin/awk
    FILES=`${svnlook} changed -t ${TXN} ${REPOS} | ${AWK} '{print $2}'`
    for FILE in $FILES; do
    size=`${svnlook} filesize -t ${TXN} ${REPOS} ${FILES}`
    if [[ "$size" -gt "$MAX_SIZE" ]] ; then 
        echo "Your commit has been blocked because you are trying to commit a too large file." 1>&2
        exit 1
    fi
    done
    exit 0