Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Dave Barach | 8d0f2f0 | 2018-03-12 09:31:36 -0400 | [diff] [blame] | 3 | # Copyright (c) 2015 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 | |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 16 | VPP_DIR=`dirname $0`/../../ |
| 17 | EXIT_CODE=0 |
| 18 | FIX="0" |
Damjan Marion | 2470485 | 2016-09-07 13:10:50 +0200 | [diff] [blame] | 19 | FULL="0" |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 20 | CHECKSTYLED_FILES="" |
| 21 | UNCHECKSTYLED_FILES="" |
| 22 | |
| 23 | # If the user provides --fix, then actually fix things |
| 24 | # Note: this is meant for use outside of the CI Jobs, by users cleaning things up |
| 25 | |
Damjan Marion | 2470485 | 2016-09-07 13:10:50 +0200 | [diff] [blame] | 26 | while true; do |
| 27 | case ${1} in |
| 28 | --fix) |
| 29 | FIX="1" |
| 30 | ;; |
| 31 | --full) |
| 32 | FULL="1" |
| 33 | ;; |
| 34 | esac |
| 35 | shift || break |
| 36 | done |
| 37 | |
| 38 | if [ "${FULL}" == "1" ]; then |
| 39 | FILELIST=$(git ls-tree -r HEAD --name-only) |
| 40 | else |
| 41 | FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u) |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 42 | fi |
| 43 | |
| 44 | # Check to make sure we have indent. Exit if we don't with an error message, but |
| 45 | # don't *fail*. |
| 46 | command -v indent > /dev/null |
| 47 | if [ $? != 0 ]; then |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 48 | echo "Cound not find required command \"indent\". Checkstyle aborted" |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 49 | exit ${EXIT_CODE} |
| 50 | fi |
| 51 | indent --version |
| 52 | |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 53 | # Check to make sure we have clang-format. Exit if we don't with an error message, but |
| 54 | # don't *fail*. |
Klement Sekera | 8a398bb | 2017-09-26 02:39:40 +0200 | [diff] [blame] | 55 | HAVE_CLANG_FORMAT=0 |
Klement Sekera | 6b6bd9d | 2017-10-04 06:26:36 +0200 | [diff] [blame] | 56 | command -v clang-format > /dev/null |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 57 | if [ $? != 0 ]; then |
| 58 | echo "Could not find command \"clang-format\". Checking C++ files will cause abort" |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 59 | else |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 60 | clang-format --version |
Klement Sekera | 8a398bb | 2017-09-26 02:39:40 +0200 | [diff] [blame] | 61 | x=$(echo "" | clang-format 2>&1) |
| 62 | if [[ "$x" == "" ]]; then |
| 63 | HAVE_CLANG_FORMAT=1 |
| 64 | else |
| 65 | echo "Output produced while formatting empty file (expected empty string):" |
| 66 | echo "$x" |
| 67 | echo "Could not find working \"clang-format\". Checking C++ files will cause abort" |
| 68 | fi |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 69 | fi |
| 70 | |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 71 | cd ${VPP_DIR} |
| 72 | git status |
Damjan Marion | 2470485 | 2016-09-07 13:10:50 +0200 | [diff] [blame] | 73 | for i in ${FILELIST}; do |
Damjan Marion | 757585d | 2017-04-20 11:42:28 +0200 | [diff] [blame] | 74 | if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 75 | grep -q "fd.io coding-style-patch-verification: ON" ${i} |
| 76 | if [ $? == 0 ]; then |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 77 | EXTENSION=`basename ${i} | sed 's/^\w\+.//'` |
| 78 | case ${EXTENSION} in |
| 79 | hpp|cpp|cc|hh) |
| 80 | CMD="clang-format" |
| 81 | if [ ${HAVE_CLANG_FORMAT} == 0 ]; then |
| 82 | echo "C++ file detected. Abort. (missing clang-format)" |
| 83 | exit ${EXIT_CODE} |
| 84 | fi |
| 85 | ;; |
| 86 | *) |
| 87 | CMD="indent" |
| 88 | ;; |
| 89 | esac |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 90 | CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}" |
| 91 | if [ ${FIX} == 0 ]; then |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 92 | if [ "${CMD}" == "clang-format" ] |
| 93 | then |
| 94 | clang-format ${i} > ${i}.out2 |
| 95 | else |
| 96 | indent ${i} -o ${i}.out1 > /dev/null 2>&1 |
| 97 | indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1 |
| 98 | fi |
| 99 | # Remove trailing whitespace |
| 100 | sed -i -e 's/[[:space:]]*$//' ${i}.out2 |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 101 | diff -q ${i} ${i}.out2 |
| 102 | else |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 103 | if [ "${CMD}" == "clang-format" ]; then |
| 104 | clang-format -i ${i} > /dev/null 2>&1 |
| 105 | else |
| 106 | indent ${i} |
| 107 | indent ${i} |
| 108 | fi |
| 109 | # Remove trailing whitespace |
| 110 | sed -i -e 's/[[:space:]]*$//' ${i} |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 111 | fi |
| 112 | if [ $? != 0 ]; then |
| 113 | EXIT_CODE=1 |
| 114 | echo |
| 115 | echo "Checkstyle failed for ${i}." |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 116 | if [ "${CMD}" == "clang-format" ]; then |
| 117 | echo "Run clang-format as shown to fix the problem:" |
| 118 | echo "clang-format -i ${VPP_DIR}${i}" |
| 119 | else |
| 120 | echo "Run indent (twice!) as shown to fix the problem:" |
| 121 | echo "indent ${VPP_DIR}${i}" |
| 122 | echo "indent ${VPP_DIR}${i}" |
| 123 | fi |
Ed Warnicke | be053b8 | 2016-08-05 11:43:58 -0700 | [diff] [blame] | 124 | fi |
| 125 | if [ -f ${i}.out1 ]; then |
| 126 | rm ${i}.out1 |
| 127 | fi |
| 128 | if [ -f ${i}.out2 ]; then |
| 129 | rm ${i}.out2 |
| 130 | fi |
| 131 | else |
| 132 | UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}" |
| 133 | fi |
| 134 | else |
| 135 | UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}" |
| 136 | fi |
| 137 | done |
| 138 | |
| 139 | if [ ${EXIT_CODE} == 0 ]; then |
| 140 | echo "*******************************************************************" |
| 141 | echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED" |
| 142 | echo "*******************************************************************" |
| 143 | else |
| 144 | echo "*******************************************************************" |
| 145 | echo "* VPP CHECKSTYLE FAILED" |
| 146 | echo "* CONSULT FAILURE LOG ABOVE" |
| 147 | echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue" |
| 148 | echo "*******************************************************************" |
| 149 | fi |
| 150 | exit ${EXIT_CODE} |