xref: /dpdk/devtools/check-git-log.sh (revision 33b84a2efca7ac188def108ba8b981daa7572b9a)
1#! /bin/sh
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright 2016 6WIND S.A.
4
5# Check commit logs (headlines and references)
6#
7# If any doubt about the formatting, please check in the most recent history:
8#	git log --format='%>|(15)%cr   %s' --reverse | grep -i <pattern>
9
10print_usage () {
11	cat <<- END_OF_HELP
12	usage: $(basename $0) [-h] [-nX|-r range]
13
14	Check commit log formatting.
15	The git commits to be checked can be specified as a "git log" option,
16	by latest git commits limited with -n option, or commits in the git
17	range specified with -r option.
18	e.g. To check only the last commit, ‘-n1’ or ‘-r@~..’ is used.
19	If no range provided, default is origin/main..HEAD.
20	END_OF_HELP
21}
22
23selfdir=$(dirname $(readlink -f $0))
24# The script caters for two formats, the new preferred format, and the old
25# format to ensure backward compatibility.
26# The new format is aligned with the format of the checkpatches script,
27# and allows for specifying the patches to check by passing -nX or -r range.
28# The old format allows for specifying patches by passing -X or range
29# as the first argument.
30range=${1:-origin/main..}
31
32if [ "$range" = '--help' ] ; then
33	print_usage
34	exit 0
35# convert -N to HEAD~N.. in order to comply with git-log-fixes.sh getopts
36elif printf -- "$range" | grep -q '^-[0-9]\+' ; then
37	range="HEAD$(printf -- "$range" | sed 's,^-,~,').."
38else
39	while getopts hr:n: ARG ; do
40		case $ARG in
41			n ) range="HEAD~$OPTARG.." ;;
42			r ) range=$OPTARG ;;
43			h ) print_usage ; exit 0 ;;
44			? ) print_usage ; exit 1 ;;
45		esac
46	done
47	shift $(($OPTIND - 1))
48fi
49
50commits=$(git log --format='%h' --reverse $range)
51headlines=$(git log --format='%s' --reverse $range)
52bodylines=$(git log --format='%b' --reverse $range)
53fixes=$(git log --format='%h %s' --reverse $range | grep -i ': *fix' | cut -d' ' -f1)
54stablefixes=$($selfdir/git-log-fixes.sh $range | sed '/(N\/A)$/d'  | cut -d' ' -f2)
55tags=$(git log --format='%b' --reverse $range | grep -i -e 'by *:' -e 'fix.*:')
56bytag='\(Reported\|Suggested\|Signed-off\|Acked\|Reviewed\|Tested\)-by:'
57reltag='Coverity issue:\|Bugzilla ID:\|Fixes:\|Cc:'
58
59failure=false
60
61# check headline format (spacing, no punctuation, no code)
62bad=$(echo "$headlines" | grep --color=always \
63	-e '	' \
64	-e '^ ' \
65	-e ' $' \
66	-e '\.$' \
67	-e '[,;!?&|]' \
68	-e ':.*_' \
69	-e '^[^:]\+$' \
70	-e ':[^ ]' \
71	-e ' :' \
72	| sed 's,^,\t,')
73[ -z "$bad" ] || { printf "Wrong headline format:\n$bad\n" && failure=true;}
74
75# check headline prefix when touching only drivers, e.g. net/<driver name>
76bad=$(for commit in $commits ; do
77	headline=$(git log --format='%s' -1 $commit)
78	files=$(git diff-tree --no-commit-id --name-only -r $commit)
79	[ -z "$(echo "$files" | grep -v '^\(drivers\|doc\|config\)/')" ] ||
80		continue
81	drv=$(echo "$files" | grep '^drivers/' | cut -d "/" -f 2,3 | sort -u)
82	drvgrp=$(echo "$drv" | cut -d "/" -f 1 | uniq)
83	if [ $(echo "$drvgrp" | wc -l) -gt 1 ] ; then
84		echo "$headline" | grep -v '^drivers:'
85	elif [ $(echo "$drv" | wc -l) -gt 1 ] ; then
86		echo "$headline" | grep -v "^drivers/$drvgrp"
87	else
88		echo "$headline" | grep -v "^$drv"
89	fi
90done | sed 's,^,\t,')
91[ -z "$bad" ] || { printf "Wrong headline prefix:\n$bad\n" && failure=true;}
92
93# check headline prefix for libraries
94bad=$(echo "$headlines" | grep --color=always \
95	-e '^lib/' \
96	| sed 's,^,\t,')
97[ -z "$bad" ] || { printf "Wrong headline prefix:\n$bad\n" && failure=true;}
98
99# check headline label for common typos
100bad=$(echo "$headlines" | grep --color=always \
101	-e '^example[:/]' \
102	-e '^apps/' \
103	-e '^testpmd' \
104	-e 'test-pmd' \
105	-e '^bond:' \
106	| sed 's,^,\t,')
107[ -z "$bad" ] || { printf "Wrong headline label:\n$bad\n" && failure=true;}
108
109# check headline lowercase for first words
110bad=$(echo "$headlines" | grep --color=always \
111	-e '^.*[[:upper:]].*:' \
112	-e ': *[[:upper:]]' \
113	| sed 's,^,\t,')
114[ -z "$bad" ] || { printf "Wrong headline uppercase:\n$bad\n" && failure=true;}
115
116# check headline case (Rx/Tx, VF, L2, MAC, Linux ...)
117IFS='
118'
119words="$selfdir/words-case.txt"
120for word in $(cat $words); do
121	bad=$(echo "$headlines" | grep -iw $word | grep -vw $word)
122	if [ "$word" = "Tx" ]; then
123		bad=$(echo $bad | grep -v 'OCTEON TX')
124	fi
125	for bad_line in $bad; do
126		bad_word=$(echo $bad_line | cut -d":" -f2 | grep -iwo $word)
127		[ -z "$bad_word" ] || { printf "Wrong headline case:\n\
128			\"$bad_line\": $bad_word --> $word\n" && failure=true;}
129	done
130done
131
132# check headline length (60 max)
133bad=$(echo "$headlines" |
134	awk 'length>60 {print}' |
135	sed 's,^,\t,')
136[ -z "$bad" ] || { printf "Headline too long:\n$bad\n" && failure=true;}
137
138# check body lines length (75 max)
139bad=$(echo "$bodylines" | grep -v '^Fixes:' |
140	awk 'length>75 {print}' |
141	sed 's,^,\t,')
142[ -z "$bad" ] || { printf "Line too long:\n$bad\n" && failure=true;}
143
144# check starting commit message with "It"
145bad=$(for commit in $commits ; do
146	firstbodyline=$(git log --format='%b' -1 $commit | head -n1)
147	echo "$firstbodyline" | grep --color=always -ie '^It '
148done | sed 's,^,\t,')
149[ -z "$bad" ] || { printf "Wrong beginning of commit message:\n$bad\n"\
150	&& failure=true;}
151
152# check tags spelling
153bad=$(echo "$tags" |
154	grep -v "^$bytag [^,]* <.*@.*>$" |
155	grep -v '^Fixes: [0-9a-f]\{7\}[0-9a-f]* (".*")$' |
156	sed 's,^.,\t&,')
157[ -z "$bad" ] || { printf "Wrong tag:\n$bad\n" && failure=true;}
158
159# check missing Coverity issue: tag
160bad=$(for commit in $commits; do
161	body=$(git log --format='%b' -1 $commit)
162	echo "$body" | grep -qi coverity || continue
163	echo "$body" | grep -q '^Coverity issue:' && continue
164	git log --format='\t%s' -1 $commit
165done)
166[ -z "$bad" ] || { printf "Missing 'Coverity issue:' tag:\n$bad\n"\
167	&& failure=true;}
168
169# check missing Bugzilla ID: tag
170bad=$(for commit in $commits; do
171	body=$(git log --format='%b' -1 $commit)
172	echo "$body" | grep -qi bugzilla || continue
173	echo "$body" | grep -q '^Bugzilla ID:' && continue
174	git log --format='\t%s' -1 $commit
175done)
176[ -z "$bad" ] || { printf "Missing 'Bugzilla ID:' tag:\n$bad\n"\
177	&& failure=true;}
178
179# check missing Fixes: tag
180bad=$(for fix in $fixes ; do
181	git log --format='%b' -1 $fix | grep -q '^Fixes: ' ||
182		git log --format='\t%s' -1 $fix
183done)
184[ -z "$bad" ] || { printf "Missing 'Fixes' tag:\n$bad\n" && failure=true;}
185
186# check Fixes: reference
187fixtags=$(echo "$tags" | grep '^Fixes: ')
188bad=$(for fixtag in $fixtags ; do
189	hash=$(echo "$fixtag" | sed 's,^Fixes: \([0-9a-f]*\).*,\1,')
190	if git branch --contains $hash 2>&- | grep -q '^\*' ; then
191		good="Fixes: $hash "$(git log --format='("%s")' -1 $hash 2>&-)
192	else
193		good="reference not in current branch"
194	fi
195	printf "$fixtag" | grep -v "^$good$"
196done | sed 's,^,\t,')
197[ -z "$bad" ] || { printf "Wrong 'Fixes' reference:\n$bad\n" && failure=true;}
198
199# check Cc: stable@dpdk.org for fixes
200bad=$(for fix in $stablefixes ; do
201	git log --format='%b' -1 $fix | grep -qi '^Cc: *stable@dpdk.org' ||
202		git log --format='\t%s' -1 $fix
203done)
204[ -z "$bad" ] || { printf "Is it candidate for Cc: stable@dpdk.org backport?\n$bad\n"\
205	&& failure=true;}
206
207# check tag sequence
208bad=$(for commit in $commits; do
209	body=$(git log --format='%b' -1 $commit)
210	echo "$body" |
211	grep -o -e "$reltag\|^[[:blank:]]*$\|$bytag" |
212	# retrieve tags only
213	cut -f1 -d":" |
214	# it is okay to have several tags of the same type
215	# but for processing we need to squash them
216	uniq |
217	# make sure the tags are in the proper order as presented in SEQ
218	awk -v subject="$(git log --format='\t%s' -1 $commit)" 'BEGIN{
219		SEQ[0] = "Coverity issue";
220		SEQ[1] = "Bugzilla ID";
221		SEQ[2] = "Fixes";
222		SEQ[3] = "Cc";
223		SEQ[4] = "^$";
224		SEQ[5] = "Reported-by";
225		SEQ[6] = "Suggested-by";
226		SEQ[7] = "Signed-off-by";
227		SEQ[8] = "Acked-by";
228		SEQ[9] = "Reviewed-by";
229		SEQ[10] = "Tested-by";
230		latest = 0;
231		chronological = 0;
232	}
233	{
234		for (seq = 0; seq < length(SEQ); seq++) {
235			if (chronological == 1)
236				continue;
237			if (match($0, SEQ[seq])) {
238				if (seq < latest) {
239					print subject " (" $0 ":)";
240					break;
241				} else {
242					latest = seq;
243				}
244			}
245		}
246		if (match($0, "Signed-off-by"))
247			chronological = 1;
248	 }'
249done)
250[ -z "$bad" ] || { printf "Wrong tag order: \n$bad\n"\
251	&& failure=true;}
252
253# check required tag
254bad=$(for commit in $commits; do
255	body=$(git log --format='%b' -1 $commit)
256	echo $body | grep -q "Signed-off-by:" ||
257	git log --format='\t%s' -1 $commit
258done)
259[ -z "$bad" ] || { printf "Missing 'Signed-off-by:' tag: \n$bad\n"\
260	&& failure=true;}
261
262total=$(echo "$commits" | wc -l)
263if $failure ; then
264	printf "\nInvalid patch(es) found - checked $total patch"
265else
266	printf "\n$total/$total valid patch"
267fi
268[ $total -le 1 ] || printf 'es'
269printf '\n'
270$failure && exit 1 || exit 0
271