SVN pre-commit hook to restrict file extensions fr

2019-07-07 00:54发布

问题:

Using the following code in the Collabnet SVN's pre commit script to restrict the specific file extensions to get committed, but it is committing all the files. Could you please tell where I'm going wrong.

#!/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

回答1:

Below script works for me!! Do let me know your thoughts

#!/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


回答2:

I have the same problem as @San, but the answer from @Jijo doesn't directly help on my case because the script doesn't work for me as I am running the SVN server on AIX, I have enhance the script as shown below that could suit for my case. Anyhow thanks @Jijo sharing out the script.

#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


回答3:

Solution for Windows pre-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