Coding standards cleanup - remove trailing whitespace, fixes VPP-332
[vpp.git] / build-root / scripts / checkstyle.sh
1 #!/bin/bash
2
3 VPP_DIR=`dirname $0`/../../
4 EXIT_CODE=0
5 FIX="0"
6 CHECKSTYLED_FILES=""
7 UNCHECKSTYLED_FILES=""
8
9 # If the user provides --fix, then actually fix things
10 # Note: this is meant for use outside of the CI Jobs, by users cleaning things up
11
12 if [ $# -gt 0 ] && [ ${1} == '--fix' ]; then
13     FIX="1"
14 fi
15
16 # Check to make sure we have indent.  Exit if we don't with an error message, but
17 # don't *fail*.
18 command -v indent > /dev/null
19 if [ $? != 0 ]; then
20     echo "Cound not find required commend \"indent\".  Checkstyle aborted"
21     exit ${EXIT_CODE}
22 fi
23 indent --version
24
25 cd ${VPP_DIR}
26 git status
27 for i in `git ls-tree -r HEAD --name-only`;do
28     if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "build-root/emacs-lisp/fix-coding-style.el" ]; then
29         grep -q "fd.io coding-style-patch-verification: ON" ${i}
30         if [ $? == 0 ]; then
31             CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
32             if [ ${FIX} == 0 ]; then
33                 indent ${i} -o ${i}.out1 > /dev/null 2>&1
34                 indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
35                 # Remove trailing whitespace
36                 sed -i -e 's/[[:space:]]*$//' ${i}.out2
37                 diff -q ${i} ${i}.out2
38             else
39                 indent ${i}
40                 indent ${i}
41                 # Remove trailing whitespace
42                 sed -i -e 's/[[:space:]]*$//' ${i}
43             fi
44             if [ $? != 0 ]; then
45                 EXIT_CODE=1
46                 echo
47                 echo "Checkstyle failed for ${i}."
48                 echo "Run indent (twice!) as shown to fix the problem:"
49                 echo "indent ${VPP_DIR}${i}"
50                 echo "indent ${VPP_DIR}${i}"
51             fi
52             if [ -f ${i}.out1 ]; then
53                 rm ${i}.out1
54             fi
55             if [ -f ${i}.out2 ]; then
56                 rm ${i}.out2
57             fi
58         else
59             UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
60         fi
61     else
62         UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
63     fi
64 done
65
66 if [ ${EXIT_CODE} == 0 ]; then
67     echo "*******************************************************************"
68     echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
69     echo "*******************************************************************"
70 else
71     echo "*******************************************************************"
72     echo "* VPP CHECKSTYLE FAILED"
73     echo "* CONSULT FAILURE LOG ABOVE"
74     echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
75     echo "*******************************************************************"
76 fi
77 exit ${EXIT_CODE}