misc: migrate from GNU indent to clang-format
[vpp.git] / extras / scripts / checkstyle.sh
1 #!/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
16 set -eEo pipefail
17
18 CLANG_FORMAT_VER=10
19 GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
20 CLANG_FORMAT_DIFF_ARGS="-style file -p1"
21 SUFFIX="-${CLANG_FORMAT_VER}"
22
23 clang-format${SUFFIX} --version
24
25 in=$(mktemp)
26 git diff ${GIT_DIFF_ARGS} > ${in}
27
28 line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
29 if [ ${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
39 fi
40
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}
45   rm ${in}
46   exit 0
47 fi
48
49 line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
50 if [ ${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
60 fi
61
62 out=$(mktemp)
63
64 cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
65 rm ${in}
66
67 line_count=$(cat ${out} | wc -l)
68
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}
72 else
73   cat ${out}
74 fi
75
76 rm ${out}
77
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 "*******************************************************************"
84     exit 1
85 else
86     echo "*******************************************************************"
87     echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
88     echo "*******************************************************************"
89     exit 0
90 fi