blob: 4dcf77874d3ede70c996786d603c0d0c35dfb08b [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
Damjan Marion04e7c0f2021-03-23 21:34:08 +010023CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-10}
Damjan Marion942542f2020-12-12 19:09:31 +010024GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
25CLANG_FORMAT_DIFF_ARGS="-style file -p1"
26SUFFIX="-${CLANG_FORMAT_VER}"
27
Ray Kinsella71835982021-03-12 15:57:29 +000028# Attempt to find clang-format to confirm Clang version.
29if command -v clang-format${SUFFIX} &> /dev/null;
30then
31 CLANG_FORMAT=clang-format${SUFFIX}
32elif command -v clang-format &> /dev/null;
33then
34 CLANG_FORMAT=clang-format
35fi
36
37CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version)
38echo $CLANG_FORMAT_VERSION
39
40# Confirm that Clang is the expected version.
41if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]];
42then
43 echo "*******************************************************************"
44 echo "* CHECKSTYLE VERSION REGEX CHECK FAILED"
45 echo "* $CLANG_FORMAT_VERSION"
46 echo "*******************************************************************"
47 exit 1
48fi
49
50if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]];
51then
52 echo "*******************************************************************"
53 echo "* CHECKSTYLE VERSION CHECK FAILED"
54 echo "* Expected major version $CLANG_FORMAT_VER, found ${BASH_REMATCH[1]}"
55 echo "*******************************************************************"
56 exit 1
57fi
58
59# Attempt to find clang-format-diff.
60if command -v clang-format-diff${SUFFIX} &> /dev/null;
61then
62 CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX}
63elif command -v clang-format-diff &> /dev/null;
64then
65 CLANG_FORMAT=clang-format-diff
66elif [ ! -f $CLANG_FORMAT_DIFF ] ;
67then
68 echo "*******************************************************************"
69 echo "* CHECKSTYLE FAILED"
70 echo "* Could not locate the clang-format-diff script"
71 echo "*******************************************************************"
72 exit 1
73fi
Damjan Marion942542f2020-12-12 19:09:31 +010074
75in=$(mktemp)
Neale Ranns8c0474a2021-01-04 08:58:12 +000076git diff ${GIT_DIFF_ARGS} ':!*.patch' > ${in}
Damjan Marion942542f2020-12-12 19:09:31 +010077
78line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
79if [ ${line_count} -gt 0 ] ; then
80 echo
81 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
82 echo
83 echo "*******************************************************************"
84 echo "* CHECKSTYLE FAILED"
85 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
86 echo "*******************************************************************"
87 rm ${in}
88 exit 1
89fi
90
91if [ "${1}" == "--fix" ]; then
Ray Kinsella71835982021-03-12 15:57:29 +000092 cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i
Damjan Marion942542f2020-12-12 19:09:31 +010093 filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
94 git status ${filelist}
95 rm ${in}
96 exit 0
97fi
98
99line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
100if [ ${line_count} -gt 0 ] ; then
101 echo
102 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
103 echo
104 echo "*******************************************************************"
105 echo "* CHECKSTYLE FAILED"
106 echo "* Trailing whitespace detected"
107 echo "*******************************************************************"
108 rm ${in}
109 exit 1
110fi
111
112out=$(mktemp)
113
Ray Kinsella71835982021-03-12 15:57:29 +0000114cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
Damjan Marion942542f2020-12-12 19:09:31 +0100115rm ${in}
116
117line_count=$(cat ${out} | wc -l)
118
119if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
120 command -v highlight &> /dev/null ; then
121 highlight --syntax diff -O ansi ${out}
122else
123 cat ${out}
124fi
125
126rm ${out}
127
128if [ ${line_count} -gt 0 ] ; then
129 echo "*******************************************************************"
130 echo "* CHECKSTYLE FAILED"
131 echo "* CONSULT DIFF ABOVE"
132 echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
133 echo "*******************************************************************"
134 exit 1
135else
136 echo "*******************************************************************"
137 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
138 echo "*******************************************************************"
139 exit 0
140fi