svngreppre-commit-hooksvn-hookspre-commit

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


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

Solution

  • 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