svn pre-commit hook - 如何在某些文件类型中阻止关键字?

时间:2015-04-17 19:16:48

标签: svn grep pre-commit-hook svn-hooks pre-commit

我正在尝试编写一个svn预提交挂钩,如果某些文件类型中存在某些关键字,则会出错。

我的情况是,如果文件是.java,.jsp或.jspf文件,我想确保它们中不存在“http://”和“https://”。到目前为止,如果关键字存在于任何文件中,我可以抛出错误,但不是我想检查的文件类型。

这是我到目前为止所拥有的:

$SVNLOOK diff -t "$TXN" "$REPOS" | grep -i "https://" > /dev/null && { echo "Your commit has been blocked because it contains the keyword https://." 1>&2; exit 1; }

2 个答案:

答案 0 :(得分:1)

我使用svnlook changedsvnlook cat

的组合来计算出来
#Put all the restricted formats in variable FILTER
HTTP_FILTER=".(j|jsp)$"


# 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 [[ "$HTTP_FILTER" == *"$EXTENSION"* ]]; then

        # needed to only use http:/ or https:/ - for some reason doing double slash (i.e. http://)
        # would not return results.
        $SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | egrep -wi "https:/|http:/" > /dev/null && { echo "Your commit has been blocked because it contains the keyword https:// or http://." 1>&2; exit 1; }

    fi

done

答案 1 :(得分:0)

是的,你可以很容易地做到。

以下是此类脚本的链接 http://www.scmtechblog.net/2014/07/few-pre-commit-svn-scripts-and-tricks.html

看看“检测合并冲突” 您可以将冲突标记更改为所需的文本。

上面将检查正在更改的文件的增量。

如果要检查整个文件,可以查看以下脚本 看看Puppet“文件解析” 您可以修改脚本以grep查看文本。