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

Last change on this file since 2739 was 2739, checked in by sam, 8 years ago

build: fix CRLF issues in user directories even if they're whitelisted, or git-svn
users will get all sorts of weird conflicts.

  • Property svn:executable set to *
File size: 5.2 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    can_check_crlf=false
66    ;;
67  *)
68    can_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    should_check_crlf=false
107    should_check_spaces=false
108
109    case "$file" in
110      # These files we know how to handle
111      *.c|*.cpp|*.m|*.mm|*.h|*.hh|*.lolfx|*.lua|*.l|*.y|*.sh|*.py)
112          should_check_crlf=true
113          should_check_spaces=true
114          ;;
115      *)
116          continue
117          ;;
118    esac
119
120    case "$file" in
121      # These files aren't ours, don't fix
122      src/bullet/*|\
123      src/lua/*|\
124      external/*|\
125      */generated/*|\
126      web/plugins/*)
127          continue
128          ;;
129    esac
130
131    case "$file" in
132      # Don't harass these people, but fix their line endings
133      people/peeweek/*|\
134      people/touky/*|\
135      people/benlitz/*|\
136      people/sam/lua-*)
137          should_check_spaces=false
138          ;;
139    esac
140
141    clean=true
142
143    # Check for CR LF
144    if [ "$can_check_crlf" = true -a "$should_check_crlf" = true ]; then
145        ncrlfs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
146        total_crlfs="$(($total_crlfs + $ncrlfs))"
147        if [ "$ncrlfs" -gt 0 ]; then
148            clean=false
149            if [ "$fix" = true ]; then
150                $d2u -q "$file"
151                info "$file has $ncrlfs CR characters"
152            else
153                error "$file has $ncrlfs CR characters"
154            fi
155        fi
156    fi
157
158    # Check for trailing spaces
159    if [ "$should_check_spaces" = true ]; then
160        nspaces="$($SED 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
161        total_spaces="$(($total_spaces + $nspaces))"
162        if [ "$nspaces" -gt 0 ]; then
163            clean=false
164            if [ "$fix" = true ]; then
165                $SED -i 's/[[:space:]][[:space:]]*$//g' "$file"
166                info "$file has $nspaces trailing spaces"
167            else
168                error "$file has $nspaces trailing spaces"
169            fi
170        fi
171    fi
172
173    # Check for tabs
174    if [ "$should_check_spaces" = true ]; then
175        ntabs="$(tr -cd '\t' < "$file" | wc -c)"
176        total_tabs="$(($total_tabs + $ntabs))"
177        if [ "$ntabs" -gt 0 ]; then
178            clean=false
179            if [ "$fix" = true ]; then
180                $SED -i 's/\t/    /g' "$file"
181                info "$file has $ntabs tabs"
182            else
183                error "$file has $ntabs tabs"
184            fi
185        fi
186    fi
187
188    if [ "$clean" != true ]; then
189        total_errors="$(($total_errors + 1))"
190    fi
191done
192IFS="$OIFS"
193
194if [ "$total_errors" -gt 0 ]; then
195    if [ "$commit" = "true" ]; then
196        # EITHER: commit all modified files
197        svn commit --username lolbot --non-interactive -F - << EOF
198fixed $total_errors files out of $total_files:
199 - removed $total_crlfs CR characters
200 - removed $total_spaces trailing whitespaces
201 - replaced $total_tabs tabs with spaces
202EOF
203    elif [ "$fix" = "true" ]; then
204        # OR: report in stdout
205        info "fixed $total_errors files out of $total_files:"
206        if [ "$total_crlfs" -gt 0 ]; then
207            info " - fixed $total_crlfs CR characters"
208        fi
209        if [ "$total_spaces" -gt 0 ]; then
210            info " - fixed $total_spaces trailing spaces"
211        fi
212        if [ "$total_tabs" -gt 0 ]; then
213            info " - fixed $total_tabs tabs"
214        fi
215        info "re-run with -c to commit fixes"
216    else
217        # OR: warn about how to fix errors
218        info "re-run with -w to fix errors"
219    fi
220else
221    info "all $total_files source files appear to be OK, congratulations"
222fi
223
Note: See TracBrowser for help on using the repository browser.