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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
#!/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_FILES\n$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_FILES\n$FILE" fi fi if [ -n $DO_PHP_SHORT_TAG_CHECK ]; then # check php short tags VIOLATIONS=`grep -rn '<!--?[^p]' $TMPFILE_ORIG` -->&2 if [ -n "$VIOLATIONS" ]; then PHP_SHORT_TAG_FILES="$PHP_SHORT_TAG_FILES\n$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<!--?php'` -->&2 if [ -n "$VIOLATIONS" ]; then SPACE_BEFOR_PHP_FILES="$SPACE_BEFOR_PHP_FILES\n$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 |