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

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

build: do not try to fix formatting in people/benlitz/*.

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