【问题标题】:SVN pre-commit hook Max size fileSVN pre-commit hook 最大文件大小
【发布时间】:2022-01-14 04:39:36
【问题描述】:

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

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

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

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

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

File 20 is too large

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

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

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

但它仍然没有工作。请问有人能写出正确的变体吗?

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

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

【问题讨论】:

    标签: linux bash svn hook filesize


    【解决方案1】:

    这个wariant为我工作:)

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

    【讨论】:

      【解决方案2】:

      您在代码之外至少犯了 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
      

      【讨论】:

      • @Gretc - 我没有想法(现在没有任何 *Nix 用于测试),但stackoverflow.com/a/27640316/960558(第二个代码-sn-p)会给你 1)工作 2)稍微改进了使用钩子
      • 我尝试了第二段代码,但不幸的是它不起作用。我可以添加任何大小的提交。
      • 据我了解它必须是 size=$(svnlook -t $TXN filesize $REPOS $file)
      • @Gretc - 我更喜欢遵循默认模式svnlook filesize -t $TXN $REPOS $file,但是是的,结果必须是 10 位数字
      • $file 必须在某处定义?因为它仍然对我不起作用 XD
      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      相关资源
      最近更新 更多