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

Last change on this file since 1887 was 1887, checked in by lolbot, 7 years ago

build: improve source checking script for lolbot.

  • Property svn:executable set to *
File size: 3.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# 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/*|people/*|*/generated/*)
78          : # These files aren't ours, don't fix
79          ;;
80      *.c|*.cpp|*.h|*.l|*.y)
81          clean=true
82
83          # Check for CR LF
84          ncrs="$(od -tx1 "$file" | cut -b8- | tr ' ' '\n' | grep -c 0d || true)"
85          total_crs="$(($total_crs + $ncrs))"
86          if [ "$ncrs" -gt 0 ]; then
87              clean=false
88              if [ "$fix" = true ]; then
89                  $d2u -q "$file"
90                  info "$file has $ncrs CR characters"
91              else
92                  error "$file has $ncrs CR characters"
93              fi
94          fi
95
96          # Check for trailing spaces
97          nspaces="$(sed 's/.*[^ \t]//' "$file" | tr -cd '\t ' | wc -c)"
98          total_spaces="$(($total_spaces + $nspaces))"
99          if [ "$nspaces" -gt 0 ]; then
100              clean=false
101              if [ "$fix" = true ]; then
102                  sed -i 's/[[:space:]][[:space:]]*$//g' "$file"
103                  info "$file has $nspaces trailing spaces"
104              else
105                  error "$file has $nspaces trailing spaces"
106              fi
107          fi
108
109          # Check for tabs
110          ntabs="$(tr -cd '\t' < "$file" | wc -c)"
111          total_tabs="$(($total_tabs + $ntabs))"
112          if [ "$ntabs" -gt 0 ]; then
113              clean=false
114              if [ "$fix" = true ]; then
115                  sed -i 's/\t/    /g' "$file"
116                  info "$file has $ntabs tabs"
117              else
118                  error "$file has $ntabs tabs"
119              fi
120          fi
121
122          total_files="$(($total_files + 1))"
123          if [ "$clean" != true ]; then
124              total_errors="$(($total_errors + 1))"
125          fi
126          ;;
127    esac
128done
129IFS="$OIFS"
130
131if [ "$total_errors" -gt 0 ]; then
132    if [ "$commit" = "true" ]; then
133        # EITHER: commit all modified files
134        svn commit --username lolbot --non-interactive -F - << EOF
135fixed $total_errors files out of $total_files:
136 - fixed $total_crs CR characters
137 - fixed $total_spaces trailing spaces
138 - fixed $total_tabs tabs
139EOF
140    elif [ "$fix" = "true" ]; then
141        # OR: report in stdout
142        info "fixed $total_errors files out of $total_files:"
143        if [ "$total_crs" -gt 0 ]; then
144            info " - fixed $total_crs CR characters"
145        fi
146        if [ "$total_spaces" -gt 0 ]; then
147            info " - fixed $total_spaces trailing spaces"
148        fi
149        if [ "$total_tabs" -gt 0 ]; then
150            info " - fixed $total_tabs tabs"
151        fi
152    else
153        # OR: warn about how to fix errors
154        info "re-run with -w to fix errors"
155    fi
156fi
157
Note: See TracBrowser for help on using the repository browser.