blob: b40f43fa1e67a892fca800765332f983884b3d46 [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
18CLANG_FORMAT_VER=10
19GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
20CLANG_FORMAT_DIFF_ARGS="-style file -p1"
21SUFFIX="-${CLANG_FORMAT_VER}"
22
23clang-format${SUFFIX} --version
24
25in=$(mktemp)
26git diff ${GIT_DIFF_ARGS} > ${in}
27
28line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
29if [ ${line_count} -gt 0 ] ; then
30 echo
31 sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
32 echo
33 echo "*******************************************************************"
34 echo "* CHECKSTYLE FAILED"
35 echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
36 echo "*******************************************************************"
37 rm ${in}
38 exit 1
39fi
40
41if [ "${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}
45 rm ${in}
46 exit 0
47fi
48
49line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
50if [ ${line_count} -gt 0 ] ; then
51 echo
52 sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
53 echo
54 echo "*******************************************************************"
55 echo "* CHECKSTYLE FAILED"
56 echo "* Trailing whitespace detected"
57 echo "*******************************************************************"
58 rm ${in}
59 exit 1
60fi
61
62out=$(mktemp)
63
64cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
65rm ${in}
66
67line_count=$(cat ${out} | wc -l)
68
69if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
70 command -v highlight &> /dev/null ; then
71 highlight --syntax diff -O ansi ${out}
72else
73 cat ${out}
74fi
75
76rm ${out}
77
78if [ ${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 "*******************************************************************"
84 exit 1
85else
86 echo "*******************************************************************"
87 echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
88 echo "*******************************************************************"
89 exit 0
90fi