scripts: reduce line size of commit checks
[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' $range)
53 headlines=$(git log --format='%s' $range)
54 bodylines=$(git log --format='%b' $range)
55 fixes=$(git log --format='%h %s' $range | grep -i ': *fix' | cut -d' ' -f1)
56 tags=$(git log --format='%b' $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 label for common typos
74 bad=$(echo "$headlines" | grep --color=always \
75         -e '^example[:/]' \
76         -e '^apps/' \
77         -e '^testpmd' \
78         -e 'test-pmd' \
79         -e '^bond:' \
80         | sed 's,^,\t,')
81 [ -z "$bad" ] || printf "Wrong headline label:\n$bad\n"
82
83 # check headline lowercase for first words
84 bad=$(echo "$headlines" | grep --color=always \
85         -e '^.*[A-Z].*:' \
86         -e ': *[A-Z]' \
87         | sed 's,^,\t,')
88 [ -z "$bad" ] || printf "Wrong headline uppercase:\n$bad\n"
89
90 # check headline uppercase (Rx/Tx, VF, L2, MAC, Linux, ARM...)
91 bad=$(echo "$headlines" | grep -E --color=always \
92         -e '\<(rx|tx|RX|TX)\>' \
93         -e '\<[pv]f\>' \
94         -e '\<l[234]\>' \
95         -e ':.*\<dma\>' \
96         -e ':.*\<pci\>' \
97         -e ':.*\<mtu\>' \
98         -e ':.*\<mac\>' \
99         -e ':.*\<vlan\>' \
100         -e ':.*\<rss\>' \
101         -e ':.*\<freebsd\>' \
102         -e ':.*\<linux\>' \
103         -e ':.*\<tilegx\>' \
104         -e ':.*\<tile-gx\>' \
105         -e ':.*\<arm\>' \
106         -e ':.*\<armv7\>' \
107         -e ':.*\<armv8\>' \
108         | sed 's,^,\t,')
109 [ -z "$bad" ] || printf "Wrong headline lowercase:\n$bad\n"
110
111 # check headline length (60 max)
112 bad=$(echo "$headlines" |
113         awk 'length>60 {print}' |
114         sed 's,^,\t,')
115 [ -z "$bad" ] || printf "Headline too long:\n$bad\n"
116
117 # check body lines length (75 max)
118 bad=$(echo "$bodylines" | grep -v '^Fixes:' |
119         awk 'length>75 {print}' |
120         sed 's,^,\t,')
121 [ -z "$bad" ] || printf "Line too long:\n$bad\n"
122
123 # check starting commit message with "It"
124 bad=$(for commit in $commits ; do
125         firstbodyline=$(git log --format='%b' -1 $commit | head -n1)
126         echo "$firstbodyline" | grep --color=always -ie '^It '
127 done | sed 's,^,\t,')
128 [ -z "$bad" ] || printf "Wrong beginning of commit message:\n$bad\n"
129
130 # check tags spelling
131 bad=$(echo "$tags" |
132         grep -v "^$bytag [^,]* <.*@.*>$" |
133         grep -v '^Fixes: [0-9a-f]\{7\}[0-9a-f]* (".*")$' |
134         sed 's,^.,\t&,')
135 [ -z "$bad" ] || printf "Wrong tag:\n$bad\n"
136
137 # check blank line after last Fixes: tag
138 bad=$(echo "$bodylines" |
139         sed -n 'N;/\nFixes:/D;/\n$/D;/^Fixes:/P' |
140         sed 's,^.,\t&,')
141 [ -z "$bad" ] || printf "Missing blank line after 'Fixes' tag:\n$bad\n"
142
143 # check missing Fixes: tag
144 bad=$(for fix in $fixes ; do
145         git log --format='%b' -1 $fix | grep -q '^Fixes: ' ||
146                 git log --format='\t%s' -1 $fix
147 done)
148 [ -z "$bad" ] || printf "Missing 'Fixes' tag:\n$bad\n"
149
150 # check Fixes: reference
151 IFS='
152 '
153 fixtags=$(echo "$tags" | grep '^Fixes: ')
154 bad=$(for fixtag in $fixtags ; do
155         hash=$(echo "$fixtag" | sed 's,^Fixes: \([0-9a-f]*\).*,\1,')
156         if git branch --contains $hash | grep -q '^\*' ; then
157                 good="Fixes: $hash "$(git log --format='("%s")' -1 $hash 2>&-)
158         else
159                 good="reference not in current branch"
160         fi
161         printf "$fixtag" | grep -v "^$good$"
162 done | sed 's,^,\t,')
163 [ -z "$bad" ] || printf "Wrong 'Fixes' reference:\n$bad\n"