xref: /spdk/scripts/check_format.sh (revision 179ed697b3c461d100e675915d074be717b7b9cc)
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 rte_virtio | 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*/*' ':!lib/bdev/virtio/rte_virtio*/*' >> 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 blank lines at end of file..."
55
56if ! git grep -I -l -e . -z | \
57	xargs -0 -P8 -n1 scripts/eofnl > eofnl.log; then
58	echo " Incorrect end-of-file formatting detected"
59	cat eofnl.log
60	rc=1
61else
62	echo " OK"
63fi
64rm -f eofnl.log
65
66echo -n "Checking for POSIX includes..."
67git grep -I -i -f scripts/posix.txt -- './*' ':!include/spdk/stdinc.h' ':!lib/vhost/rte_vhost*/**' ':!lib/bdev/virtio/rte_virtio*/**' ':!scripts/posix.txt' > scripts/posix.log || true
68if [ -s scripts/posix.log ]; then
69	echo "POSIX includes detected. Please include spdk/stdinc.h instead."
70	cat scripts/posix.log
71	rc=1
72else
73	echo " OK"
74fi
75rm -f scripts/posix.log
76
77if hash pep8; then
78	echo -n "Checking Python style..."
79
80	PEP8_ARGS+=" --ignore=E302" # ignore 'E302 expected 2 blank lines, found 1'
81	PEP8_ARGS+=" --max-line-length=140"
82
83	error=0
84	git ls-files '*.py' | xargs -n1 pep8 $PEP8_ARGS > pep8.log || error=1
85	if [ $error -ne 0 ]; then
86		echo " Python formatting errors detected"
87		cat pep8.log
88		rc=1
89	else
90		echo " OK"
91	fi
92	rm -f pep8.log
93fi
94
95# Check if any of the public interfaces were modified by this patch.
96# Warn the user to consider updating the changelog any changes
97# are detected.
98echo -n "Checking whether CHANGELOG.md should be updated..."
99staged=$(git diff --name-only --cached .)
100working=$(git status -s --porcelain | grep -iv "??" | awk '{print $2}')
101files="$staged $working"
102if [[ "$files" = " " ]]; then
103	files=$(git diff-tree --no-commit-id --name-only -r HEAD)
104fi
105
106has_changelog=0
107for f in $files; do
108	if [[ $f == CHANGELOG.md ]]; then
109		# The user has a changelog entry, so exit.
110		has_changelog=1
111		break
112	fi
113done
114
115needs_changelog=0
116if [ $has_changelog -eq 0 ]; then
117	for f in $files; do
118		if [[ $f == include/spdk/* ]] || [[ $f == scripts/rpc.py ]] || [[ $f == etc/* ]]; then
119			echo ""
120			echo -n "$f was modified. Consider updating CHANGELOG.md."
121			needs_changelog=1
122		fi
123	done
124fi
125
126if [ $needs_changelog -eq 0 ]; then
127	echo " OK"
128else
129	echo ""
130fi
131
132exit $rc
133