source: trunk/build/check-source.sh @ 1900

Last change on this file since 1900 was 1900, checked in by sam, 7 years ago

build: do not check for CRLF on Windows, since most files would have been
converted by the Vcs. Also ignore Trac plugins.

  • Property svn:executable set to *
File size: 4.5 KB
Line 
1#!/bin/sh
2
3set -e
4
5fix=false
6quiet=false
7while [ "$#" -gt 0 ]; do
8    case "$1" in
9      -q)
10        quiet=true
11        ;;
12      -w)
13        fix=true
14        ;;
15      -c)
16        commit=true
17        fix=true
18        quiet=true
19        ;;
20      *)
21        echo "E: invalid argument $1"
22        exit 1
23        ;;
24    esac
25    shift
26done
27
28error() { if [ "$quiet" != true ]; then echo "E: $1"; fi }
29info() { if [ "$quiet" != true ]; then echo "I: $1"; fi }
30
31# Find out where the top directory is and go there
32top_srcdir="$(cd "$(dirname $0)"; cd ..; pwd)"
33cd "$top_srcdir"
34
35# Check for working tools
36#if [ "$(echo foo | grep -c foo)" != 1 ]; then
37#    error "grep -c does not appear to work, cancelling"
38#    exit 0
39#fi
40if d2u -h >/dev/null 2>&1; then
41    d2u=d2u
42elif dos2unix -h >/dev/null 2>&1; then
43    d2u=dos2unix
44else
45    error "d2u or dos2unix not found, cancelling"
46    exit 0
47fi
48
49# Find out whether we need to care about CRLF
50case "$(uname 2>/dev/null)" in
51  MINGW*)
52    check_crlf=false
53    ;;
54  *)
55    check_crlf=true
56    ;;
57esac
58
59# Find out what kind of Vcs directory this is
60if [ -f "$top_srcdir/.git/index" ]; then
61    info "detected Git repository"
62    repo=git
63elif [ -f "$top_srcdir/.svn/format" ]; then
64    info "detected SVN repository"
65    repo=svn
66else
67    info "not in a Vcs directory, nothing to do"
68    exit 0
69fi
70
71total_crlfs=0
72total_spaces=0
73total_tabs=0
74
75OIFS="$IFS"
76IFS=$'\n'
77if [ "$repo" = git ]; then
78    FILES="`git ls-files`"
79else
80    FILES="`svn ls -R`"
81fi
82
83total_files=0
84for file in $FILES; do
85    if [ -f "$file" ]; then
86        total_files="$(($total_files + 1))"
87    fi
88done
89
90total_errors=0
91for file in $FILES; do
92    case "$file" in
93      # These files aren't ours, don't fix
94      src/bullet/*|contrib/*|*/generated/*|web/plugins/*)
95          :
96          ;;
97      # Don't harass these people
98      people/peeweek/*|people/touky/*)
99          :
100          ;;
101      # These files we know how to handle
102      *.c|*.cpp|*.m|*.mm|*.h|*.hh|*.lolfx|*.l|*.y|*.sh|*.py)
103          clean=true
104
105          # Check for CR LF
106          if [ "$check_crlf" = true ]; then
107              ncrlfs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
108              total_crlfs="$(($total_crlfs + $ncrlfs))"
109              if [ "$ncrlfs" -gt 0 ]; then
110                  clean=false
111                  if [ "$fix" = true ]; then
112                      $d2u -q "$file"
113                      info "$file has $ncrlfs CR characters"
114                  else
115                      error "$file has $ncrlfs CR characters"
116                  fi
117              fi
118          fi
119
120          # Check for trailing spaces
121          nspaces="$(sed 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
122          total_spaces="$(($total_spaces + $nspaces))"
123          if [ "$nspaces" -gt 0 ]; then
124              clean=false
125              if [ "$fix" = true ]; then
126                  sed -i 's/[[:space:]][[:space:]]*$//g' "$file"
127                  info "$file has $nspaces trailing spaces"
128              else
129                  error "$file has $nspaces trailing spaces"
130              fi
131          fi
132
133          # Check for tabs
134          ntabs="$(tr -cd '\t' < "$file" | wc -c)"
135          total_tabs="$(($total_tabs + $ntabs))"
136          if [ "$ntabs" -gt 0 ]; then
137              clean=false
138              if [ "$fix" = true ]; then
139                  sed -i 's/\t/    /g' "$file"
140                  info "$file has $ntabs tabs"
141              else
142                  error "$file has $ntabs tabs"
143              fi
144          fi
145
146          if [ "$clean" != true ]; then
147              total_errors="$(($total_errors + 1))"
148          fi
149          ;;
150    esac
151done
152IFS="$OIFS"
153
154if [ "$total_errors" -gt 0 ]; then
155    if [ "$commit" = "true" ]; then
156        # EITHER: commit all modified files
157        svn commit --username lolbot --non-interactive -F - << EOF
158fixed $total_errors files out of $total_files:
159 - removed $total_crlfs CR characters
160 - removed $total_spaces trailing whitespaces
161 - replaced $total_tabs tabs with spaces
162EOF
163    elif [ "$fix" = "true" ]; then
164        # OR: report in stdout
165        info "fixed $total_errors files out of $total_files:"
166        if [ "$total_crlfs" -gt 0 ]; then
167            info " - fixed $total_crlfs CR characters"
168        fi
169        if [ "$total_spaces" -gt 0 ]; then
170            info " - fixed $total_spaces trailing spaces"
171        fi
172        if [ "$total_tabs" -gt 0 ]; then
173            info " - fixed $total_tabs tabs"
174        fi
175    else
176        # OR: warn about how to fix errors
177        info "re-run with -w to fix errors"
178    fi
179else
180    info "all $total_files source files appear to be OK, congratulations"
181fi
182
Note: See TracBrowser for help on using the repository browser.