blob: f4ea79adc6d686ec65deb1054a7c9a42c8ccc405 [file] [log] [blame]
Ed Warnickebe053b82016-08-05 11:43:58 -07001#!/bin/bash
2
3VPP_DIR=`dirname $0`/../../
4EXIT_CODE=0
5FIX="0"
Damjan Marion24704852016-09-07 13:10:50 +02006FULL="0"
Ed Warnickebe053b82016-08-05 11:43:58 -07007CHECKSTYLED_FILES=""
8UNCHECKSTYLED_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
Damjan Marion24704852016-09-07 13:10:50 +020013while true; do
14 case ${1} in
15 --fix)
16 FIX="1"
17 ;;
18 --full)
19 FULL="1"
20 ;;
21 esac
22 shift || break
23done
24
25if [ "${FULL}" == "1" ]; then
26 FILELIST=$(git ls-tree -r HEAD --name-only)
27else
28 FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u)
Ed Warnickebe053b82016-08-05 11:43:58 -070029fi
30
31# Check to make sure we have indent. Exit if we don't with an error message, but
32# don't *fail*.
33command -v indent > /dev/null
34if [ $? != 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020035 echo "Cound not find required command \"indent\". Checkstyle aborted"
Ed Warnickebe053b82016-08-05 11:43:58 -070036 exit ${EXIT_CODE}
37fi
38indent --version
39
Klement Sekeradc15be22017-06-12 06:49:33 +020040# Check to make sure we have clang-format. Exit if we don't with an error message, but
41# don't *fail*.
Klement Sekera8a398bb2017-09-26 02:39:40 +020042HAVE_CLANG_FORMAT=0
Klement Sekera6b6bd9d2017-10-04 06:26:36 +020043command -v clang-format > /dev/null
Klement Sekeradc15be22017-06-12 06:49:33 +020044if [ $? != 0 ]; then
45 echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
Klement Sekeradc15be22017-06-12 06:49:33 +020046else
Klement Sekeradc15be22017-06-12 06:49:33 +020047 clang-format --version
Klement Sekera8a398bb2017-09-26 02:39:40 +020048 x=$(echo "" | clang-format 2>&1)
49 if [[ "$x" == "" ]]; then
50 HAVE_CLANG_FORMAT=1
51 else
52 echo "Output produced while formatting empty file (expected empty string):"
53 echo "$x"
54 echo "Could not find working \"clang-format\". Checking C++ files will cause abort"
55 fi
Klement Sekeradc15be22017-06-12 06:49:33 +020056fi
57
Ed Warnickebe053b82016-08-05 11:43:58 -070058cd ${VPP_DIR}
59git status
Damjan Marion24704852016-09-07 13:10:50 +020060for i in ${FILELIST}; do
Damjan Marion757585d2017-04-20 11:42:28 +020061 if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
Ed Warnickebe053b82016-08-05 11:43:58 -070062 grep -q "fd.io coding-style-patch-verification: ON" ${i}
63 if [ $? == 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020064 EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
65 case ${EXTENSION} in
66 hpp|cpp|cc|hh)
67 CMD="clang-format"
68 if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
69 echo "C++ file detected. Abort. (missing clang-format)"
70 exit ${EXIT_CODE}
71 fi
72 ;;
73 *)
74 CMD="indent"
75 ;;
76 esac
Ed Warnickebe053b82016-08-05 11:43:58 -070077 CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
78 if [ ${FIX} == 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020079 if [ "${CMD}" == "clang-format" ]
80 then
81 clang-format ${i} > ${i}.out2
82 else
83 indent ${i} -o ${i}.out1 > /dev/null 2>&1
84 indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
85 fi
86 # Remove trailing whitespace
87 sed -i -e 's/[[:space:]]*$//' ${i}.out2
Ed Warnickebe053b82016-08-05 11:43:58 -070088 diff -q ${i} ${i}.out2
89 else
Klement Sekeradc15be22017-06-12 06:49:33 +020090 if [ "${CMD}" == "clang-format" ]; then
91 clang-format -i ${i} > /dev/null 2>&1
92 else
93 indent ${i}
94 indent ${i}
95 fi
96 # Remove trailing whitespace
97 sed -i -e 's/[[:space:]]*$//' ${i}
Ed Warnickebe053b82016-08-05 11:43:58 -070098 fi
99 if [ $? != 0 ]; then
100 EXIT_CODE=1
101 echo
102 echo "Checkstyle failed for ${i}."
Klement Sekeradc15be22017-06-12 06:49:33 +0200103 if [ "${CMD}" == "clang-format" ]; then
104 echo "Run clang-format as shown to fix the problem:"
105 echo "clang-format -i ${VPP_DIR}${i}"
106 else
107 echo "Run indent (twice!) as shown to fix the problem:"
108 echo "indent ${VPP_DIR}${i}"
109 echo "indent ${VPP_DIR}${i}"
110 fi
Ed Warnickebe053b82016-08-05 11:43:58 -0700111 fi
112 if [ -f ${i}.out1 ]; then
113 rm ${i}.out1
114 fi
115 if [ -f ${i}.out2 ]; then
116 rm ${i}.out2
117 fi
118 else
119 UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
120 fi
121 else
122 UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
123 fi
124done
125
126if [ ${EXIT_CODE} == 0 ]; then
127 echo "*******************************************************************"
128 echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
129 echo "*******************************************************************"
130else
131 echo "*******************************************************************"
132 echo "* VPP CHECKSTYLE FAILED"
133 echo "* CONSULT FAILURE LOG ABOVE"
134 echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
135 echo "*******************************************************************"
136fi
137exit ${EXIT_CODE}