summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/checkwhite
blob: 84779b9934b9ed5a6576212bdf90031e530b9284 (plain) (blame)
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
#!/usr/bin/perl -w
use Encode;
use Text::Tabs;
use Getopt::Std;
use strict;

our ($opt_n, $opt_t);
getopts('t:n'); # this removes -t and -n from @ARGV;
my $tabstop = $opt_t if ($opt_t);
my $dry_run = $opt_n;
my $any_bad = 0;

my @files = @ARGV;
unless (@files)
{
    open FLIST, "git ls-files|" or die "Can't run git ls-files";
    @files = <FLIST>;
    close FLIST;
}

for (@files)
{
    chomp;
    next if -d $_;
    -f $_ or (print(STDERR "Can't read $_\n"), next);
    next if /webserver\/static\/scripts\/contrib\//i;
    next if /\.(png|gif|ttf|ico|icns|fig|tex|eps|pdf)$/i;
    next if /\.(sln|vim|pbxproj|vsprops|plist|csproj|config|cs)$/i;
    next if /\.(vcproj|vcproj\.user|vcxproj|vcxproj\.filters)$/i;
    next if m[(^|/)\.git(modules|attributes)$];
    next if /\.(lex|tab)\./;
    next if !/\./;
    my $tab = /\.(rb|pl|sh)$/;  # Allow tabs for these files.
    $tab = 1 if /Makefile/i;    # ... and for makefiles.
    my $bom = /\.js$/;          # And BOM for these.
    $bom = 1 if /CREDITS/;
    undef local $/;
    open F, "<$_" or die "Can't open $_";
    my $file = $_;
    my $cont=$_=<F>;
    close F;

    eval{decode("UTF-8", "$_", Encode::FB_CROAK)};
    if ($@)
    {
        print "invalid UTF-8: $file\n";
        # We don't know the actual encoding, assume a Windows-using american/
        # frenchman/german/finn.  Sorry, polacks and russkies.
        Encode::from_to($_, "CP1252", "UTF-8");
    }
    $_.="\n", print "missing newline at eof: $file\n" unless /\n$/s or /^$/;
    print "extra newlines at eof: $file\n" if s/\n+\n$/\n/s;
    $_=expand $_, print "tab: $file\n" if !$tab && /\t/;
    print "spaces at eol: $file\n" if s/ +\n/\n/sg;
    print "CR: $file\n" if s/\r//sg;
    # Note: it's a byte string, as we had to handle invalid encodings above,
    # and $cont may be invalid.
    print "zero width space: $file\n" if s/\xe2\x80\x8b//sg; # U+200B
    print "BOM: $file\n" if !$bom && s/\xef\xbb\xbf//sg; # U+FFEF
    print "Control statement space: $file\n" if $file =~ /\.(cc|h|js)$/i
        && s/\b(if|while|for|switch|catch)\(/$1 (/sg;

    if ($_ ne $cont)
    {
        $any_bad = 1;
        if (!$dry_run)
        {
            open F, ">$file" or die;
            print F;
            close F;
        }
    }
}

exit 1 if ($dry_run and $any_bad);