summaryrefslogtreecommitdiffstats
path: root/src/build/tools/verify-commit
blob: f42d53884e6a270f39dd3d5b8088dee0c73b83e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/tools/verify-commit $
#
# IBM CONFIDENTIAL
#
# COPYRIGHT International Business Machines Corp. 2013
#
# p1
#
# Object Code Only (OCO) source materials
# Licensed Internal Code Source Materials
# IBM HostBoot Licensed Internal Code
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.
#
# Origin: 30
#
# IBM_PROLOG_END_TAG

use strict;

my $issueFound = 0;
my $errorFound = 0;

verifyPatchSet();       # Verify the patch contents.
verifyCommitMsg();      # Verify the commit message.

# Finish out the divider.
if ($issueFound)
{
    print "------------------------------------------------------------\n";
}

# Return a bad RC if we found an error.  Let warnings pass.
exit ($errorFound ? -1 : 0);


########################### Subroutines ################################

# @sub verifyPatchSet
#
# Extract the contents (lines changed) from the patch set and verify.
#
sub verifyPatchSet
{
    # git-diff options:
    #   * Diff against the previous commit (HEAD~1)
    #   * Filter only added and modified files (AM).
    #   * Show only the lines changed, with no context (U0).
    # Grep only the lines marked with "+" (instead of "-") to find just the
    # actions done by this patchset and not the content removed.
    open PATCH_CONTENTS, "git diff HEAD~1 --diff-filter=AM -U0 | ".
                         "grep -e \"^+\" -e \"^@@.*+[0-9]\\+\" |";

    my %fileContents = ();

    my $lastFile = "";
    my $fileLines = ();
    my $lineCount = 0;
    while (my $line = <PATCH_CONTENTS>)
    {
        chomp $line;

        # Line starting with "+++ b/path/to/file" indicate a new file.
        if ($line =~ m/^\+\+\+ b\/(.*)/)
        {
            # Save previous file into the map.
            if ($lastFile ne "")
            {
                $fileContents{$lastFile} = $fileLines;
                $fileLines = ();
                $lineCount = 0;
            }
            $lastFile = $1;
        }
        # Lines starting with "@@" indicates a seek in the file, so update
        # line numbers.
        elsif ($line =~ m/^@@.*\+([0-9]+)/)
        {
            $lineCount = $1 - 1;
        }
        else
        {
            $line =~ s/^\+//; # filter off the leading + symbol.
            $lineCount++;
            push @{$fileLines}, [$line, $lineCount];
        }
    }
    if ($lastFile ne "") # Save last file into the map.
    {
        $fileContents{$lastFile} = $fileLines;
        $fileLines = ();
    }

    # Verify each line of each file.
    foreach my $file (sort keys %fileContents)
    {
        foreach my $line (@{$fileContents{$file}})
        {
            verifyFileLine($file, @{$line}[0], @{$line}[1]);
        }
    }
}

# @sub verifyFileLine
#
# Checks a particular line of the file for the following issues:
#     * Warning: Lines longer than 80 characters, except in trace statement.
#     * Warning: Trailing whitespace.
#     * Warning: Tab characters outside of makefiles.
#     * Warning: TODO or FIXME type tag without a corresponding RTC number.
#     * Warning: NOMERGE tag found.
#
sub verifyFileLine
{
    my ($file,$line,$count) = @_;

    # Check line length.
    if (length($line) > 80)
    {
        # Allow trace statements to slide.
        if (($line =~ m/TRAC[DSFU]/) ||
            ($line =~m/TS_FAIL/) ||
            ($line =~m/printk/) ||
            ($line =~ m/FAPI_(INF|IMP|ERR|DBG|SCAN)/))
        {
        }
        else
        {
            warning($file,$line,$count,
                    (sprintf "Length is more than 80 characters (%d).",
                     length($line))
                   );
        }
    }

    # Check trailing whitespace.
    if ($line =~ m/\s$/)
    {
        warning($file,$line,$count,
                "Trailing whitespace found.");
    }

    # Check tabs.
    if ($line =~ m/\t/)
    {
        # Makefiles are ok (require tabs).
        if (not (($file =~ m/makefile/) || ($file =~ m/\.mk/)))
        {
            warning($file,$line,$count,
                    "Tab character found.");
        }
    }

    # Check "TODO" or "FIXME" type comments.
    if (($line =~ m/TODO/i) || ($line =~ m/FIXME/i))
    {
        if (not ($line =~ m/RTC[\s:]*[0-9]*/))
        {
            warning($file,$line,$count,
                    "TODO/FIXME tag without corresponding RTC number.");
        }
    }

    # Check "NOMERGE" type comment.
    if ($line =~ m/NOMERGE/i)
    {
        warning($file,$line,$count,
                "NOMERGE tag found.");
    }
}

# @sub verifyCommitMsg
#
# Looks at the commit message to verify the following items:
#    * Topic is exactly 1 line long.
#    * Lines are less than 80 characters.
#    * No trailing whitespace.
#    * Tags, such as 'RTC:', are only found in the footer.
#    * Untagged lines are not found in the footer.
#    * RTC tag is formatted correctly.
#    * Warning for lacking RTC tag.
#
sub verifyCommitMsg
{
    open COMMIT_CONTENTS, "git log -n1 --pretty=format:%B |";
    my $lineCount = 0;
    my $rtcTag = "";
    my $cqTag = "";
    my $taggedLine = "";
    my $untaggedLine = "";

    while (my $line = <COMMIT_CONTENTS>)
    {
        $lineCount++;
        chomp $line;

        # Check line length over 80 characters.
        if (length($line) > 80)
        {
            error("Commit Message",$line,$lineCount,
                    (sprintf "Length is more than 80 characters (%d).",
                     length($line))
                 );
        }

        # Check trailing whitespace.
        if ($line =~ m/[^\s]+\s$/)
        {
            error("Commit Message",$line,$lineCount,
                  "Trailing whitespace found.");
        }

        # Blank line indicates a new "section".
        if ($line =~ m/^$/)
        {
            # Check for tags outside of the footer.
            # (new section implies previous section was not a footer)
            if ("" ne $taggedLine)
            {
                error("Commit Message",$taggedLine,$lineCount,
                      "Tagged line found outside commit-msg footer.");
            }

            $rtcTag = "";
            $cqTag = "";
            $untaggedLine = "";
            $taggedLine = "";
        }
        else
        {
            # Check for multi-line topic.
            if ($lineCount == 2)
            {
                error("Commit Message",$line,$lineCount,
                      "Topic must be only one line long.");
            }
        }

        # Verify format of RTC message.
        if ($line =~ m/^\s*RTC:\s*[0-9]+(.*)/)
        {
            if ("" ne $rtcTag)
            {
                error("Commit Message",$line,$lineCount,
                      "Duplicate RTC tag found.");
            }

            $rtcTag = $line;
            if ("" ne $1)
            {
                error("Commit Message",$line,$lineCount,
                        (sprintf "RTC tag format incorrect (%s).", $1));
            }
        }

        if ($line =~ m/^\s*CQ: \s*[A-Z][A-Z][0-9]+(.*)/)
        {
            if ("" ne $cqTag)
            {
                error("Commit Message",$line,$lineCount,
                      "Duplicate CQ tag found.");
            }

            $cqTag = $line;
            if ("" ne $1)
            {
                error("Commit Message",$line,$lineCount,
                        (sprintf "CQ tag format incorrect (%s).", $1));
            }
        }

        # Identify if this is a tagged line or a non-tagged line and store
        # away.
        if ($line =~ m/^\s*[A-Za-z0-9\-_]+:[^:]/)
        {
            # We allow lines that look like tags in the topic like...
            # "FOO: Adding support for BAR."
            if ($lineCount > 1)
            {
                $taggedLine = $line;
            }
        }
        else
        {
            $untaggedLine = $line;
        }
    }

    # Warn for missing RTC tag.
    if (("" eq $rtcTag) && ("" eq $cqTag))
    {
        warning("Commit Message","<end-of-file>",$lineCount,
                "Neither RTC nor CQ tag found.");
    }

    # Error for a mix of tag / untagged in the last section (ie. untagged
    # lines in the footer).
    if (("" ne $untaggedLine) && ("" ne $taggedLine))
    {
        error("Commit Message",$untaggedLine,$lineCount,
                "Untagged line found in footer.");
    }
}

sub warning
{
    my ($file, $line, $count, $statement) = @_;
    print "------------------------------------------------------------\n";
    print "WARNING: $statement\n";
    print "  $file:$count\n";
    print "    $line\n";

    $issueFound = 1;
}

sub error
{
    my ($file, $line, $count, $statement) = @_;
    print "------------------------------------------------------------\n";
    print "ERROR: $statement\n";
    print "  $file:$count\n";
    print "    $line\n";

    $issueFound = 1;
    $errorFound = 1;
}

OpenPOWER on IntegriCloud