2012-10-30 135 views
3

在Collabnet SVN的預提交腳本中使用以下代碼來限制要提交的特定文件擴展名,但它提交了所有文件。你能告訴我哪裏出錯了嗎?SVN提前掛鉤以限制文件擴展名被提交

#!/bin/sh 

REPOS="$1" 
TXN="$2" 

SVNLOOK=/home/csvn/csvn/bin/svnlook 

LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c` 

if [ "$LOGMSG" -lt 10 ]; 
then 
    echo -e "\n===========================================================" 1>&2 
    echo -e "|| COMMENTS ARE MADE MANDATORY.PLEASE ENTER RELEVANT COMMENT & COMMIT AGAIN Sanjeev sas||" 1>&2 
    echo -e "===========================================================" 1>&2 
    exit 1 
fi 

$SVNLOOK changed -t $TXN $REPOS | awk ' 
BEGIN { 
FILTER=".(sh|xls|xlsx|pdf|jpg|JPG|gif|GIF|png|PNG|doc|DOC|docx|DOCX|mpg|swf|avi|mp3|mp4|zip|rar|gz|csv|o|obj|tar|gz|JPEG|jpeg|WMV|wmv|DAT|dat|3GP|3gp|MPEG|mpeg|VOD|vod|ear|jar|war|exe|ppt|PPT|PPTX|pptx|7z|iso|ISO|EAR|WAR|JAR|msg|MSG|rtf|RTF|xlsm|XLSM|vsd|VSD|dot|DOT|camrec|ECF|ecf|mff|MFF|class|CLASS)$" 
} 
{ 
for (i = 1; i < 2; i++) $i = ""; sub(/^ */, ""); 
if(match(tolower($1),FILTER)) 
{ 
    print "File" $1 "is not allowed file type to commit" 
    exit 1 
} 
}'  

exit 0 

回答

3

下面的腳本適合我! 不要讓我知道你的想法

#!/bin/bash 

REPOS=$1 
TXN=$2 
AWK=/bin/awk 
SVNLOOK="/usr/bin/svnlook"; 

#Put all the restricted formats in variable FILTER 
FILTER=".(sh|xls|xlsx|exe|xlsm|XLSM|vsd|VSD|bak|BAK|class|CLASS)$" 

# 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 

done 
exit 0 
0

我有同樣的問題,因爲@San,但是從@Jijo回答不直接對我的情況下提供幫助,因爲作爲我運行的腳本不爲我工作在AIX上的SVN服務器,我已經增強了腳本,如下所示,可以適合我的情況。無論如何,謝謝@Jijo分享劇本。

#Put all the restricted formats in variable FILTER 
FILTER=".(xls|xlsx|exe|pptx|PPTX|vsd|VSD|bak|BAK|class|CLASS|zip|ZIP|doc|DOC|docx|DOCX)$" 

# Figure out what directories have changed using svnlook. 
FILES=$(${SVNLOOK} changed -t ${TXN} ${REPOS} | ${AWK} '{ print $NF }') > /dev/null 

for FILE in $FILES 
do 
    EXTENSION=`echo "$FILE" | cut -d'.' -f2-` 

    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 
done 
0

解決方案的Windows預commit.bat

@ECHO OFF 

set REPOS=%1 
set TXN=%2 
set SVNLOOK="path\to\svnlook.exe" 

set FORMATS=*.ext1, *.ext2, *.ext3 
:CHECK1 
%SVNLOOK% changed -t %TXN% %REPOS% | findstr /R /I ".ext1$" 
if %ERRORLEVEL% EQU 1 goto CHECK2 
echo Your commit has been blocked because you are trying to commit a restricted file. Restricted formats %FORMATS% >&2 
exit /b 1 

:CHECK2 
%SVNLOOK% changed -t %TXN% %REPOS% | findstr /R /I ".ext2$" 
if %ERRORLEVEL% EQU 1 goto CHECK3 
echo Your commit has been blocked because you are trying to commit a restricted file. Restricted formats %FORMATS% >&2 
exit /b 1 

:CHECK3 
%SVNLOOK% changed -t %TXN% %REPOS% | findstr /R /I ".ext3$" 
if %ERRORLEVEL% EQU 1 goto OK 
echo Your commit has been blocked because you are trying to commit a restricted file. Restricted formats %FORMATS% >&2 
exit /b 1 

:OK 
exit /b 0