3

I'm trying to write a svn pre-commit hook which will give an error if certain keywords exist in certain file types.

The case for me is, if the file is a .java, .jsp or .jspf file I want to make sure that "http://" and "https://" do not exist in them. So far, I can throw an error if the keyword exists in any file, but not JUST the filetypes I want to check.

Here's what I have so far:

$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; }
Ben Carlson
  • 762
  • 8
  • 18

2 Answers2

1

I figured it out using a combination of svnlook changed and svnlook 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
Ben Carlson
  • 762
  • 8
  • 18
0

Yes, You can do it very easily.

Here is the link for such scripts http://www.scmtechblog.net/2014/07/few-pre-commit-svn-scripts-and-tricks.html

Look at the "Detect merge conflict" You can change the conflict marker to your desirable text.

Above will check for the delta of the files which are being changed.

If you want to check the whole file you can have a look at following script Look at Puppet "file parsing" You can modify the script to grep for your text.

  • Hi Vishal, this is close, but other than being much nicer written than the one I posted, it doesn't do what I need, which is to check for the keyword in ONLY certain file types. – Ben Carlson Apr 20 '15 at 16:56
  • Hi Ben, Yes file type can also be get, there is script in same blog "Binary file check" to check the binary file, You can change it and check for the file type you want. I know i am not providing exact script which you are looking for, but combining both scripts and with little change you can achieve it. – vishal sahasrabuddhe Apr 22 '15 at 04:30
  • Thanks Vishal, the problem is, each script uses a different method to find the differences. One looks only at the file names, the other only the contents of the files. I'll post my solution (assuming I find one! :) when I arrive at it. – Ben Carlson Apr 23 '15 at 03:36