xref: /isa-l_crypto/tools/check_format.sh (revision dcb32c4b921d3e13f66c80b6ea64cb957fd8e989)
1#!/usr/bin/env bash
2
3set -e
4rc=0
5verbose=0
6clang_format_min_version=18
7
8function clang_format_version() {
9    version_str=$($clang_format --version)
10    regex="[0-9]+"
11    if [[ $version_str =~ $regex ]]; then
12        major_version="${BASH_REMATCH[0]}"
13	echo $major_version
14    fi
15}
16
17# set clang-format binary if not set externally
18if [[ -z $CLANGFORMAT ]]; then
19    clang_format="clang-format"
20else
21    clang_format=$CLANGFORMAT
22fi
23
24while [ -n "$*" ]; do
25    case "$1" in
26	-v )
27	    verbose=1
28	    shift
29	    ;;
30	-h )
31	    echo check_format.sh [-h -v]
32	    exit 0
33	    ;;
34    esac
35done
36
37echo "Checking format of files in the git index at $PWD"
38if ! git rev-parse --is-inside-work-tree >& /dev/null; then
39    echo "Not in a git repo: Fail"
40    exit 1
41fi
42
43if [ $(clang_format_version) -ge $clang_format_min_version ]; then
44    echo "Checking C files for coding style (clang-format v$(clang_format_version))..."
45    for f in `git ls-files '*.[c|h]'`; do
46	[ "$verbose" -gt 0 ] && echo "checking style on $f"
47	if ! $clang_format -style=file --dry-run --Werror "$f" >/dev/null 2>&1; then
48	    echo "  File found with formatting issues: $f"
49	    [ "$verbose" -gt 0 ] && $clang_format -style=file --dry-run "$f"
50	    rc=1
51	fi
52    done
53    [ "$rc" -gt 0 ] && echo "  Run ./tools/format.sh to fix formatting issues"
54else
55    echo "You do not have clang-format version ${clang_format_min_version}+" \
56	"installed so your code style is not being checked!"
57fi
58
59if hash grep; then
60    echo "Checking for dos and whitespace violations..."
61    for f in $(git ls-files -- ':(exclude)intel-ipsec-mb*'); do
62	[ "$verbose" -gt 0 ] && echo "checking whitespace on $f"
63	if grep -q '[[:space:]]$' $f ; then
64	    echo "  File found with trailing whitespace: $f"
65	    rc=1
66	fi
67	if grep -q $'\r' $f ; then
68	    echo "  File found with dos formatting: $f"
69	    rc=1
70	fi
71    done
72fi
73
74echo "Checking source files for permissions..."
75while read -r perm _res0 _res1 f; do
76    [ -z "$f" ] && continue
77    [ "$verbose" -gt 0 ] && echo "checking permissions on $f"
78    if [ "$perm" -ne 100644 ]; then
79	echo "  File found with permissions issue ($perm): $f"
80	rc=1
81    fi
82done <<< $(git ls-files -s -- ':(exclude)*.sh' ':(exclude)intel-ipsec-mb*')
83
84echo "Checking script files for permissions..."
85while read -r perm _res0 _res1 f; do
86    [ -z "$f" ] && continue
87    [ "$verbose" -gt 0 ] && echo "checking permissions on $f"
88    if [ "$perm" -ne 100755 ]; then
89	echo "  Script found with permissions issue ($perm): $f"
90	rc=1
91    fi
92done <<< $(git ls-files -s '*.sh')
93
94
95echo "Checking for signoff in commit message..."
96if ! git log -n 1 --format=%B --no-merges  | grep -q "^Signed-off-by:" ; then
97    hash=`git rev-parse --short HEAD`
98    echo "  Commit $hash not signed off. Please read src/CONTRIBUTING.md"
99    rc=1
100fi
101
102[ "$rc" -gt 0 ] && echo Format Fail || echo Format Pass
103
104exit $rc
105