-
Notifications
You must be signed in to change notification settings - Fork 542
/
checkstyle.sh
executable file
·63 lines (55 loc) · 1.78 KB
/
checkstyle.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env bash
# A simple script for checking coding style
set -eu
shopt -s extglob
### prepare to check and output violations ####################################
export PAGER=
# use colored output (red, green) for tty
if [[ -t 1 ]]; then
color() {
local color=$1; shift
case $color in
red) echo -ne $'\E[0;31m' ;; # use red
green) echo -ne $'\E[0;32m' ;; # use green
esac
"$@"
echo -ne $'\E[0m' # and reset color
}
else
color() { shift; "$@"; }
fi
CHECKING() {
LASTCHECK=$1
echo >&2 "# CHECKING CODING STYLE: $LASTCHECK"
trap 'c=$?; color red echo >&2 "# CODING STYLE VIOLATIONS: $LASTCHECK"; exit $c' ERR
}
violations=$(mktemp ${TMPDIR:-/tmp}/checkstyle.XXXXXX)
NO() {
"$@" >"$violations" || true
if [[ -s "$violations" ]]; then
color red cat "$violations"
return 1
fi
}
### check coding style by finding violations ##################################
textfiles=(
$(git ls-files | xargs file --mime-type | grep text/ | cut -d: -f1)
)
CHECKING "All text files should have EOL (end of line) at the end"
NO find "${textfiles[@]}" -type f \
-exec sh -c '[ `tail -1 "$1" | wc -l` -eq 0 ]' -- {} \; -print
CHECKING "All text files should have Unix line endings"
NO git grep -l -I $'\r''$'
CHECKING "No text file should have trailing spaces"
NO grep -l '[[:space:]]\+$' -- "${textfiles[@]}"
CHECKING "All code should use spaces instead of tab character"
NO git grep -l -I '^[[:space:]]*'$'\t' -- \
src/{main,test}/{scala,python}/ \
util/ \
ddlib/!(Makefile) \
examples/*{,/*}/{udf/,*.{sh,py,md}} \
mln/ \
doc/!(Makefile) \
#
###############################################################################
color green echo >&2 "# NO CODING STYLE VIOLATIONS! :)"