File: syntax-check.sh

package info (click to toggle)
subversion 1.5.1dfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 41,860 kB
  • ctags: 43,900
  • sloc: ansic: 522,648; python: 64,596; sh: 12,784; ruby: 11,838; cpp: 9,584; java: 8,130; lisp: 7,131; perl: 5,686; makefile: 895; xml: 759
file content (168 lines) | stat: -rwxr-xr-x 5,551 bytes parent folder | download | duplicates (4)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
#
# SVN Syntax Check Hook Script
# Copyright (c) 2007, Lucas Nealan <lucas@sizzo.org>, Facebook Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
# 02110-1301  USA
#
# --------------------------------------------------------------------
#
# This script provides language independant syntax checking 
# functionality intended to be invoked from a subversion pre-commit
# hook.
#
# Invocation: /path/to/syntax-check $1 $2
#         or: source syntax-check 
#
# Requires bash 3.x or higher.
#

FPATTERN="\.\(php\|phpt\)$"
FLANG="PHP" 
SYNTAX_CMD="php"
SYNTAX_ARGS="-l"
# address to email notifications of syntax errors
NOTIFY_SYNTAX="user@domain.tld"
# adderss to notify for syntax script failures
NOTIFY_ERROR="user@domain.tld" 
# log of syntax errors, must be writable by svn server users
SYNTAX_LOG="/tmp/syntax.log"
BYPASSPW="change_this_syntax_bypass_password_please"

[ -z "$REPOS" ] && REPOS="$1"
[ -z "$TXN" ]   && TXN="$2"
[ -z "$MODE" ]  && if [ -n "$3" ]; then MODE="$3"; else MODE="-t"; fi
[ -z "$SVNLOOK" ] && SVNLOOK=svnlook
[ -z "$LOG" ] && LOG=`$SVNLOOK log $MODE "$TXN" "$REPOS"`
[ -z "$DIFF" ] && DIFF=`$SVNLOOK diff $MODE "$TXN" "$REPOS"`
[ -z "$AUTHOR" ] && AUTHOR=`$SVNLOOK author $MODE "$TXN" "$REPOS"`
[ -z "$CHANGEDFILES" ] && CHANGEDFILES=`$SVNLOOK changed $MODE "$TXN" "$REPOS"`
[ -n "$SYNTAXENABLED" ] && SYNTAXENABLED="1"

syntaxclean() {
  [ -d $1 ] && rm -rf $1
}

syntaxexit() {
  echo $1 >> /dev/stderr
  echo $1 >> $SYNTAX_LOG

  # save working dir and debug files
  if [ -d $WORKING ]; then
    [ -n "$DIFF" ] && echo $"$DIFF" > $WORKING/patch
    [ -n "$LOG" ] && echo $"$LOG" > $WORKING/svnlog
    [ -n "$CHANGED" ] && echo $"$CHANGED" > $WORKING/changed
    mv $WORKING $WORKING.failed
  fi

  [ -n "$NOTIFY_ERROR" ] && echo "$1 ($WORKING.failed)" | mail -s "syntax commit failed" $NOTIFY_ERROR

  # clean working dir if specified
  [ -n $2 ] && syntaxclean $2
  exit 1
}

function strlpad() {
  STR="$1"
  CHAR=$2
  LEN=$3
  L=${#STR}
  D=$((LEN-L))
  echo "$STR""`printf '%'$D's'| tr \  \"$CHAR\"`"
}

function errormessage() {
  echo
  HEADER=`strlpad "Error" "-" 76`
  echo "|--$HEADER|" >&2
  SPACES="`strlpad '' ' ' 78`"
  echo "|$SPACES|" >&2
  ERROR=`strlpad "$1" " " 77`
  echo "| $ERROR|" >&2
  echo "|$SPACES|" >&2
  LINE=`strlpad "" "-" 78`
  echo "|$LINE|" >&2
  echo 
}

if [ "$SYNTAXENABLED" == "1" ]; then
  # allow selective bypass of syntax check for commits
  [[ "$LOG" =~ "$BYPASSPW" ]] && return;

  # get changed file list and count
  NUMMATCHCHANGED=0
  if [ -n "$CHANGEDFILES" ]; then
    MATCHCHANGED=`echo $"$CHANGEDFILES" | grep "$FPATTERN"`
    [ -n "$MATCHCHANGED" ] && NUMMATCHCHANGED=`echo $"$MATCHCHANGED" | wc -l`
  fi

  # make sure matched files were changed
  if [ $NUMMATCHCHANGED -gt 0 ]; then
    # create temporary working directory 
    WORKING=/tmp/$(basename $0).$$
    [ -d $WORKING ] && rm -rf $WORKING
    mkdir $WORKING || syntaxexit "failed to create temp dir for syntax check: $WORKING"
    cd $WORKING

    # export changed files (no dirs) from local repo (speed)
    IFS=$'\n'
    for LINE in $MATCHCHANGED; do
      IFS=' '
      WORDS=($LINE)
      FSTATUS=${WORDS[0]}
      FNAME=${WORDS[1]}

      # only export modified and deleted files. new files wont exist in repo yet
      if [ "$FSTATUS" == "U" ] || [ "$FSTATUS" == "UU" ] || [ "$FSTATUS" == "A" ]; then
        TMPFNAME=${FNAME//\//.}
        $SVNLOOK cat $MODE "$TXN" "$REPOS" $FNAME > $TMPFNAME

        file `which $SYNTAX_CMD` || syntaxexit "unablet to find systax command binary: $SYNTAX_CMD"
        SYNTAXERROR=`$SYNTAX_CMD $SYNTAX_ARGS $TMPFNAME 2> $WORKING/$TMPFNAME.STDERR`
        SYNTAXRETURN=$?
        [ -s "$WORKING/$TMPFNAME.STDERR" ] && SYNTAXWARNING=`cat $WORKING/$TMPFNAME.STDERR`

        if [ "$SYNTAXRETURN" -ne 0 ] || [ -n "$SYNTAXWARNING" ]; then
          [ -n "$SYNTAXWARNING" ] && SYNTAXERROR=$SYNTAXWARNING

          # cleanup HTML out of PHP parse error so a human can read it
          if [ "$FLANG" == "PHP" ]; then
            SYNTAXERROR=`echo $SYNTAXERROR | sed -e 's/<[^<]*>//g' | cut -d',' -f 2`
            SYNTAXERROR=`echo $SYNTAXERROR | sed -e 's/\(on line [0-9]* \)/\1\n/g'`
          fi
          
          # sloppy email notification
          ETMP=$WORKING/sloppy.txt
          echo "$FLANG Syntax Error: $SYNTAXERROR" > $ETMP
          echo >> $ETMP
          echo Log: $LOG >> $ETMP
          echo >> $ETMP
          echo $"$DIFF" >> $ETMP
          cat $ETMP | mail -s "SVN SYNTAX ERROR: $AUTHOR" $NOTIFY_SYNTAX
          rm -f $ETMP

          echo "$AUTHOR: $FLANG Syntax Error: $SYNTAXERROR" >> $SYNTAX_LOG
          errormessage "$FLANG Syntax Error: $SYNTAXERROR"
          syntaxclean $WORKING
          exit 1
        fi
      fi
    done
    # exit within a loop only sets the return value of the loop itself, check this to exit
    [ $? -ne 0 ] && exit 1
  fi 
  syntaxclean $WORKING
fi