summaryrefslogtreecommitdiff
path: root/git-hooks/sanitize-commit
blob: 36d0e4fb4e1a6179cbc0fa319a66d6a6160998ff (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#! /usr/bin/perl -w
# Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved.
#
# Contact:  Nokia Corporation <info@qt.nokia.com>
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
#

use strict;

if ($#ARGV < 0 or $#ARGV > 1 || ($#ARGV == 1 && $ARGV[1] !~ /^(strict|gerrit)$/)) {
    print STDERR "Usage: $0 <sha1> [strict]\n";
    exit 2;
}
my $sha1 = $ARGV[0];
my $gerrit = ($#ARGV == 1 && $ARGV[1] eq "gerrit");
my $strict = $gerrit || ($#ARGV == 1 && $ARGV[1] eq "strict");

my %cfg = ();
if (defined $ENV{GIT_PUSH}) {
    foreach my $c (split ",", $ENV{GIT_PUSH}) {
        $cfg{$c} = 1;
    }
}
my $fail = 0;
my $printed = $gerrit;
my $file = "";
my $fail_file = "-";
my $summary;
my ($lpfx, $elpfx) = ($gerrit ? ("", "\n") : ("***   ", "***\n"));

sub printerr()
{
  die "cannot run git: ".$! if ($? < 0);
  die "git crashed with signal ".$? if ($? & 127);
  die "git exited with status ".($? >> 8) if ($?);
}

sub complain()
{
    my ($msg, $key, $level) = @_;
    my $pfx;

    if (!$printed) {
        $summary =~ s/^(.{50}).{5,}$/$1\[...]/;
        print "***\n*** Suspicious changes in commit ".$sha1." (".$summary."):\n";
        $printed = 1;
    }
    if (length($file)) {
        if ($file ne $fail_file) {
            print $elpfx.$lpfx.$file.":\n";
            $fail_file = $file;
        }
        $pfx = $lpfx."  - ";
    } else {
        if ($file ne $fail_file) {
            print $elpfx;
            $fail_file = "";
        }
        $pfx = $lpfx."- ";
    }
    $level = 0 if (!defined($level) || ($level < 0 && $strict && length($key)));
    if ($level >= 0) {
        $fail = $level + 1 if ($level >= $fail);
        if ($gerrit) {
            print $pfx.$msg."\n";
        } else {
            print $pfx.$msg." (key \"".$key."\")\n";
        }
    } else {
        print $pfx."Hint: ".$msg."\n";
    }
}

my $ln = 0;
my $iswip = defined($cfg{wip});
my $revok = defined($cfg{revby});
my $badlog = defined($cfg{log});
my $parents = 0;
my ($badauthor, $badcommitter) = (0, 0);
my ($revert1, $revert2, $nonrevert) = (0, 0, 0);
open MSG, "git log -1 --pretty=raw ".$sha1." |" or die "cannot run git: $!";
while (<MSG>) {
    chomp;
    if (!s/^    //) {
        if (/^parent /) {
            $parents++ ;
        } elsif (/^author .*\.\(none\)/) {
            $badauthor = 1;
        } elsif (/^commiter .*\.\(none\)/) {
            $badcommitter = 1;
        }
        next
    }
    if ($ln == 0) {
        $summary = $_;
        $revert1 = 1 if (/^Revert ".*"$/);
        if (!$iswip && $parents < 2 && /\bWIP\b|\*{3}|^(?:squash|fixup)! |^(.)\1*$/i) {
            &complain("Apparently pushing a Work In Progress", "wip", 1);
        } elsif (!$iswip && !$badlog && length($_) < 7) {
            &complain("Log message summary is too short", "log");
        } elsif (!$badlog && !$revert1 && length($_) > 120) {
            &complain("Log message summary is excessively long", "log");
        } elsif ($parents < 2 && !$revert1 && length($_) > 70) {
            &complain("Aim for shorter log message summaries", "", -1);
        }
    } else {
        if (/^This reverts commit [[:xdigit:]]{40}\.?$/) {
            $revert2 = 1;
        } elsif (!/^[-\w]+:|^$/) {
            $nonrevert = 1;
        }
        if ($ln == 1) {
            if (!$badlog && $_ ne "") {
                &complain("2nd line of log message is not empty", "log");
            }
        } else {
            if (!$revok && /^Reviewed-by: *(pending|TBD)?$/i) {
                &complain("Bogus or empty Reviewed-by header", "revby", 1);
                $revok = 1;
            }
        }
    }
    $ln++;
}
close MSG;
printerr;

if ($revert1 && $revert2 && !$nonrevert) {
    &complain("Revert without justification", "revert", 1);
}
# These need to be delayed, because at the time they are found the subject is not known yet.
if ($badauthor) {
    &complain("Bogus author email", "email", 1);
}
if ($badcommitter) {
    &complain("Bogus committer email", "email", 1);
}

my $chunk = 0;
my @addi = ();
my @deli = ();
my $nonws;
my $ws;
my $mixws_check = 0;
my $lineno = 0;
my @ws_files = ();
my %ws_lines = (); # hash of lists
my $braces = 0;
my $open_key = qr/\s*#\s*if|.*{/;
my $close_key = qr/\s*#\s*endif|.*}/;
my $kill_all_ws = qr/\s+((?:\"(?:\\.|[^\"])*\"|\S)+)/; # Collapse all whitespace not inside strings.
my $kill_nl_ws = qr/((?:\"(?:\\.|[^\"])*\"|\S)+)\s+/; # Collapse all non-leading whitespace not inside strings.

sub flushChunk()
{
    my $loc_nonws = 0;
    my $nlonly = 1;
    my ($ai, $di) = (0, 0);
    while (!$loc_nonws) {
        my ($a, $d) = ("", "");
        while ($ai < @addi) {
            $a = $addi[$ai++];
            $a =~ s/\s+$//;
            if (length($a)) {
                $nlonly = 0;
                last;
            }
        }
        while ($di < @deli) {
            $d = $deli[$di++];
            $d =~ s/\s+$//;
            if (length($d)) {
                $nlonly = 0;
                last;
            }
        }
        last if (!length($a) && !length($d));

        $a =~ /^$close_key/o and $braces--;
        $d =~ /^$close_key/o and $braces++;
        if ($braces) {
            $a =~ s/$kill_nl_ws/$1/go;
            $d =~ s/$kill_nl_ws/$1/go;
        } else {
            $a =~ s/$kill_all_ws/$1/go;
            $d =~ s/$kill_all_ws/$1/go;
        }
        $loc_nonws = 1 if ($a ne $d);
        $a =~ /^$open_key/o and $braces++;
        $d =~ /^$open_key/o and $braces--;
    }
    while ($ai < @addi) {
        my $a = $addi[$ai++];
        $a =~ /^$close_key/o and $braces--;
        $a =~ /^$open_key/o and $braces++;
    }
    while ($di < @deli) {
        my $d = $deli[$di++];
        $d =~ /^$close_key/o and $braces++;
        $d =~ /^$open_key/o and $braces--;
    }
    if ($loc_nonws) {
        $nonws = 1;
    } elsif (!$nlonly) {
        $ws = 1;
        push @ws_files, $file if (!defined($ws_lines{$file}));
        push @{$ws_lines{$file}}, $lineno - $#addi;
    }
    @addi = @deli = ();
    $chunk = 0;
}

sub formatSize($)
{
    my $sz = shift;
    if ($sz >= 10 * 1024 * 1024) {
        return int($sz / (1024 * 1024))."MiB";
    } elsif ($sz >= 10 * 1024) {
        return int($sz / 1024)."KiB";
    } else {
        return int($sz)."B";
    }
}

sub isExe($)
{
    my $sha = shift;
    my $type = `git cat-file -p $sha | file -b -`;
    return $type =~ /^(ELF|PE32) /;
}

my @style_fails = ();

sub styleFail($)
{
    my $why = shift;
    push @style_fails, $lineno.": ".$why;
}

sub flushFile()
{
    if (@style_fails) {
        &complain("Style issues", "style", -1);
        for my $sf (@style_fails) {
            print $lpfx."    ".$sf."\n";
        }
        @style_fails = ();
    }
}

my $merge;
my $new_file;
my $maybe_bin;
my $is_submodule;
my $clike;
my $size;
my $check_gen = 0;
my $crlf_fail;
my $conflict_fail;
my $tabs_check;
my $ws_check;
my $tsv_check;
my $ctlkw_check;
open DIFF, "git diff-tree --no-commit-id --diff-filter=ACMR --src-prefix=\@old\@/ --dst-prefix=\@new\@/ --full-index -r -U100000 --cc -C -l1000 --root ".$sha1." |" or die "cannot run git: $!";
while (<DIFF>) {
    if (/^-/) {
        if ($mixws_check) {
            /^--- / and next;
            push @deli, substr($_, 1);
            $chunk = 1;
        }
        next;
    }
    if ($lineno < 50 && $check_gen) {
        if (/All changes made in this file will be lost|This file is automatically generated|DO NOT EDIT|DO NOT delete this file|[Gg]enerated by|uicgenerated|produced by gperf/) {
            &complain("Adding generated file", "generated") if ($new_file && !defined($cfg{generated}));
            $ws_check = 0;
            $check_gen = 0;
        }
    }
    if (/^\+/) {
        if (/^\+\+\+ /) {
            # This indicates a text file; binary files have "Binary files ... and ... differ" instead.
            $maybe_bin = 0;
            if ($file =~ /\.(prl|la|pc|ilk)$/i) {
                &complain("Adding build artifact", "generated") if ($new_file && !defined($cfg{generated}));
                $ws_check = 0;
            } else {
                $check_gen = 1;
            }
            next;
        }
        $lineno++;
        if ($merge) {
            # Consider only lines which are new relative to both parents, i.e., were added during the merge.
            s/^\+\+// or next;
        } else {
           $_ = substr($_, 1);
            if ($mixws_check) {
                push @addi, $_;
                $chunk = 1;
            }
        }
        if (!$crlf_fail && /\r\n$/) {
            $crlf_fail = 1;
            &complain("CRLF line endings", "crlf");
        }
        if (!$conflict_fail && /^(?:[<>=]){7}( |$)/) {
            &complain("Unresolved merge conflict", "conflict");
            $conflict_fail = 1;
        }
        if ($ws_check) {
            if ($tsv_check) {
                styleFail("Mixing spaces with TABs") if (/^ +\t|\t +/);
            } else {
                styleFail("Space indent followed by a TAB character") if (/^ +\t/);
                styleFail("TAB character in non-leading whitespace") if (/\S *\t/);
                styleFail("Trailing whitespace") if (/[ \t]\r?\n$/);
                if ($tabs_check) {
                    styleFail("Leading tabs") if (/^\t+/);
                    if ($ctlkw_check) {
                        styleFail("Flow control keywords must be followed by a single space")
                           if (/\b(if|for|foreach|Q_FOREACH|while|do|switch)(|  +)\(/);
                    }
                }
            }
        }
    } else {
        flushChunk() if ($chunk);
        if (/^ /) {
            $lineno++ if (!$merge || !/^ -/);
            next;
        }
        if ($merge ? /^\@\@\@ -\S+ -\S+ \+(\d+)/ : /^\@\@ -\S+ \+(\d+)/) {
            $lineno = $1 - 1;
            next;
        }
        if (/^diff /) {
            flushFile();
            if (/^diff --git \@old\@\/.+ \@new\@\/(.+)$/) {
                $merge = 0;
            } elsif (/^diff --cc (.+)$/) {
                $merge = 1;
            } else {
                print STDERR "Warning: cannot parse diff header '".$_."'\n";
                next;
            }
            $file = $1;
            #print "*** got file ".$file.".\n";
            $clike = ($file =~ /\.(c|cc|cpp|c\+\+|cxx|qdoc|m|mm|h|hpp|hxx|cs|java|js|qs|qml|g|y|ypp|pl|glsl)$/i);
            my $foreign = ($file =~ /\/3rdparty\//);
            $new_file = 0;
            $maybe_bin = 0;
            $is_submodule = 0;
            $crlf_fail = defined($cfg{crlf});
            $mixws_check = !$merge && !$foreign && $clike && !defined($cfg{mixws});
            $ws_check = !defined($cfg{style}) && !$foreign && ($file !~ /\.(ts|diff|patch)$|^\.gitmodules$/);
            $tsv_check = $ws_check && ($file =~ /((^|\/)objects\.map$|\.tsv$)/);
            $tabs_check = $ws_check && !$tsv_check && ($file !~ /((^|\/)Makefile\b|\.def$)/);
            $ctlkw_check = $tabs_check && $clike;
            $conflict_fail = defined($cfg{conflict});
            $braces = 0;
            $check_gen = 0;
            next;
        }
        if ($maybe_bin && /^Binary files /) {
            if ($new_file) {
                if (!defined($cfg{generated}) && ($file =~ /\.(obj|o|lib|a|dll|so|exe|exp|qm|pdb|idb|suo)$/i || isExe($maybe_bin))) {
                    &complain("Adding build artifact", "generated");
                }
            } else {
                if (!defined($cfg{giant}) && $size > (2 << 20)) {
                    &complain("Changing huge binary file (".formatSize($size)." > 2MiB)", "giant", 1);
                }
            }
            next;
        }
        if ($_ eq "new file mode 160000\n") {
            $is_submodule = 1;
            next;
        }
        if (!$is_submodule && /^index ([\w,]+)\.\.(\w+)(?! 160000)( |$)/) {
            my ($old_trees, $new_tree) = ($1, $2);
            #print "*** got index $old_trees $new_tree.\n";
            $size = `git cat-file -s $new_tree`;
            my $issrc = $clike || ($file =~ /\.(s|asm|pas|l|m4|bat|cmd|sh|py|qdoc(conf)?)$/i);
            if ($old_trees =~ /^0{40}(,0{40})*$/) {
            #print "*** adding ".$file.".\n";
                if (!$conflict_fail && $file =~ /\.(BACKUP|BASE|LOCAL|REMOTE)\.[^\/]+$/) {
                    &complain("Adding temporary file from merge conflict resolution", "conflict", 1);
                    $conflict_fail = 1;
                }
                if (!defined($cfg{alien}) && $file =~ /(\.(sln|vcproj|vcxproj|pro\.user)|(^|\/)(Makefile\.am|CMakeLists\.txt))$/i) {
                    &complain("Warning: Adding alien build system file", "alien");
                }
                if ($size > (2 << 20)) {
                    if (!defined($cfg{giant})) {
                        &complain("Adding huge file (".formatSize($size)." > 2MiB)", "giant", 1);
                    }
                } elsif ($size > 50000 && !$issrc && !defined($cfg{size})) {
                    &complain("Warning: Adding big file (".formatSize($size)." > 50kB)", "size");
                }
                $size = 0;
                $new_file = 1;
            } elsif ($size > 20000 && !$issrc && !defined($cfg{size})) {
                my $old_size = 0;
                for my $old_tree (split(/,/, $old_trees)) {
                    my $osz = `git cat-file -s $old_tree`;
                    $old_size = $osz if ($osz > $old_size);
                }
                if ($size > $old_size * 3 / 2) {
                    &complain("Warning: Increasing file size by more than 50% (".
                                formatSize($old_size)." => ".formatSize($size).")", "size");
                }
            }
            $maybe_bin = $new_tree;
            next;
        }
    }
}
close DIFF;
printerr;
flushFile();
if ($mixws_check) {
    flushChunk() if ($chunk);
    if ($nonws and $ws) {
        $file = "";
        &complain("Mixing whitespace-only changes with other changes", "mixws", -1);
        for my $fn (@ws_files) {
            print $lpfx."  WS-only in ".$fn.": ".join(", ", @{$ws_lines{$fn}})."\n";
        }
    }
}

exit ($gerrit ? (!$fail ? 11 : (10 - $fail)) : $fail)