xref: /isa-l/tools/check_format.sh (revision 1187583a979dcd98945c8f01905140c9b78d8d11)
1#!/usr/bin/env bash
2
3set -e
4rc=0
5verbose=0
6# NOTE: there is a bug in GNU indent command line parse where it says, that
7#       -il6 require numeric parameter. This is because it treat it like "-i"
8#       param. Here we pass -i8 which is default for linux code style and
9#       we use long parameter name for indent label.
10indent_args='-i8 -linux -l95 -cp1 -lps -ncs --indent-label6'
11function iver { printf "%03d%03d%03d%03d" $(echo "$@" | sed 's/^.* indent//; y/./ /'); }
12
13while [ -n "$*" ]; do
14    case "$1" in
15	-v )
16	    verbose=1
17	    shift
18	    ;;
19	-h )
20	    echo check_format.sh [-h -v]
21	    exit 0
22	    ;;
23    esac
24done
25
26echo "Checking format of files in the git index at $PWD"
27if ! git rev-parse --is-inside-work-tree >& /dev/null; then
28    echo "Not in a git repo: Fail"
29    exit 1
30fi
31
32# On FreeBSD we need to use gindent
33for indent_tool in indent gindent ''; do
34	if hash $indent_tool && [ $(iver $($indent_tool --version)) -ge $(iver 2.2.12) ]; then
35		break
36	fi
37done
38
39if [ -n "$indent_tool" ]; then
40    echo "Checking C files for coding style..."
41    for f in `git ls-files '*.c'`; do
42	[ "$verbose" -gt 0 ] && echo "checking style on $f"
43	if ! $indent_tool $indent_args -st $f | diff -q $f - >& /dev/null; then
44	    echo "  File found with formatting issues: $f"
45	    [ "$verbose" -gt 0 ] 2> /dev/null && $indent_tool $indent_args -st $f | diff -u $f -
46	    rc=1
47	fi
48    done
49    [ "$rc" -gt 0 ] && echo "  Run ./tools/iindent on files"
50else
51	echo "You do not have a recent indent installed so your code style is not being checked!"
52fi
53
54if hash grep; then
55    echo "Checking for dos and whitespace violations..."
56    for f in $(git ls-files); do
57	[ "$verbose" -gt 0 ] && echo "checking whitespace on $f"
58	if grep -q '[[:space:]]$' $f ; then
59	    echo "  File found with trailing whitespace: $f"
60	    rc=1
61	fi
62	if grep -q $'\r' $f ; then
63	    echo "  File found with dos formatting: $f"
64	    rc=1
65	fi
66    done
67fi
68
69echo "Checking source files for permissions..."
70while read -r perm _res0 _res1 f; do
71    [ -z "$f" ] && continue
72    [ "$verbose" -gt 0 ] && echo "checking permissions on $f"
73    if [ "$perm" -ne 100644 ]; then
74	echo "  File found with permissions issue ($perm): $f"
75	rc=1
76    fi
77done <<< $(git ls-files -s -- ':(exclude)*.sh' ':(exclude)*iindent')
78
79echo "Checking script files for permissions..."
80while read -r perm _res0 _res1 f; do
81    [ -z "$f" ] && continue
82    [ "$verbose" -gt 0 ] && echo "checking permissions on $f"
83    if [ "$perm" -ne 100755 ]; then
84	echo "  Script found with permissions issue ($perm): $f"
85	rc=1
86    fi
87done <<< $(git ls-files -s '*.sh')
88
89
90echo "Checking for signoff in commit message..."
91if ! git log -n 1 --format=%B --no-merges | grep -q "^Signed-off-by:" ; then
92    echo "  Commit not signed off. Please read src/CONTRIBUTING.md"
93    rc=1
94fi
95
96[ "$rc" -gt 0 ] && echo Format Fail || echo Format Pass
97
98exit $rc
99