SVN 预提交挂钩最大文件大小

SVN pre-commit hook Max size file

我对预提交挂钩有疑问。脚本的前半部分有效,但检查最大尺寸的后半部分无效。有人知道问题出在哪里吗?

#!/bin/bash

REPOS=
TXN=
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  }'` > /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

好的,我做了一个小脚本来检查文件大小

#!/bin/bash

REPOS=
TXN=
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

当我尝试共同提交具有 25.5 MB 的文件时,在输出中只看到 20

File 20 is too large

为什么只有 20 个?我认为输出必须像 26826864 Bytes

好的,这个脚本的最后一个 wariuan 是

#!/bin/bash

REPOS=
TXN=
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

但是还是不行。有人可以写出正确的变体吗?

在那种情况下,据我理解正确,scrypt 必须如下所示。但它仍然不起作用,也许有人知道问题出在哪里?然后我可以添加一个任意大小的文件。

#!/bin/bash

REPOS=
TXN=
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

您在代码之外至少犯了 2 个错误:

  • 之前使用钩子(我无法从记忆中回忆起格式)之前没有手动验证|wc -c的输出
  • 没有仔细 RTFM:$SVNLOOK cat 的管道是不必要的复杂化,而你有子命令 filesize.

加法

代码来自我的评论主题(仔细阅读,不要错过获取每个文件的周期)

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

这个 wariant 正在为我工​​作:)

#!/bin/bash
REPOS=
TXN=
MAX_SIZE=20971520
svnlook=/usr/bin/svnlook
AWK=/bin/awk
FILES=`${svnlook} changed -t ${TXN} ${REPOS} | ${AWK} '{print }'`
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