pre commit Hook用于阻止大于20MB的提交

Kak*_*aku 2 svn hook

是否可以为SVN 1.8编写prcommit挂钩,以避免提交大于~20MB的文件.任何建议,将不胜感激.谢谢 !我试过这个,但这不适用于二进制文件或其他文件扩展名

filesize=$($SVNLOOK cat -t $TXN $REPOS $f|wc -c)
if [ "$filesize" -gt "$MAX_SIZE"]
then
    echo "File $f is too large(must <=$MAX_SIZE)" >&2
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

Dav*_* W. 6

你需要在这里做两件事:

  • 首先用于svn -t$TXN changed获取事务中已更改的路径.您需要过滤掉目录,这些目录将以svn changeda结尾返回/.
  • 对于每个条目svn changed,您需要svn -t$TXN filesize file针对它运行.然后,总结文件更改.

像这样的东西.这没有经过测试,但应该给你一个想法:

total=0
svnlook -t $TXN changed | while read status file
do
    [[ status == "D" ]] && continue  # Ignore deletions
    [[ $file == */ ]] && continue    # Skip directories
    size=$(svnlook -t $TXN filesize $REPOS $file)
    ((total+=size))
done
if [[ $total -gt $maxsize ]]
then
    echo "Commit is too big!" >&2
    exit 1
else
    exit 0
fi
Run Code Online (Sandbox Code Playgroud)

但是,我不喜欢使用shell作为钩子脚本.相反,您应该使用像Python或Perl这样的真正的脚本语言.它们更容易使用,功能更强大,更灵活.

你为什么要过滤掉这些大提交?您是否正在让开发人员尝试提交他们刚刚构建的可执行文件?


懒惰的獾指出你不是在谈论总提交大小,而是只有一个文件那么大.如果是这种情况,你会做一个类似的钩子,但没有总计:

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