VPP-237: Checkstyle script to check for new checkstyle breakage
[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                 diff -q ${i} ${i}.out2
36             else
37                 indent ${i}
38                 indent ${i}
39             fi
40             if [ $? != 0 ]; then
41                 EXIT_CODE=1
42                 echo
43                 echo "Checkstyle failed for ${i}."
44                 echo "Run indent (twice!) as shown to fix the problem:"
45                 echo "indent ${VPP_DIR}${i}"
46                 echo "indent ${VPP_DIR}${i}"
47             fi
48             if [ -f ${i}.out1 ]; then
49                 rm ${i}.out1
50             fi
51             if [ -f ${i}.out2 ]; then
52                 rm ${i}.out2
53             fi
54         else
55             UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
56         fi
57     else
58         UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
59     fi
60 done
61
62 if [ ${EXIT_CODE} == 0 ]; then
63     echo "*******************************************************************"
64     echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
65     echo "*******************************************************************"
66 else
67     echo "*******************************************************************"
68     echo "* VPP CHECKSTYLE FAILED"
69     echo "* CONSULT FAILURE LOG ABOVE"
70     echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
71     echo "*******************************************************************"
72 fi
73 exit ${EXIT_CODE}