blob: 58f6715411490f122f642941ce870daace4473da [file] [log] [blame]
Ed Warnickebe053b82016-08-05 11:43:58 -07001#!/bin/bash
2
Dave Barach8d0f2f02018-03-12 09:31:36 -04003# Copyright (c) 2015 Cisco and/or its affiliates.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at:
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Ed Warnickebe053b82016-08-05 11:43:58 -070016VPP_DIR=`dirname $0`/../../
17EXIT_CODE=0
18FIX="0"
Damjan Marion24704852016-09-07 13:10:50 +020019FULL="0"
Ed Warnickebe053b82016-08-05 11:43:58 -070020CHECKSTYLED_FILES=""
21UNCHECKSTYLED_FILES=""
22
23# If the user provides --fix, then actually fix things
24# Note: this is meant for use outside of the CI Jobs, by users cleaning things up
25
Damjan Marion24704852016-09-07 13:10:50 +020026while true; do
27 case ${1} in
28 --fix)
29 FIX="1"
30 ;;
31 --full)
32 FULL="1"
33 ;;
34 esac
35 shift || break
36done
37
38if [ "${FULL}" == "1" ]; then
39 FILELIST=$(git ls-tree -r HEAD --name-only)
40else
41 FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u)
Ed Warnickebe053b82016-08-05 11:43:58 -070042fi
43
44# Check to make sure we have indent. Exit if we don't with an error message, but
45# don't *fail*.
46command -v indent > /dev/null
47if [ $? != 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020048 echo "Cound not find required command \"indent\". Checkstyle aborted"
Ed Warnickebe053b82016-08-05 11:43:58 -070049 exit ${EXIT_CODE}
50fi
51indent --version
52
Klement Sekeradc15be22017-06-12 06:49:33 +020053# Check to make sure we have clang-format. Exit if we don't with an error message, but
54# don't *fail*.
Klement Sekera8a398bb2017-09-26 02:39:40 +020055HAVE_CLANG_FORMAT=0
Klement Sekera6b6bd9d2017-10-04 06:26:36 +020056command -v clang-format > /dev/null
Klement Sekeradc15be22017-06-12 06:49:33 +020057if [ $? != 0 ]; then
58 echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
Klement Sekeradc15be22017-06-12 06:49:33 +020059else
Klement Sekeradc15be22017-06-12 06:49:33 +020060 clang-format --version
Klement Sekera8a398bb2017-09-26 02:39:40 +020061 x=$(echo "" | clang-format 2>&1)
62 if [[ "$x" == "" ]]; then
63 HAVE_CLANG_FORMAT=1
64 else
65 echo "Output produced while formatting empty file (expected empty string):"
66 echo "$x"
67 echo "Could not find working \"clang-format\". Checking C++ files will cause abort"
68 fi
Klement Sekeradc15be22017-06-12 06:49:33 +020069fi
70
Ed Warnickebe053b82016-08-05 11:43:58 -070071cd ${VPP_DIR}
72git status
Damjan Marion24704852016-09-07 13:10:50 +020073for i in ${FILELIST}; do
Damjan Marion757585d2017-04-20 11:42:28 +020074 if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
Dave Barach119286e2020-05-08 09:46:17 -040075 grep -q '>>>>>>>' ${i}
76 if [ $? == 0 ]; then
77 echo "Unresolved merge conflict detected in" ${i} "... Abort."
78 exit 1
79 fi
Ed Warnickebe053b82016-08-05 11:43:58 -070080 grep -q "fd.io coding-style-patch-verification: ON" ${i}
81 if [ $? == 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020082 EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
83 case ${EXTENSION} in
84 hpp|cpp|cc|hh)
85 CMD="clang-format"
86 if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
87 echo "C++ file detected. Abort. (missing clang-format)"
88 exit ${EXIT_CODE}
89 fi
90 ;;
91 *)
92 CMD="indent"
93 ;;
94 esac
Ed Warnickebe053b82016-08-05 11:43:58 -070095 CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
96 if [ ${FIX} == 0 ]; then
Klement Sekeradc15be22017-06-12 06:49:33 +020097 if [ "${CMD}" == "clang-format" ]
98 then
99 clang-format ${i} > ${i}.out2
100 else
101 indent ${i} -o ${i}.out1 > /dev/null 2>&1
102 indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
103 fi
104 # Remove trailing whitespace
105 sed -i -e 's/[[:space:]]*$//' ${i}.out2
Ed Warnickebe053b82016-08-05 11:43:58 -0700106 diff -q ${i} ${i}.out2
107 else
Klement Sekeradc15be22017-06-12 06:49:33 +0200108 if [ "${CMD}" == "clang-format" ]; then
109 clang-format -i ${i} > /dev/null 2>&1
110 else
111 indent ${i}
112 indent ${i}
113 fi
114 # Remove trailing whitespace
115 sed -i -e 's/[[:space:]]*$//' ${i}
Ed Warnickebe053b82016-08-05 11:43:58 -0700116 fi
117 if [ $? != 0 ]; then
118 EXIT_CODE=1
119 echo
120 echo "Checkstyle failed for ${i}."
Klement Sekeradc15be22017-06-12 06:49:33 +0200121 if [ "${CMD}" == "clang-format" ]; then
122 echo "Run clang-format as shown to fix the problem:"
123 echo "clang-format -i ${VPP_DIR}${i}"
124 else
125 echo "Run indent (twice!) as shown to fix the problem:"
126 echo "indent ${VPP_DIR}${i}"
127 echo "indent ${VPP_DIR}${i}"
128 fi
Ed Warnickebe053b82016-08-05 11:43:58 -0700129 fi
130 if [ -f ${i}.out1 ]; then
131 rm ${i}.out1
132 fi
133 if [ -f ${i}.out2 ]; then
134 rm ${i}.out2
135 fi
136 else
137 UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
138 fi
139 else
140 UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
141 fi
142done
143
144if [ ${EXIT_CODE} == 0 ]; then
145 echo "*******************************************************************"
146 echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
147 echo "*******************************************************************"
148else
149 echo "*******************************************************************"
150 echo "* VPP CHECKSTYLE FAILED"
151 echo "* CONSULT FAILURE LOG ABOVE"
152 echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
153 echo "*******************************************************************"
154fi
155exit ${EXIT_CODE}