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