summaryrefslogtreecommitdiff
path: root/scripts/maintainer-checkpatch.bash
blob: b799ad79684ca653c6670898b4aece7050c57aa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash

#
# Copyright (c) 2015 Wind River Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

exe_name=$(basename $0)

# check the last n patches patches from the current branch for errors
# usage: maintainer-checkpatch.bash [(-n <num commits>) | (-c <commit>)] [-s]
# where: -n <num commits> selects the last n commits (default: 1)
#        -c <commit> selects the "since" commit
#        -s asks for a summary instead of details
#
# -c and -n are mutually exclusive

checkpatch_switches="\
	--patch \
	--no-tree \
	--show-types \
	--max-line-length=100 \
"
ignore_list=BRACES,PRINTK_WITHOUT_KERN_LEVEL,SPLIT_STRING,FILE_PATH_CHANGES,GERRIT_CHANGE_ID

timestamp_bin=${ZEPHYR_BASE}/scripts/timestamp
timestamp="${timestamp_bin} -u"
checkpatch_bin=${ZEPHYR_BASE}/scripts/checkpatch.pl
checkpatch="${checkpatch_bin} ${checkpatch_switches} --ignore ${ignore_list}"

ts=$(${timestamp})
outdir=/tmp/${exe_name}-${ts}

declare num_commits=1
declare summary=n
declare since_commit=""

function usage {
	printf "usage: %s [(-n <num commits>) | (-c <commit>)] [-s]\n" $exe_name >&2
}

function fail {
	usage
	exit -1
}

function format_patch_fail {
	printf "'git format-patch' failed\n"
	exit -1
}

function verify_needed {
	needed="\
		${timestamp_bin} \
		${checkpatch_bin} \
	"
	for i in $needed; do
		type $i &>/dev/null
		if [ $? != 0 ]; then
			printf "need '%s' but not found in PATH\n" $i >&2
			exit -1
		fi
	done
}

function get_opts {
	declare -r optstr="n:c:sh"
	while getopts ${optstr} opt; do
		case ${opt} in
			n) num_commits=${OPTARG} ;;
			c) since_commit=${OPTARG} ;;
			s) summary=y ;;
			h) usage; exit 0 ;;
			*) fail ;;
		esac
	done

	if [ ${num_commits} != 1 -a "x${since_commit}" != x ]; then
		fail
	fi
}

verify_needed
get_opts $@

if [ x${since_commit} != x ]; then
	since=${since_commit}
else
	since=HEAD~${num_commits}
fi
git format-patch ${since} -o ${outdir} 2>/dev/null >/dev/null
[ $? = 0 ] || format_patch_fail

for i in $(ls ${outdir}/*); do
	printf "\n$(basename ${i})\n"
	if [ ${summary} = y ]; then
		${checkpatch} $i | grep "total:"
	else
		${checkpatch} $i
	fi
done

rm -rf ${outdir}