1#!/usr/bin/env bash 2 3readonly BASEDIR=$(readlink -f $(dirname $0))/.. 4cd $BASEDIR 5 6# exit on errors 7set -e 8 9rc=0 10 11if hash astyle; then 12 echo -n "Checking coding style..." 13 rm -f astyle.log 14 touch astyle.log 15 # Exclude rte_vhost code imported from DPDK - we want to keep the original code 16 # as-is to enable ongoing work to synch with a generic upstream DPDK vhost library, 17 # rather than making diffs more complicated by a lot of changes to follow SPDK 18 # coding standards. 19 git ls-files '*.[ch]' '*.cpp' '*.cc' '*.cxx' '*.hh' '*.hpp' | \ 20 grep -v rte_vhost | grep -v cpp_headers | \ 21 xargs astyle --options=.astylerc >> astyle.log 22 if grep -q "^Formatted" astyle.log; then 23 echo " errors detected" 24 git diff 25 sed -i -e 's/ / /g' astyle.log 26 grep --color=auto "^Formatted.*" astyle.log 27 echo "Incorrect code style detected in one or more files." 28 echo "The files have been automatically formatted." 29 echo "Remember to add the files to your commit." 30 rc=1 31 else 32 echo " OK" 33 fi 34 rm -f astyle.log 35else 36 echo "You do not have astyle installed so your code style is not being checked!" 37fi 38 39echo -n "Checking comment style..." 40 41git grep --line-number -e '/[*][^ *-]' -- '*.[ch]' > comment.log || true 42git grep --line-number -e '[^ ][*]/' -- '*.[ch]' ':!lib/vhost/rte_vhost*/*' >> comment.log || true 43git grep --line-number -e '^[*]' -- '*.[ch]' >> comment.log || true 44 45if [ -s comment.log ]; then 46 echo " Incorrect comment formatting detected" 47 cat comment.log 48 rc=1 49else 50 echo " OK" 51fi 52rm -f comment.log 53 54echo -n "Checking for spaces before tabs..." 55git grep --line-number $' \t' -- > whitespace.log || true 56if [ -s whitespace.log ]; then 57 echo " Spaces before tabs detected" 58 cat whitespace.log 59 rc=1 60else 61 echo " OK" 62fi 63rm -f whitespace.log 64 65echo -n "Checking trailing whitespace in output strings..." 66 67git grep --line-number -e ' \\n"' -- '*.[ch]' > whitespace.log || true 68 69if [ -s whitespace.log ]; then 70 echo " Incorrect trailing whitespace detected" 71 cat whitespace.log 72 rc=1 73else 74 echo " OK" 75fi 76rm -f whitespace.log 77 78echo -n "Checking for use of forbidden library functions..." 79 80git grep --line-number -w '\(strncpy\|strcpy\|strcat\|sprintf\|vsprintf\)' -- './*.c' ':!lib/vhost/rte_vhost*/**' > badfunc.log || true 81if [ -s badfunc.log ]; then 82 echo " Forbidden library functions detected" 83 cat badfunc.log 84 rc=1 85else 86 echo " OK" 87fi 88rm -f badfunc.log 89 90echo -n "Checking blank lines at end of file..." 91 92if ! git grep -I -l -e . -z | \ 93 xargs -0 -P8 -n1 scripts/eofnl > eofnl.log; then 94 echo " Incorrect end-of-file formatting detected" 95 cat eofnl.log 96 rc=1 97else 98 echo " OK" 99fi 100rm -f eofnl.log 101 102echo -n "Checking for POSIX includes..." 103git grep -I -i -f scripts/posix.txt -- './*' ':!include/spdk/stdinc.h' ':!include/linux/**' ':!lib/vhost/rte_vhost*/**' ':!scripts/posix.txt' > scripts/posix.log || true 104if [ -s scripts/posix.log ]; then 105 echo "POSIX includes detected. Please include spdk/stdinc.h instead." 106 cat scripts/posix.log 107 rc=1 108else 109 echo " OK" 110fi 111rm -f scripts/posix.log 112 113if hash pycodestyle 2>/dev/null; then 114 PEP8=pycodestyle 115elif hash pep8 2>/dev/null; then 116 PEP8=pep8 117fi 118 119if [ ! -z ${PEP8} ]; then 120 echo -n "Checking Python style..." 121 122 PEP8_ARGS+=" --max-line-length=140" 123 124 error=0 125 git ls-files '*.py' | xargs -n1 $PEP8 $PEP8_ARGS > pep8.log || error=1 126 if [ $error -ne 0 ]; then 127 echo " Python formatting errors detected" 128 cat pep8.log 129 rc=1 130 else 131 echo " OK" 132 fi 133 rm -f pep8.log 134else 135 echo "You do not have pycodestyle or pep8 installed so your Python style is not being checked!" 136fi 137 138# Check if any of the public interfaces were modified by this patch. 139# Warn the user to consider updating the changelog any changes 140# are detected. 141echo -n "Checking whether CHANGELOG.md should be updated..." 142staged=$(git diff --name-only --cached .) 143working=$(git status -s --porcelain | grep -iv "??" | awk '{print $2}') 144files="$staged $working" 145if [[ "$files" = " " ]]; then 146 files=$(git diff-tree --no-commit-id --name-only -r HEAD) 147fi 148 149has_changelog=0 150for f in $files; do 151 if [[ $f == CHANGELOG.md ]]; then 152 # The user has a changelog entry, so exit. 153 has_changelog=1 154 break 155 fi 156done 157 158needs_changelog=0 159if [ $has_changelog -eq 0 ]; then 160 for f in $files; do 161 if [[ $f == include/spdk/* ]] || [[ $f == scripts/rpc.py ]] || [[ $f == etc/* ]]; then 162 echo "" 163 echo -n "$f was modified. Consider updating CHANGELOG.md." 164 needs_changelog=1 165 fi 166 done 167fi 168 169if [ $needs_changelog -eq 0 ]; then 170 echo " OK" 171else 172 echo "" 173fi 174 175exit $rc 176