Uncategorized

SVN Hooks

Hi there, within a big php project you make typos. here is a small prehook script for svn that will actually prevent commits with several common and annoying mistakes:

  • Spaces in filenames
  • Spaces prior to the first <?php tag
  • PHP Short Tags
  • windows line endings

[bash]#!/bin/bash

# configuration
DO_DOS_FILE_CHECK=1
DO_SPACE_FILE_CHECK=1
DO_PHP_SHORT_TAG_CHECK=1
DO_SPACE_BEFOR_PHP_CHECK=1

REPOS=”$1″
TXN=”$2″

SVNLOOK=/usr/bin/svnlook
TMPFILE_ORIG=/tmp/svnhook_$TXN
TMPFILE_UNIX=/tmp/svnhook_unix_$TXN

FILES=`$SVNLOOK changed “$REPOS” -t “$TXN” | sed -r ‘s/^[D].*$//;s/^[AU]{1,2}s+//’`
DOS_FILES=””
SPACE_FILES=””
PHP_SHORT_TAG_FILES=””
SPACE_BEFOR_PHP_FILES=””

IFS=$’n’

for FILE in $FILES
do
LAST=${FILE#${FILE%?}}

# echo “FILE = ‘$FILE'” >&2

if [ “$LAST” = “/” ]; then
continue
else
if [ -n $DO_SPACE_FILE_CHECK ]; then
# space in filename check
SPACE_VIOLATIONS=`echo “$FILE” | grep -l ” “`

if [ -n “$SPACE_VIOLATIONS” ]; then
SPACE_FILES=”$SPACE_FILESn$FILE”
continue
fi
fi

$SVNLOOK cat “$REPOS” -t “$TXN” “$FILE” > $TMPFILE_ORIG || continue

if [ -n $DO_DOS_FILE_CHECK ]; then
# check windows line endings
VIOLATIONS=`grep -cIls $’r$’ $TMPFILE_ORIG` >&2

if [ -n “$VIOLATIONS” ]; then
DOS_FILES=”$DOS_FILESn$FILE”
fi
fi

if [ -n $DO_PHP_SHORT_TAG_CHECK ]; then
# check php short tags
VIOLATIONS=`grep -rn ‘&2
if [ -n “$VIOLATIONS” ]; then
PHP_SHORT_TAG_FILES=”$PHP_SHORT_TAG_FILESn$FILE: $VIOLATIONS”
fi
fi

if [ -n $DO_SPACE_BEFOR_PHP_CHECK ]; then
# check for space prior to php tag
VIOLATIONS=`head -n 1 $TMPFILE_ORIG | grep ‘^s&2
if [ -n “$VIOLATIONS” ]; then
SPACE_BEFOR_PHP_FILES=”$SPACE_BEFOR_PHP_FILESn$FILE”
fi
fi
fi
done

rm -f $TMPFILE_ORIG
rm -f $TMPFILE_UNIX

if [ “$DOS_FILES” != “” ] || [ “$SPACE_FILES” != “” ] || [ “$SPACE_BEFOR_PHP_FILES” != “” ] || [ “$PHP_SHORT_TAG_FILES” != “” ]; then
echo -e “n” >&2
if [ “$DOS_FILES” != “” ]; then
echo “ONLY UNIX LINEENDINGS ALLOWED!!!” >&2
echo “the following file contains Windows / Mac Lineendings:” >&2
echo -e $DOS_FILES >&2
echo -e “n” >&2
fi

if [ “$SPACE_FILES” != “” ]; then
echo “SPACES ARE NOT ALLOWED IN FILE / FOLDER NAMES!!!” >&2
echo “the following file contain spaces:” >&2
echo -e $SPACE_FILES >&2
echo -e “n” >&2
fi

if [ “$SPACE_BEFOR_PHP_FILES” != “” ]; then
echo “SPACES ARE NOT ALLOWED BEFOR THE PHP TAG IN THE FIRST LINE!!!” >&2
echo “the following file contain spaces:” >&2
echo -e $SPACE_BEFOR_PHP_FILES >&2
echo -e “n” >&2
fi

if [ “$PHP_SHORT_TAG_FILES” != “” ]; then
echo “SHORT PHP TAGS ARE NOT ALLOWED!!!” >&2
echo “the following file contain short tags” >&2
echo -e $PHP_SHORT_TAG_FILES >&2
echo -e “n” >&2
fi

exit 2
fi

# All checks passed, so allow the commit.
exit 0
[/bash]

Leave a comment