1#!/usr/bin/env bash
2
3#
4# Copyright (c) 2015 Wind River Systems, Inc.
5#
6# SPDX-License-Identifier: Apache-2.0
7#
8
9exe_name=$(basename $0)
10
11# check the last n patches patches from the current branch for errors
12# usage: maintainer-checkpatch.bash [(-n <num commits>) | (-c <commit>)] [-s]
13# where: -n <num commits> selects the last n commits (default: 1)
14#        -c <commit> selects the "since" commit
15#        -s asks for a summary instead of details
16#
17# -c and -n are mutually exclusive
18
19checkpatch_switches="\
20	--patch \
21	--no-tree \
22	--show-types \
23	--max-line-length=100 \
24"
25ignore_list=BRACES,PRINTK_WITHOUT_KERN_LEVEL,SPLIT_STRING,FILE_PATH_CHANGES,GERRIT_CHANGE_ID
26
27timestamp_bin=${ZEPHYR_BASE}/scripts/checkpatch/timestamp
28timestamp="${timestamp_bin} -u"
29checkpatch_bin=${ZEPHYR_BASE}/scripts/checkpatch.pl
30checkpatch="${checkpatch_bin} ${checkpatch_switches} --ignore ${ignore_list}"
31
32ts=$(${timestamp})
33outdir=/tmp/${exe_name}-${ts}
34
35declare num_commits=1
36declare summary=n
37declare since_commit=""
38
39function usage {
40	printf "usage: %s [(-n <num commits>) | (-c <commit>)] [-s]\n" $exe_name >&2
41}
42
43function fail {
44	usage
45	exit -1
46}
47
48function format_patch_fail {
49	printf "'git format-patch' failed\n"
50	exit -1
51}
52
53function verify_needed {
54	needed="\
55		${timestamp_bin} \
56		${checkpatch_bin} \
57	"
58	for i in $needed; do
59		type $i &>/dev/null
60		if [ $? != 0 ]; then
61			printf "need '%s' but not found in PATH\n" $i >&2
62			exit -1
63		fi
64	done
65}
66
67function get_opts {
68	declare -r optstr="n:c:sh"
69	while getopts ${optstr} opt; do
70		case ${opt} in
71			n) num_commits=${OPTARG} ;;
72			c) since_commit=${OPTARG} ;;
73			s) summary=y ;;
74			h) usage; exit 0 ;;
75			*) fail ;;
76		esac
77	done
78
79	if [ ${num_commits} != 1 -a "x${since_commit}" != x ]; then
80		fail
81	fi
82}
83
84verify_needed
85get_opts $@
86
87if [ x${since_commit} != x ]; then
88	since=${since_commit}
89else
90	since=HEAD~${num_commits}
91fi
92git format-patch ${since} -o ${outdir} 2>/dev/null >/dev/null
93[ $? = 0 ] || format_patch_fail
94
95for i in $(ls ${outdir}/*); do
96	printf "\n$(basename ${i})\n"
97	if [ ${summary} = y ]; then
98		${checkpatch} $i | grep "total:"
99	else
100		${checkpatch} $i
101	fi
102done
103
104rm -rf ${outdir}
105