blob: 2b884f5f08b2f9699d92f2c529de9f7ced0d3a6e [file] [log] [blame]
Damjan Marion942542f2020-12-12 19:09:31 +01001#!/bin/bash
2
3# Copyright (c) 2020 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
16set -eEo pipefail
17
Ray Kinsella71835982021-03-12 15:57:29 +000018CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+'
19CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py"
20
Dave Wallacefb1135d2021-11-09 13:58:12 -050021# TODO: Remove clang-format-${CLANG_FORMAT_VER} from 'make install-deps' when
22# CLANG_FORMAT_VER default value is upgraded
Dave Wallace4a332312022-03-17 17:44:35 -040023CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-11}
Damjan Marion942542f2020-12-12 19:09:31 +010024GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
Dave Wallace5d0fa2f2022-03-23 21:29:54 -040025GIT_DIFF_EXCLUDE_LIST=(
26 ':!*.patch'
27 ':(exclude)*src/vppinfra/dlmalloc.*'
28)
Damjan Marion942542f2020-12-12 19:09:31 +010029CLANG_FORMAT_DIFF_ARGS="-style file -p1"
30SUFFIX="-${CLANG_FORMAT_VER}"
31
Ray Kinsella71835982021-03-12 15:57:29 +000032# Attempt to find clang-format to confirm Clang version.
33if command -v clang-format${SUFFIX} &> /dev/null;
34then
35 CLANG_FORMAT=clang-format${SUFFIX}
36elif command -v clang-format &> /dev/null;
37then
38 CLANG_FORMAT=clang-format
39fi
40
41CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
42echo $CLANG_FORMAT_VERSION
43
44# Confirm that Clang is the expected version.
45if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
46then
47 echo "*******************************************************************"
48 echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
49 echo "* $CLANG_FORMAT_VERSION"
50 echo "*******************************************************************"
51 exit 1
52fi
53
54if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
55then
56 echo "*******************************************************************"
57 echo "* CHECKSTYLE VERSION CHECK FAILED"
58 echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
59 echo "*******************************************************************"
60 exit 1
61fi
62
63# Attempt to find clang-format-diff.
64if command -v clang-format-diff${SUFFIX} &> /dev/null;
65then
66 CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
Klement Sekerab9ff03c2022-02-18 16:23:33 +000067elif command -v clang-format-diff.py &> /dev/null;
68then
69 CLANG_FORMAT_DIFF=clang-format-diff.py
Ray Kinsella71835982021-03-12 15:57:29 +000070elif command -v clang-format-diff &> /dev/null;
71then
Klement Sekerab9ff03c2022-02-18 16:23:33 +000072 CLANG_FORMAT_DIFF=clang-format-diff
Ray Kinsella71835982021-03-12 15:57:29 +000073elif [ ! -f $CLANG_FORMAT_DIFF ] ;
74then
75 echo "*******************************************************************"
76 echo "* CHECKSTYLE FAILED"
77 echo "* Could not locate the clang-format-diff script"
78 echo "*******************************************************************"
79 exit 1
80fi
Damjan Marion942542f2020-12-12 19:09:31 +010081
82in=$(mktemp)
Dave Wallace5d0fa2f2022-03-23 21:29:54 -040083git diff ${GIT_DIFF_ARGS} ${GIT_DIFF_EXCLUDE_LIST[@]} > ${in}
Damjan Marion942542f2020-12-12 19:09:31 +010084
85line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
86if [ ${line_count} -gt 0 ] ; then
87 echo
88 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
89 echo
90 echo "*******************************************************************"
91 echo "* CHECKSTYLE FAILED"
92 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
93 echo "*******************************************************************"
94 rm ${in}
95 exit 1
96fi
97
98if [ "${1}" == "--fix" ]; then
Ray Kinsella71835982021-03-12 15:57:29 +000099 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
Damjan Marion942542f2020-12-12 19:09:31 +0100100 filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
101 git status ${filelist}
102 rm ${in}
103 exit 0
104fi
105
106line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
107if [ ${line_count} -gt 0 ] ; then
108 echo
109 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
110 echo
111 echo "*******************************************************************"
112 echo "* CHECKSTYLE FAILED"
113 echo "* Trailing whitespace detected"
114 echo "*******************************************************************"
115 rm ${in}
116 exit 1
117fi
118
119out=$(mktemp)
120
Ray Kinsella71835982021-03-12 15:57:29 +0000121cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
Damjan Marion942542f2020-12-12 19:09:31 +0100122rm ${in}
123
124line_count=$(cat ${out} | wc -l)
125
126if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
127 command -v highlight &> /dev/null ; then
128 highlight --syntax diff -O ansi ${out}
129else
130 cat ${out}
131fi
132
133rm ${out}
134
135if [ ${line_count} -gt 0 ] ; then
136 echo "*******************************************************************"
137 echo "* CHECKSTYLE FAILED"
138 echo "* CONSULT DIFF ABOVE"
139 echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
140 echo "*******************************************************************"
141 exit 1
142else
143 echo "*******************************************************************"
144 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
145 echo "*******************************************************************"
146 exit 0
147fi