Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 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 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 18 | CLANG_FORMAT_VER_REGEX='([0-9]+)\.[0-9]+\.[0-9]+' |
| 19 | CLANG_FORMAT_DIFF="/usr/share/clang/clang-format-diff.py" |
| 20 | |
Dave Wallace | fb1135d | 2021-11-09 13:58:12 -0500 | [diff] [blame] | 21 | # TODO: Remove clang-format-${CLANG_FORMAT_VER} from 'make install-deps' when |
| 22 | # CLANG_FORMAT_VER default value is upgraded |
Dave Wallace | 4a33231 | 2022-03-17 17:44:35 -0400 | [diff] [blame^] | 23 | CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-11} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 24 | GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1" |
| 25 | CLANG_FORMAT_DIFF_ARGS="-style file -p1" |
| 26 | SUFFIX="-${CLANG_FORMAT_VER}" |
| 27 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 28 | # Attempt to find clang-format to confirm Clang version. |
| 29 | if command -v clang-format${SUFFIX} &> /dev/null; |
| 30 | then |
| 31 | CLANG_FORMAT=clang-format${SUFFIX} |
| 32 | elif command -v clang-format &> /dev/null; |
| 33 | then |
| 34 | CLANG_FORMAT=clang-format |
| 35 | fi |
| 36 | |
| 37 | CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version) |
| 38 | echo $CLANG_FORMAT_VERSION |
| 39 | |
| 40 | # Confirm that Clang is the expected version. |
| 41 | if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]]; |
| 42 | then |
| 43 | echo "*******************************************************************" |
| 44 | echo "* CHECKSTYLE VERSION REGEX CHECK FAILED" |
| 45 | echo "* $CLANG_FORMAT_VERSION" |
| 46 | echo "*******************************************************************" |
| 47 | exit 1 |
| 48 | fi |
| 49 | |
| 50 | if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]]; |
| 51 | then |
| 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 |
| 57 | fi |
| 58 | |
| 59 | # Attempt to find clang-format-diff. |
| 60 | if command -v clang-format-diff${SUFFIX} &> /dev/null; |
| 61 | then |
| 62 | CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX} |
Klement Sekera | b9ff03c | 2022-02-18 16:23:33 +0000 | [diff] [blame] | 63 | elif command -v clang-format-diff.py &> /dev/null; |
| 64 | then |
| 65 | CLANG_FORMAT_DIFF=clang-format-diff.py |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 66 | elif command -v clang-format-diff &> /dev/null; |
| 67 | then |
Klement Sekera | b9ff03c | 2022-02-18 16:23:33 +0000 | [diff] [blame] | 68 | CLANG_FORMAT_DIFF=clang-format-diff |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 69 | elif [ ! -f $CLANG_FORMAT_DIFF ] ; |
| 70 | then |
| 71 | echo "*******************************************************************" |
| 72 | echo "* CHECKSTYLE FAILED" |
| 73 | echo "* Could not locate the clang-format-diff script" |
| 74 | echo "*******************************************************************" |
| 75 | exit 1 |
| 76 | fi |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 77 | |
| 78 | in=$(mktemp) |
Neale Ranns | 8c0474a | 2021-01-04 08:58:12 +0000 | [diff] [blame] | 79 | git diff ${GIT_DIFF_ARGS} ':!*.patch' > ${in} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 80 | |
| 81 | line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l) |
| 82 | if [ ${line_count} -gt 0 ] ; then |
| 83 | echo |
| 84 | sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in} |
| 85 | echo |
| 86 | echo "*******************************************************************" |
| 87 | echo "* CHECKSTYLE FAILED" |
| 88 | echo "* Please remove INDENT-ON and INDENT-OFF from modified lines." |
| 89 | echo "*******************************************************************" |
| 90 | rm ${in} |
| 91 | exit 1 |
| 92 | fi |
| 93 | |
| 94 | if [ "${1}" == "--fix" ]; then |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 95 | cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 96 | filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in}) |
| 97 | git status ${filelist} |
| 98 | rm ${in} |
| 99 | exit 0 |
| 100 | fi |
| 101 | |
| 102 | line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l) |
| 103 | if [ ${line_count} -gt 0 ] ; then |
| 104 | echo |
| 105 | sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in} |
| 106 | echo |
| 107 | echo "*******************************************************************" |
| 108 | echo "* CHECKSTYLE FAILED" |
| 109 | echo "* Trailing whitespace detected" |
| 110 | echo "*******************************************************************" |
| 111 | rm ${in} |
| 112 | exit 1 |
| 113 | fi |
| 114 | |
| 115 | out=$(mktemp) |
| 116 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 117 | cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 118 | rm ${in} |
| 119 | |
| 120 | line_count=$(cat ${out} | wc -l) |
| 121 | |
| 122 | if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \ |
| 123 | command -v highlight &> /dev/null ; then |
| 124 | highlight --syntax diff -O ansi ${out} |
| 125 | else |
| 126 | cat ${out} |
| 127 | fi |
| 128 | |
| 129 | rm ${out} |
| 130 | |
| 131 | if [ ${line_count} -gt 0 ] ; then |
| 132 | echo "*******************************************************************" |
| 133 | echo "* CHECKSTYLE FAILED" |
| 134 | echo "* CONSULT DIFF ABOVE" |
| 135 | echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue" |
| 136 | echo "*******************************************************************" |
| 137 | exit 1 |
| 138 | else |
| 139 | echo "*******************************************************************" |
| 140 | echo "* CHECKSTYLE SUCCESSFULLY COMPLETED" |
| 141 | echo "*******************************************************************" |
| 142 | exit 0 |
| 143 | fi |