]> git.lizzy.rs Git - minetest.git/blob - util/travis/lint.sh
LINT fix & check all files with clang-format
[minetest.git] / util / travis / lint.sh
1 #! /bin/bash
2 function perform_lint() {
3         echo "Performing LINT..."
4         if hash clang-format-3.9 2>/dev/null; then
5                 CLANG_FORMAT=clang-format-3.9
6         else
7                 CLANG_FORMAT=clang-format
8         fi
9         echo "LINT: Using binary $CLANG_FORMAT"
10         CLANG_FORMAT_WHITELIST="util/travis/clang-format-whitelist.txt"
11
12         files_to_lint="$(find src/ -name '*.cpp' -or -name '*.h')"
13
14         local errorcount=0
15         local fail=0
16         for f in ${files_to_lint}; do
17                 d=$(diff -u "$f" <(${CLANG_FORMAT} "$f") || true)
18
19                 if ! [ -z "$d" ]; then
20                         whitelisted=$(awk '$1 == "'$f'" { print 1 }' "$CLANG_FORMAT_WHITELIST")
21
22                         # If file is not whitelisted, mark a failure
23                         if [ -z ${whitelisted} ]; then
24                                 errorcount=$((errorcount+1))
25
26                                 printf "The file %s is not compliant with the coding style" "$f"
27                                 if [ ${errorcount} -gt 50 ]; then
28                                         printf "\nToo many errors encountered previously, this diff is hidden.\n"
29                                 else
30                                         printf ":\n%s\n" "$d"
31                                 fi
32
33                                 fail=1
34                         fi
35                 fi
36         done
37
38         if [ "$fail" = 1 ]; then
39                 echo "LINT reports failure."
40                 exit 1
41         fi
42
43         echo "LINT OK"
44 }
45