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 | |
Damjan Marion | 04e7c0f | 2021-03-23 21:34:08 +0100 | [diff] [blame] | 21 | CLANG_FORMAT_VER=${CLANG_FORMAT_VER:-10} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 22 | GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1" |
| 23 | CLANG_FORMAT_DIFF_ARGS="-style file -p1" |
| 24 | SUFFIX="-${CLANG_FORMAT_VER}" |
| 25 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 26 | # Attempt to find clang-format to confirm Clang version. |
| 27 | if command -v clang-format${SUFFIX} &> /dev/null; |
| 28 | then |
| 29 | CLANG_FORMAT=clang-format${SUFFIX} |
| 30 | elif command -v clang-format &> /dev/null; |
| 31 | then |
| 32 | CLANG_FORMAT=clang-format |
| 33 | fi |
| 34 | |
| 35 | CLANG_FORMAT_VERSION=$(${CLANG_FORMAT} --version) |
| 36 | echo $CLANG_FORMAT_VERSION |
| 37 | |
| 38 | # Confirm that Clang is the expected version. |
| 39 | if [[ ! $CLANG_FORMAT_VERSION =~ $CLANG_FORMAT_VER_REGEX ]]; |
| 40 | then |
| 41 | echo "*******************************************************************" |
| 42 | echo "* CHECKSTYLE VERSION REGEX CHECK FAILED" |
| 43 | echo "* $CLANG_FORMAT_VERSION" |
| 44 | echo "*******************************************************************" |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | if [[ ! $CLANG_FORMAT_VER == "${BASH_REMATCH[1]}" ]]; |
| 49 | then |
| 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 |
| 55 | fi |
| 56 | |
| 57 | # Attempt to find clang-format-diff. |
| 58 | if command -v clang-format-diff${SUFFIX} &> /dev/null; |
| 59 | then |
| 60 | CLANG_FORMAT_DIFF=clang-format-diff${SUFFIX} |
| 61 | elif command -v clang-format-diff &> /dev/null; |
| 62 | then |
| 63 | CLANG_FORMAT=clang-format-diff |
| 64 | elif [ ! -f $CLANG_FORMAT_DIFF ] ; |
| 65 | then |
| 66 | echo "*******************************************************************" |
| 67 | echo "* CHECKSTYLE FAILED" |
| 68 | echo "* Could not locate the clang-format-diff script" |
| 69 | echo "*******************************************************************" |
| 70 | exit 1 |
| 71 | fi |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 72 | |
| 73 | in=$(mktemp) |
Neale Ranns | 8c0474a | 2021-01-04 08:58:12 +0000 | [diff] [blame] | 74 | git diff ${GIT_DIFF_ARGS} ':!*.patch' > ${in} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 75 | |
| 76 | line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l) |
| 77 | if [ ${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 |
| 87 | fi |
| 88 | |
| 89 | if [ "${1}" == "--fix" ]; then |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 90 | cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} -i |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 91 | filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in}) |
| 92 | git status ${filelist} |
| 93 | rm ${in} |
| 94 | exit 0 |
| 95 | fi |
| 96 | |
| 97 | line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l) |
| 98 | if [ ${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 |
| 108 | fi |
| 109 | |
| 110 | out=$(mktemp) |
| 111 | |
Ray Kinsella | 7183598 | 2021-03-12 15:57:29 +0000 | [diff] [blame] | 112 | cat ${in} | ${CLANG_FORMAT_DIFF} ${CLANG_FORMAT_DIFF_ARGS} > ${out} |
Damjan Marion | 942542f | 2020-12-12 19:09:31 +0100 | [diff] [blame] | 113 | rm ${in} |
| 114 | |
| 115 | line_count=$(cat ${out} | wc -l) |
| 116 | |
| 117 | if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \ |
| 118 | command -v highlight &> /dev/null ; then |
| 119 | highlight --syntax diff -O ansi ${out} |
| 120 | else |
| 121 | cat ${out} |
| 122 | fi |
| 123 | |
| 124 | rm ${out} |
| 125 | |
| 126 | if [ ${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 |
| 133 | else |
| 134 | echo "*******************************************************************" |
| 135 | echo "* CHECKSTYLE SUCCESSFULLY COMPLETED" |
| 136 | echo "*******************************************************************" |
| 137 | exit 0 |
| 138 | fi |