xref: /spdk/scripts/check_format.sh (revision 4d43844f4d50a7abd5b6029ff3f2baff329890a8)
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' | grep -v rte_vhost | grep -v cpp_headers | \
20		xargs astyle --options=.astylerc >> astyle.log
21	if grep -q "^Formatted" astyle.log; then
22		echo " errors detected"
23		git diff
24		sed -i -e 's/  / /g' astyle.log
25		grep --color=auto "^Formatted.*" astyle.log
26		echo "Incorrect code style detected in one or more files."
27		echo "The files have been automatically formatted."
28		echo "Remember to add the files to your commit."
29		rc=1
30	else
31		echo " OK"
32	fi
33	rm -f astyle.log
34else
35	echo "You do not have astyle installed so your code style is not being checked!"
36fi
37
38echo -n "Checking comment style..."
39
40git grep --line-number -e '/[*][^ *-]' -- '*.[ch]' > comment.log || true
41git grep --line-number -e '[^ ][*]/' -- '*.[ch]' ':!lib/vhost/rte_vhost*/*' >> comment.log || true
42git grep --line-number -e '^[*]' -- '*.[ch]' >> comment.log || true
43
44if [ -s comment.log ]; then
45	echo " Incorrect comment formatting detected"
46	cat comment.log
47	rc=1
48else
49	echo " OK"
50fi
51rm -f comment.log
52
53echo -n "Checking blank lines at end of file..."
54
55if ! git grep -I -l -e . -z | \
56	xargs -0 -P8 -n1 scripts/eofnl > eofnl.log; then
57	echo " Incorrect end-of-file formatting detected"
58	cat eofnl.log
59	rc=1
60else
61	echo " OK"
62fi
63rm -f eofnl.log
64
65echo -n "Checking for POSIX includes..."
66git grep -I -i -f scripts/posix.txt -- './*' ':!include/spdk/stdinc.h' ':!lib/vhost/rte_vhost*/**' ':!scripts/posix.txt' > scripts/posix.log || true
67if [ -s scripts/posix.log ]; then
68	echo "POSIX includes detected. Please include spdk/stdinc.h instead."
69	cat scripts/posix.log
70	rc=1
71else
72	echo " OK"
73fi
74rm -f scripts/posix.log
75
76if hash pep8; then
77	echo -n "Checking Python style..."
78
79	PEP8_ARGS+=" --ignore=E302" # ignore 'E302 expected 2 blank lines, found 1'
80	PEP8_ARGS+=" --max-line-length=140"
81
82	error=0
83	git ls-files '*.py' | xargs -n1 pep8 $PEP8_ARGS > pep8.log || error=1
84	if [ $error -ne 0 ]; then
85		echo " Python formatting errors detected"
86		cat pep8.log
87		rc=1
88	else
89		echo " OK"
90	fi
91	rm -f pep8.log
92fi
93
94# Check if any of the public interfaces were modified by this patch.
95# Warn the user to consider updating the changelog any changes
96# are detected.
97echo -n "Checking whether CHANGELOG.md should be updated..."
98staged=$(git diff --name-only --cached .)
99working=$(git status -s --porcelain | grep -iv "??" | awk '{print $2}')
100files="$staged $working"
101if [[ "$files" = " " ]]; then
102	files=$(git diff-tree --no-commit-id --name-only -r HEAD)
103fi
104
105has_changelog=0
106for f in $files; do
107	if [[ $f == CHANGELOG.md ]]; then
108		# The user has a changelog entry, so exit.
109		has_changelog=1
110		break
111	fi
112done
113
114needs_changelog=0
115if [ $has_changelog -eq 0 ]; then
116	for f in $files; do
117		if [[ $f == include/spdk/* ]] || [[ $f == scripts/rpc.py ]] || [[ $f == etc/* ]]; then
118			echo ""
119			echo -n "$f was modified. Consider updating CHANGELOG.md."
120			needs_changelog=1
121		fi
122	done
123fi
124
125if [ $needs_changelog -eq 0 ]; then
126	echo " OK"
127else
128	echo ""
129fi
130
131exit $rc
132