xref: /dpdk/devtools/check-git-log.sh (revision c1d145834f287aa8cf53de914618a7312f2c360e)
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 [ "$drv" = "net/intel" ] ; then
84		drvgrp=$drv
85		drv=$(echo "$files" | grep '^drivers/' | cut -d "/" -f 2,4 | sort -u)
86		if [ $(echo "$drv" | wc -l) -ne 1 ] ; then
87			drv='net/intel'
88		elif [ "$drv" = "net/common" ] ; then
89			drv='net/intel/common'
90		fi
91	fi
92	if [ $(echo "$drvgrp" | wc -l) -gt 1 ] ; then
93		echo "$headline" | grep -v '^drivers:'
94	elif [ $(echo "$drv" | wc -l) -gt 1 ] ; then
95		echo "$headline" | grep -v "^drivers/$drvgrp"
96	else
97		echo "$headline" | grep -v "^$drv"
98	fi
99done | sed 's,^,\t,')
100[ -z "$bad" ] || { printf "Wrong headline prefix:\n$bad\n" && failure=true;}
101
102# check headline prefix for libraries
103bad=$(echo "$headlines" | grep --color=always \
104	-e '^lib/' \
105	| sed 's,^,\t,')
106[ -z "$bad" ] || { printf "Wrong headline prefix:\n$bad\n" && failure=true;}
107
108# check headline label for common typos
109bad=$(echo "$headlines" | grep --color=always \
110	-e '^example[:/]' \
111	-e '^apps/' \
112	-e '^testpmd' \
113	-e 'test-pmd' \
114	-e '^bond:' \
115	| sed 's,^,\t,')
116[ -z "$bad" ] || { printf "Wrong headline label:\n$bad\n" && failure=true;}
117
118# check headline lowercase for first words
119bad=$(echo "$headlines" | grep --color=always \
120	-e '^.*[[:upper:]].*:' \
121	-e ': *[[:upper:]]' \
122	| sed 's,^,\t,')
123[ -z "$bad" ] || { printf "Wrong headline uppercase:\n$bad\n" && failure=true;}
124
125# check headline case (Rx/Tx, VF, L2, MAC, Linux ...)
126IFS='
127'
128words="$selfdir/words-case.txt"
129for word in $(cat $words); do
130	bad=$(echo "$headlines" | grep -iw $word | grep -vw $word)
131	if [ "$word" = "Tx" ]; then
132		bad=$(echo $bad | grep -v 'OCTEON TX')
133	fi
134	for bad_line in $bad; do
135		bad_word=$(echo $bad_line | cut -d":" -f2 | grep -iwo $word)
136		[ -z "$bad_word" ] || { printf "Wrong headline case:\n\
137			\"$bad_line\": $bad_word --> $word\n" && failure=true;}
138	done
139done
140
141# check headline length (60 max)
142bad=$(echo "$headlines" |
143	awk 'length>60 {print}' |
144	sed 's,^,\t,')
145[ -z "$bad" ] || { printf "Headline too long:\n$bad\n" && failure=true;}
146
147# check body lines length (75 max)
148bad=$(echo "$bodylines" | grep -v '^Fixes:' |
149	awk 'length>75 {print}' |
150	sed 's,^,\t,')
151[ -z "$bad" ] || { printf "Line too long:\n$bad\n" && failure=true;}
152
153# check starting commit message with "It"
154bad=$(for commit in $commits ; do
155	firstbodyline=$(git log --format='%b' -1 $commit | head -n1)
156	echo "$firstbodyline" | grep --color=always -ie '^It '
157done | sed 's,^,\t,')
158[ -z "$bad" ] || { printf "Wrong beginning of commit message:\n$bad\n"\
159	&& failure=true;}
160
161# check tags spelling
162bad=$(echo "$tags" |
163	grep -v "^$bytag [^,]* <.*@.*>$" |
164	grep -v '^Fixes: [0-9a-f]\{7\}[0-9a-f]* (".*")$' |
165	sed 's,^.,\t&,')
166[ -z "$bad" ] || { printf "Wrong tag:\n$bad\n" && failure=true;}
167
168# check missing Coverity issue: tag
169bad=$(for commit in $commits; do
170	body=$(git log --format='%b' -1 $commit)
171	echo "$body" | grep -qi coverity || continue
172	echo "$body" | grep -q '^Coverity issue:' && continue
173	git log --format='\t%s' -1 $commit
174done)
175[ -z "$bad" ] || { printf "Missing 'Coverity issue:' tag:\n$bad\n"\
176	&& failure=true;}
177
178# check missing Bugzilla ID: tag
179bad=$(for commit in $commits; do
180	body=$(git log --format='%b' -1 $commit)
181	echo "$body" | grep -qi bugzilla || continue
182	echo "$body" | grep -q '^Bugzilla ID:' && continue
183	git log --format='\t%s' -1 $commit
184done)
185[ -z "$bad" ] || { printf "Missing 'Bugzilla ID:' tag:\n$bad\n"\
186	&& failure=true;}
187
188# check missing Fixes: tag
189bad=$(for fix in $fixes ; do
190	git log --format='%b' -1 $fix | grep -q '^Fixes: ' ||
191		git log --format='\t%s' -1 $fix
192done)
193[ -z "$bad" ] || { printf "Missing 'Fixes' tag:\n$bad\n" && failure=true;}
194
195# check Fixes: reference
196fixtags=$(echo "$tags" | grep '^Fixes: ')
197bad=$(for fixtag in $fixtags ; do
198	hash=$(echo "$fixtag" | sed 's,^Fixes: \([0-9a-f]*\).*,\1,')
199	if git branch --contains $hash 2>&- | grep -q '^\*' ; then
200		good="Fixes: $hash "$(git log --format='("%s")' -1 $hash 2>&-)
201	else
202		good="reference not in current branch"
203	fi
204	printf "$fixtag" | grep -v "^$good$"
205done | sed 's,^,\t,')
206[ -z "$bad" ] || { printf "Wrong 'Fixes' reference:\n$bad\n" && failure=true;}
207
208# check Cc: stable@dpdk.org for fixes
209bad=$(for fix in $stablefixes ; do
210	git log --format='%b' -1 $fix | grep -qi '^Cc: *stable@dpdk.org' ||
211		git log --format='\t%s' -1 $fix
212done)
213[ -z "$bad" ] || { printf "Is it candidate for Cc: stable@dpdk.org backport?\n$bad\n"\
214	&& failure=true;}
215
216# check tag sequence
217bad=$(for commit in $commits; do
218	body=$(git log --format='%b' -1 $commit)
219	echo "$body" |
220	grep -o -e "$reltag\|^[[:blank:]]*$\|$bytag" |
221	# retrieve tags only
222	cut -f1 -d":" |
223	# it is okay to have several tags of the same type
224	# but for processing we need to squash them
225	uniq |
226	# make sure the tags are in the proper order as presented in SEQ
227	awk -v subject="$(git log --format='\t%s' -1 $commit)" 'BEGIN{
228		SEQ[0] = "Coverity issue";
229		SEQ[1] = "Bugzilla ID";
230		SEQ[2] = "Fixes";
231		SEQ[3] = "Cc";
232		SEQ[4] = "^$";
233		SEQ[5] = "Reported-by";
234		SEQ[6] = "Suggested-by";
235		SEQ[7] = "Signed-off-by";
236		SEQ[8] = "Acked-by";
237		SEQ[9] = "Reviewed-by";
238		SEQ[10] = "Tested-by";
239		latest = 0;
240		chronological = 0;
241	}
242	{
243		for (seq = 0; seq < length(SEQ); seq++) {
244			if (chronological == 1)
245				continue;
246			if (match($0, SEQ[seq])) {
247				if (seq < latest) {
248					print subject " (" $0 ":)";
249					break;
250				} else {
251					latest = seq;
252				}
253			}
254		}
255		if (match($0, "Signed-off-by"))
256			chronological = 1;
257	 }'
258done)
259[ -z "$bad" ] || { printf "Wrong tag order: \n$bad\n"\
260	&& failure=true;}
261
262# check required tag
263bad=$(for commit in $commits; do
264	body=$(git log --format='%b' -1 $commit)
265	echo $body | grep -q "Signed-off-by:" ||
266	git log --format='\t%s' -1 $commit
267done)
268[ -z "$bad" ] || { printf "Missing 'Signed-off-by:' tag: \n$bad\n"\
269	&& failure=true;}
270
271# check names
272names=$(git log --format='From: %an <%ae>%n%b' --reverse $range |
273	sed -rn 's,.*: (.*<.*@.*>),\1,p' |
274	sort -u)
275bad=$(for contributor in $names ; do
276	contributor=$(echo $contributor | sed 's,(,\\(,')
277	! grep -qE "^$contributor($| <)" $selfdir/../.mailmap || continue
278	name=${contributor%% <*}
279	if grep -q "^$name <" $selfdir/../.mailmap ; then
280		printf "\t$contributor is not the primary email address\n"
281	else
282		printf "\t$contributor is unknown in .mailmap\n"
283	fi
284done)
285[ -z "$bad" ] || { printf "Contributor name/email mismatch with .mailmap: \n$bad\n"\
286	&& failure=true;}
287
288total=$(echo "$commits" | wc -l)
289if $failure ; then
290	printf "\nInvalid patch(es) found - checked $total patch"
291else
292	printf "\n$total/$total valid patch"
293fi
294[ $total -le 1 ] || printf 'es'
295printf '\n'
296$failure && exit 1 || exit 0
297