xref: /dpdk/devtools/checkpatches.sh (revision 06c761d6fb50c8ba5990fa48838c478b5dbd89c0)
1#! /bin/sh
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright 2015 6WIND S.A.
4
5# Load config options:
6# - DPDK_CHECKPATCH_PATH
7# - DPDK_CHECKPATCH_LINE_LENGTH
8. $(dirname $(readlink -e $0))/load-devel-config
9
10VALIDATE_NEW_API=$(dirname $(readlink -e $0))/check-symbol-change.sh
11
12length=${DPDK_CHECKPATCH_LINE_LENGTH:-80}
13
14# override default Linux options
15options="--no-tree"
16options="$options --max-line-length=$length"
17options="$options --show-types"
18options="$options --ignore=LINUX_VERSION_CODE,\
19FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
20VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
21PREFER_KERNEL_TYPES,BIT_MACRO,CONST_STRUCT,\
22SPLIT_STRING,LONG_LINE_STRING,\
23LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
24NEW_TYPEDEFS,COMPARISON_TO_NULL"
25
26clean_tmp_files() {
27	if echo $tmpinput | grep -q '^checkpatches\.' ; then
28		rm -f "$tmpinput"
29	fi
30}
31
32trap "clean_tmp_files" SIGINT
33
34print_usage () {
35	cat <<- END_OF_HELP
36	usage: $(basename $0) [-q] [-v] [-nX|patch1 [patch2] ...]]
37
38	Run Linux kernel checkpatch.pl with DPDK options.
39	The environment variable DPDK_CHECKPATCH_PATH must be set.
40
41	The patches to check can be from stdin, files specified on the command line,
42	or latest git commits limited with -n option (default limit: origin/master).
43	END_OF_HELP
44}
45
46number=0
47quiet=false
48verbose=false
49while getopts hn:qv ARG ; do
50	case $ARG in
51		n ) number=$OPTARG ;;
52		q ) quiet=true ;;
53		v ) verbose=true ;;
54		h ) print_usage ; exit 0 ;;
55		? ) print_usage ; exit 1 ;;
56	esac
57done
58shift $(($OPTIND - 1))
59
60if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
61	print_usage >&2
62	echo
63	echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
64	exit 1
65fi
66
67total=0
68status=0
69
70check () { # <patch> <commit> <title>
71	local ret=0
72
73	total=$(($total + 1))
74	! $verbose || printf '\n### %s\n\n' "$3"
75	if [ -n "$1" ] ; then
76		tmpinput=$1
77	elif [ -n "$2" ] ; then
78		tmpinput=$(mktemp checkpatches.XXXXXX)
79		git format-patch --find-renames \
80		--no-stat --stdout -1 $commit > "$tmpinput"
81	else
82		tmpinput=$(mktemp checkpatches.XXXXXX)
83		cat > "$tmpinput"
84	fi
85
86	report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
87	if [ $? -ne 0 ] ; then
88		$verbose || printf '\n### %s\n\n' "$3"
89		printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
90		ret=1
91	fi
92
93	! $verbose || printf '\nChecking API additions/removals:\n'
94	report=$($VALIDATE_NEW_API "$tmpinput")
95	if [ $? -ne 0 ] ; then
96		printf '%s\n' "$report"
97		ret=1
98	fi
99
100	clean_tmp_files
101	[ $ret -eq 0 ] && return 0
102
103	status=$(($status + 1))
104}
105
106if [ -n "$1" ] ; then
107	for patch in "$@" ; do
108		# Subject can be on 2 lines
109		subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
110		check "$patch" '' "$subject"
111	done
112elif [ ! -t 0 ] ; then # stdin
113	subject=$(while read header value ; do
114		if [ "$header" = 'Subject:' ] ; then
115			IFS= read next
116			continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
117			echo $value$continuation
118			break
119		fi
120	done)
121	check '' '' "$subject"
122else
123	if [ $number -eq 0 ] ; then
124		commits=$(git rev-list --reverse origin/master..)
125	else
126		commits=$(git rev-list --reverse --max-count=$number HEAD)
127	fi
128	for commit in $commits ; do
129		subject=$(git log --format='%s' -1 $commit)
130		check '' $commit "$subject"
131	done
132fi
133pass=$(($total - $status))
134$quiet || printf '\n%d/%d valid patch' $pass $total
135$quiet || [ $pass -le 1 ] || printf 'es'
136$quiet || printf '\n'
137exit $status
138