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

svn pre-commit hook - how to block a keyword in certain file types?

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

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

这是我目前的情况:

$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; }

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

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

看看 "Detect merge conflict" 您可以将冲突标记更改为您想要的文本。

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

如果你想检查整个文件你可以看看下面的脚本 看看傀儡"file parsing" 您可以修改脚本以 grep 获取您的文本。

我结合使用 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  }'` > /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