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

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

test: tell lolbot to fix jnat's code, too.

  • Property svn:executable set to *
File size: 4.0 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 what kind of Vcs directory this is
50if [ -f "$top_srcdir/.git/index" ]; then
51    info "detected Git repository"
52    repo=git
53elif [ -f "$top_srcdir/.svn/format" ]; then
54    info "detected SVN repository"
55    repo=svn
56else
57    info "not in a Vcs directory, nothing to do"
58    exit 0
59fi
60
61total_errors=0
62total_files=0
63total_crs=0
64total_spaces=0
65total_tabs=0
66
67OIFS="$IFS"
68IFS=$'\n'
69if [ "$repo" = git ]; then
70    FILES="`git ls-files`"
71else
72    FILES="`svn ls -R`"
73fi
74
75for file in $FILES; do
76    case "$file" in
77      src/bullet/*|contrib/*|*/generated/*)
78          : # These files aren't ours, don't fix
79          ;;
80      people/peeweek/*|people/touky/*)
81          : # Don't harass these people
82          ;;
83      *.c|*.cpp|*.h|*.l|*.y)
84          clean=true
85
86          # Check for CR LF
87          ncrs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
88          total_crs="$(($total_crs + $ncrs))"
89          if [ "$ncrs" -gt 0 ]; then
90              clean=false
91              if [ "$fix" = true ]; then
92                  $d2u -q "$file"
93                  info "$file has $ncrs CR characters"
94              else
95                  error "$file has $ncrs CR characters"
96              fi
97          fi
98
99          # Check for trailing spaces
100          nspaces="$(sed 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
101          total_spaces="$(($total_spaces + $nspaces))"
102          if [ "$nspaces" -gt 0 ]; then
103              clean=false
104              if [ "$fix" = true ]; then
105                  sed -i 's/[[:space:]][[:space:]]*$//g' "$file"
106                  info "$file has $nspaces trailing spaces"
107              else
108                  error "$file has $nspaces trailing spaces"
109              fi
110          fi
111
112          # Check for tabs
113          ntabs="$(tr -cd '\t' < "$file" | wc -c)"
114          total_tabs="$(($total_tabs + $ntabs))"
115          if [ "$ntabs" -gt 0 ]; then
116              clean=false
117              if [ "$fix" = true ]; then
118                  sed -i 's/\t/    /g' "$file"
119                  info "$file has $ntabs tabs"
120              else
121                  error "$file has $ntabs tabs"
122              fi
123          fi
124
125          total_files="$(($total_files + 1))"
126          if [ "$clean" != true ]; then
127              total_errors="$(($total_errors + 1))"
128          fi
129          ;;
130    esac
131done
132IFS="$OIFS"
133
134if [ "$total_errors" -gt 0 ]; then
135    if [ "$commit" = "true" ]; then
136        # EITHER: commit all modified files
137        svn commit --username lolbot --non-interactive -F - << EOF
138fixed $total_errors files out of $total_files:
139 - fixed $total_crs CR characters
140 - fixed $total_spaces trailing spaces
141 - fixed $total_tabs tabs
142EOF
143    elif [ "$fix" = "true" ]; then
144        # OR: report in stdout
145        info "fixed $total_errors files out of $total_files:"
146        if [ "$total_crs" -gt 0 ]; then
147            info " - fixed $total_crs CR characters"
148        fi
149        if [ "$total_spaces" -gt 0 ]; then
150            info " - fixed $total_spaces trailing spaces"
151        fi
152        if [ "$total_tabs" -gt 0 ]; then
153            info " - fixed $total_tabs tabs"
154        fi
155    else
156        # OR: warn about how to fix errors
157        info "re-run with -w to fix errors"
158    fi
159fi
160
Note: See TracBrowser for help on using the repository browser.