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