devtools: standardize script arguments
[dpdk.git] / devtools / check-git-log.sh
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
10 print_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/master..HEAD.
20         END_OF_HELP
21 }
22
23 selfdir=$(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.
30 range=${1:-origin/master..}
31
32 if [ "$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
36 elif printf -- "$range" | grep -q '^-[0-9]\+' ; then
37         range="HEAD$(printf -- "$range" | sed 's,^-,~,').."
38 else
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))
48 fi
49
50 commits=$(git log --format='%h' --reverse $range)
51 headlines=$(git log --format='%s' --reverse $range)
52 bodylines=$(git log --format='%b' --reverse $range)
53 fixes=$(git log --format='%h %s' --reverse $range | grep -i ': *fix' | cut -d' ' -f1)
54 stablefixes=$($selfdir/git-log-fixes.sh $range | sed '/(N\/A)$/d'  | cut -d' ' -f2)
55 tags=$(git log --format='%b' --reverse $range | grep -i -e 'by *:' -e 'fix.*:')
56 bytag='\(Reported\|Suggested\|Signed-off\|Acked\|Reviewed\|Tested\)-by:'
57
58 # check headline format (spacing, no punctuation, no code)
59 bad=$(echo "$headlines" | grep --color=always \
60         -e '    ' \
61         -e '^ ' \
62         -e ' $' \
63         -e '\.$' \
64         -e '[,;!?&|]' \
65         -e ':.*_' \
66         -e '^[^:]\+$' \
67         -e ':[^ ]' \
68         -e ' :' \
69         | sed 's,^,\t,')
70 [ -z "$bad" ] || printf "Wrong headline format:\n$bad\n"
71
72 # check headline prefix when touching only drivers, e.g. net/<driver name>
73 bad=$(for commit in $commits ; do
74         headline=$(git log --format='%s' -1 $commit)
75         files=$(git diff-tree --no-commit-id --name-only -r $commit)
76         [ -z "$(echo "$files" | grep -v '^\(drivers\|doc\|config\)/')" ] ||
77                 continue
78         drv=$(echo "$files" | grep '^drivers/' | cut -d "/" -f 2,3 | sort -u)
79         drvgrp=$(echo "$drv" | cut -d "/" -f 1 | uniq)
80         if [ $(echo "$drvgrp" | wc -l) -gt 1 ] ; then
81                 echo "$headline" | grep -v '^drivers:'
82         elif [ $(echo "$drv" | wc -l) -gt 1 ] ; then
83                 echo "$headline" | grep -v "^drivers/$drvgrp"
84         else
85                 echo "$headline" | grep -v "^$drv"
86         fi
87 done | sed 's,^,\t,')
88 [ -z "$bad" ] || printf "Wrong headline prefix:\n$bad\n"
89
90 # check headline label for common typos
91 bad=$(echo "$headlines" | grep --color=always \
92         -e '^example[:/]' \
93         -e '^apps/' \
94         -e '^testpmd' \
95         -e 'test-pmd' \
96         -e '^bond:' \
97         | sed 's,^,\t,')
98 [ -z "$bad" ] || printf "Wrong headline label:\n$bad\n"
99
100 # check headline lowercase for first words
101 bad=$(echo "$headlines" | grep --color=always \
102         -e '^.*[[:upper:]].*:' \
103         -e ': *[[:upper:]]' \
104         | sed 's,^,\t,')
105 [ -z "$bad" ] || printf "Wrong headline uppercase:\n$bad\n"
106
107 # check headline case (Rx/Tx, VF, L2, MAC, Linux ...)
108 IFS='
109 '
110 words="$selfdir/words-case.txt"
111 for word in $(cat $words); do
112         bad=$(echo "$headlines" | grep -iw $word | grep -v $word)
113         if [ "$word" = "Tx" ]; then
114                 bad=$(echo $bad | grep -v 'OCTEON\ TX')
115         fi
116         for bad_line in $bad; do
117                 bad_word=$(echo $bad_line | cut -d":" -f2 | grep -io $word)
118                 if [ -n "$bad_word" ]; then
119                         printf "Wrong headline case:\n\"$bad_line\": $bad_word --> $word\n"
120                 fi
121         done
122 done
123
124 # check headline length (60 max)
125 bad=$(echo "$headlines" |
126         awk 'length>60 {print}' |
127         sed 's,^,\t,')
128 [ -z "$bad" ] || printf "Headline too long:\n$bad\n"
129
130 # check body lines length (75 max)
131 bad=$(echo "$bodylines" | grep -v '^Fixes:' |
132         awk 'length>75 {print}' |
133         sed 's,^,\t,')
134 [ -z "$bad" ] || printf "Line too long:\n$bad\n"
135
136 # check starting commit message with "It"
137 bad=$(for commit in $commits ; do
138         firstbodyline=$(git log --format='%b' -1 $commit | head -n1)
139         echo "$firstbodyline" | grep --color=always -ie '^It '
140 done | sed 's,^,\t,')
141 [ -z "$bad" ] || printf "Wrong beginning of commit message:\n$bad\n"
142
143 # check tags spelling
144 bad=$(echo "$tags" |
145         grep -v "^$bytag [^,]* <.*@.*>$" |
146         grep -v '^Fixes: [0-9a-f]\{7\}[0-9a-f]* (".*")$' |
147         sed 's,^.,\t&,')
148 [ -z "$bad" ] || printf "Wrong tag:\n$bad\n"
149
150 # check missing Coverity issue: tag
151 bad=$(for commit in $commits; do
152         body=$(git log --format='%b' -1 $commit)
153         echo "$body" | grep -qi coverity || continue
154         echo "$body" | grep -q '^Coverity issue:' && continue
155         git log --format='\t%s' -1 $commit
156 done)
157 [ -z "$bad" ] || printf "Missing 'Coverity issue:' tag:\n$bad\n"
158
159 # check missing Bugzilla ID: tag
160 bad=$(for commit in $commits; do
161         body=$(git log --format='%b' -1 $commit)
162         echo "$body" | grep -qi bugzilla || continue
163         echo "$body" | grep -q '^Bugzilla ID:' && continue
164         git log --format='\t%s' -1 $commit
165 done)
166 [ -z "$bad" ] || printf "Missing 'Bugzilla ID:' tag:\n$bad\n"
167
168 # check missing Fixes: tag
169 bad=$(for fix in $fixes ; do
170         git log --format='%b' -1 $fix | grep -q '^Fixes: ' ||
171                 git log --format='\t%s' -1 $fix
172 done)
173 [ -z "$bad" ] || printf "Missing 'Fixes' tag:\n$bad\n"
174
175 # check Fixes: reference
176 fixtags=$(echo "$tags" | grep '^Fixes: ')
177 bad=$(for fixtag in $fixtags ; do
178         hash=$(echo "$fixtag" | sed 's,^Fixes: \([0-9a-f]*\).*,\1,')
179         if git branch --contains $hash 2>&- | grep -q '^\*' ; then
180                 good="Fixes: $hash "$(git log --format='("%s")' -1 $hash 2>&-)
181         else
182                 good="reference not in current branch"
183         fi
184         printf "$fixtag" | grep -v "^$good$"
185 done | sed 's,^,\t,')
186 [ -z "$bad" ] || printf "Wrong 'Fixes' reference:\n$bad\n"
187
188 # check Cc: stable@dpdk.org for fixes
189 bad=$(for fix in $stablefixes ; do
190         git log --format='%b' -1 $fix | grep -qi '^Cc: *stable@dpdk.org' ||
191                 git log --format='\t%s' -1 $fix
192 done)
193 [ -z "$bad" ] || printf "Is it candidate for Cc: stable@dpdk.org backport?\n$bad\n"