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