在CSDN上看到一个兄弟的提问,想要控制SVN上传(确切的说应该是提交)文件的大小及类型。想起来自己在管理Bug单的时候也有经常类似审计每次提交,单个C文件的代码变更行数的需求。于是写了一个pre-commit的hook,以备忘。

原贴的地址: http://topic.csdn.net/u/20080404/15/acc7c13b-9079-4241-bcc2-fdb7a80be4a8.html?seed=1212379939


#!/bin/sh

# repot && transaction arguments
REPOS="$1"
TXN="$2"

# svnlook command
SVNLOOK=/usr/local/bin/svnlook

# file filter: we only allow commit .c && .h files.
FILTER='\.(c|h)$'

# max file size in bytes after commit.
MAX_SIZE=102400

# max change per one commit
MAX_CHANGE_LINES=50

files=$($SVNLOOK changed -t $TXN $REPOS | awk '{print $2}')

# check
for f in $files
do
    # check file type
    if echo $f | grep -Eq $FILTER ; then
        # valid file
        :
    else
        echo "File $f is not a .h or .c file" >> /dev/stderr
        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)" >> /dev/stderr
        exit 1
    fi

    # check change lines
    changelines=$($SVNLOOK diff -t $TXN $REPOS $f | grep -E '^(\+|-)' | wc -l)
    if [ $changelines -gt $MAX_CHANGE_LINES ] ; then
        echo "File $f changes too much ($changelines lines, must <= $MAX_CHANGE_LINES)" >> /dev/stderr
        exit 1
    fi
done

exit 0



pre-commit 返回非0,则commit失败。
错误信息为stderr的信息。



发现svnlook 还是很强大的,有空得好好研究研究,还有什么奇妙功能可以深入挖掘之。
by matrix | 多云 2008/04/06 22:07 | 分类: 我的文档 » 开源工具 | 评论(0) | 引用(0) | 阅读(422)
分页: 1/1 第一页 1 最后页 [ 显示模式: 摘要 | 列表 ]