Imported Upstream version 16.11
[deb_dpdk.git] / scripts / check-git-log.sh
1 #! /bin/sh
2
3 # BSD LICENSE
4 #
5 # Copyright 2016 6WIND S.A.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 #   * Redistributions of source code must retain the above copyright
12 #     notice, this list of conditions and the following disclaimer.
13 #   * Redistributions in binary form must reproduce the above copyright
14 #     notice, this list of conditions and the following disclaimer in
15 #     the documentation and/or other materials provided with the
16 #     distribution.
17 #   * Neither the name of 6WIND S.A. nor the names of its
18 #     contributors may be used to endorse or promote products derived
19 #     from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 # Check commit logs (headlines and references)
34 #
35 # If any doubt about the formatting, please check in the most recent history:
36 #       git log --format='%>|(15)%cr   %s' --reverse | grep -i <pattern>
37
38 if [ "$1" = '-h' -o "$1" = '--help' ] ; then
39         cat <<- END_OF_HELP
40         usage: $(basename $0) [-h] [range]
41
42         Check commit log formatting.
43         The git range can be specified as a "git log" option,
44         e.g. -1 to check only the latest commit.
45         The default range starts from origin/master to HEAD.
46         END_OF_HELP
47         exit
48 fi
49
50 range=${1:-origin/master..}
51
52 commits=$(git log --format='%h' --reverse $range)
53 headlines=$(git log --format='%s' --reverse $range)
54 bodylines=$(git log --format='%b' --reverse $range)
55 fixes=$(git log --format='%h %s' --reverse $range | grep -i ': *fix' | cut -d' ' -f1)
56 tags=$(git log --format='%b' --reverse $range | grep -i -e 'by *:' -e 'fix.*:')
57 bytag='\(Reported\|Suggested\|Signed-off\|Acked\|Reviewed\|Tested\)-by:'
58
59 # check headline format (spacing, no punctuation, no code)
60 bad=$(echo "$headlines" | grep --color=always \
61         -e '    ' \
62         -e '^ ' \
63         -e ' $' \
64         -e '\.$' \
65         -e '[,;!?&|]' \
66         -e ':.*_' \
67         -e '^[^:]\+$' \
68         -e ':[^ ]' \
69         -e ' :' \
70         | sed 's,^,\t,')
71 [ -z "$bad" ] || printf "Wrong headline format:\n$bad\n"
72
73 # check headline prefix when touching only drivers, e.g. net/<driver name>
74 bad=$(for commit in $commits ; do
75         headline=$(git log --format='%s' -1 $commit)
76         files=$(git diff-tree --no-commit-id --name-only -r $commit)
77         [ -z "$(echo "$files" | grep -v '^\(drivers\|doc\|config\)/')" ] ||
78                 continue
79         drv=$(echo "$files" | grep '^drivers/' | cut -d "/" -f 2,3 | sort -u)
80         drvgrp=$(echo "$drv" | cut -d "/" -f 1 | uniq)
81         if [ $(echo "$drvgrp" | wc -l) -gt 1 ] ; then
82                 echo "$headline" | grep -v '^drivers:'
83         elif [ $(echo "$drv" | wc -l) -gt 1 ] ; then
84                 echo "$headline" | grep -v "^$drvgrp"
85         else
86                 echo "$headline" | grep -v "^$drv"
87         fi
88 done | sed 's,^,\t,')
89 [ -z "$bad" ] || printf "Wrong headline prefix:\n$bad\n"
90
91 # check headline label for common typos
92 bad=$(echo "$headlines" | grep --color=always \
93         -e '^example[:/]' \
94         -e '^apps/' \
95         -e '^testpmd' \
96         -e 'test-pmd' \
97         -e '^bond:' \
98         | sed 's,^,\t,')
99 [ -z "$bad" ] || printf "Wrong headline label:\n$bad\n"
100
101 # check headline lowercase for first words
102 bad=$(echo "$headlines" | grep --color=always \
103         -e '^.*[A-Z].*:' \
104         -e ': *[A-Z]' \
105         | sed 's,^,\t,')
106 [ -z "$bad" ] || printf "Wrong headline uppercase:\n$bad\n"
107
108 # check headline uppercase (Rx/Tx, VF, L2, MAC, Linux, ARM...)
109 bad=$(echo "$headlines" | grep -E --color=always \
110         -e '\<(rx|tx|RX|TX)\>' \
111         -e '\<[pv]f\>' \
112         -e '\<[hsf]w\>' \
113         -e '\<l[234]\>' \
114         -e ':.*\<api\>' \
115         -e ':.*\<arm\>' \
116         -e ':.*\<armv7\>' \
117         -e ':.*\<armv8\>' \
118         -e ':.*\<dma\>' \
119         -e ':.*\<freebsd\>' \
120         -e ':.*\<linux\>' \
121         -e ':.*\<lro\>' \
122         -e ':.*\<mac\>' \
123         -e ':.*\<mtu\>' \
124         -e ':.*\<nic\>' \
125         -e ':.*\<numa\>' \
126         -e ':.*\<pci\>' \
127         -e ':.*\<pmd\>' \
128         -e ':.*\<rss\>' \
129         -e ':.*\<tile-gx\>' \
130         -e ':.*\<tilegx\>' \
131         -e ':.*\<vlan\>' \
132         | sed 's,^,\t,')
133 [ -z "$bad" ] || printf "Wrong headline lowercase:\n$bad\n"
134
135 # special case check for VMDq to give good error message
136 bad=$(echo "$headlines" | grep -E --color=always \
137         -e '\<(vmdq|VMDQ)\>' \
138         | sed 's,^,\t,')
139 [ -z "$bad" ] || printf "Wrong headline capitalization, use 'VMDq':\n$bad\n"
140
141 # check headline length (60 max)
142 bad=$(echo "$headlines" |
143         awk 'length>60 {print}' |
144         sed 's,^,\t,')
145 [ -z "$bad" ] || printf "Headline too long:\n$bad\n"
146
147 # check body lines length (75 max)
148 bad=$(echo "$bodylines" | grep -v '^Fixes:' |
149         awk 'length>75 {print}' |
150         sed 's,^,\t,')
151 [ -z "$bad" ] || printf "Line too long:\n$bad\n"
152
153 # check starting commit message with "It"
154 bad=$(for commit in $commits ; do
155         firstbodyline=$(git log --format='%b' -1 $commit | head -n1)
156         echo "$firstbodyline" | grep --color=always -ie '^It '
157 done | sed 's,^,\t,')
158 [ -z "$bad" ] || printf "Wrong beginning of commit message:\n$bad\n"
159
160 # check tags spelling
161 bad=$(echo "$tags" |
162         grep -v "^$bytag [^,]* <.*@.*>$" |
163         grep -v '^Fixes: [0-9a-f]\{7\}[0-9a-f]* (".*")$' |
164         sed 's,^.,\t&,')
165 [ -z "$bad" ] || printf "Wrong tag:\n$bad\n"
166
167 # check blank line after last Fixes: tag
168 bad=$(echo "$bodylines" |
169         sed -n 'N;/\nFixes:/D;/\n$/D;/^Fixes:/P' |
170         sed 's,^.,\t&,')
171 [ -z "$bad" ] || printf "Missing blank line after 'Fixes' tag:\n$bad\n"
172
173 # check missing Fixes: tag
174 bad=$(for fix in $fixes ; do
175         git log --format='%b' -1 $fix | grep -q '^Fixes: ' ||
176                 git log --format='\t%s' -1 $fix
177 done)
178 [ -z "$bad" ] || printf "Missing 'Fixes' tag:\n$bad\n"
179
180 # check Fixes: reference
181 IFS='
182 '
183 fixtags=$(echo "$tags" | grep '^Fixes: ')
184 bad=$(for fixtag in $fixtags ; do
185         hash=$(echo "$fixtag" | sed 's,^Fixes: \([0-9a-f]*\).*,\1,')
186         if git branch --contains $hash 2>&- | grep -q '^\*' ; then
187                 good="Fixes: $hash "$(git log --format='("%s")' -1 $hash 2>&-)
188         else
189                 good="reference not in current branch"
190         fi
191         printf "$fixtag" | grep -v "^$good$"
192 done | sed 's,^,\t,')
193 [ -z "$bad" ] || printf "Wrong 'Fixes' reference:\n$bad\n"