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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
20 CLANG_FORMAT_DIFF_ARGS="-style file -p1"
21 SUFFIX="-${CLANG_FORMAT_VER}"
23 clang-format${SUFFIX} --version
26 git diff ${GIT_DIFF_ARGS} > ${in}
28 line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
29 if [ ${line_count} -gt 0 ] ; then
31 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
33 echo "*******************************************************************"
34 echo "* CHECKSTYLE FAILED"
35 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
36 echo "*******************************************************************"
41 if [ "${1}" == "--fix" ]; then
42 cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} -i
43 filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
44 git status ${filelist}
49 line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
50 if [ ${line_count} -gt 0 ] ; then
52 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
54 echo "*******************************************************************"
55 echo "* CHECKSTYLE FAILED"
56 echo "* Trailing whitespace detected"
57 echo "*******************************************************************"
64 cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
67 line_count=$(cat ${out} | wc -l)
69 if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
70 command -v highlight &> /dev/null ; then
71 highlight --syntax diff -O ansi ${out}
78 if [ ${line_count} -gt 0 ] ; then
79 echo "*******************************************************************"
80 echo "* CHECKSTYLE FAILED"
81 echo "* CONSULT DIFF ABOVE"
82 echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
83 echo "*******************************************************************"
86 echo "*******************************************************************"
87 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
88 echo "*******************************************************************"