coding-standard-check.pl 3.63 KB
Newer Older
1 2 3 4
#!/usr/bin/perl
# This is slightly modified from Andrew Morton's Perfect Patch.
# Lines you introduce should not have trailing whitespace.
# Also check for an indentation that has SP before a TAB.
5 6
use File::Basename;

7 8 9 10
my $found_bad = 0;
my $filename;
my $reported_filename = "";
my $lineno;
11 12
my $stack;
my $stackage;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
sub bad_line {
    my ($why, $line) = @_;

    if (!$found_bad) {
        print STDERR "*\n";
        print STDERR "* You have some suspicious patch lines:\n";
        print STDERR "*\n";
        $found_bad = 1;
    }
    if ($reported_filename ne $filename) {
        print STDERR "* In $filename\n";
        $reported_filename = $filename;
    }

    print STDERR "* $why (line $lineno)\n";
    print STDERR "$filename:$lineno:$line\n";
}

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
my @readme_filenames = ("README.Mahara", "README.mahara");
sub has_readme_mahara {
    my $path = shift;
    # Look in all parent directories in case this is part of a large library
    while ($path ne ".") {
        $path = dirname($path);
        foreach $filename (@readme_filenames) {
            if (-e "$path/$filename") {
                return 1;
            }
        }
    }
    return 0;
}

my $skipfile = 0;
47 48
while (<>) {
    if (m|^diff --git a/(.*) b/\1$|) {
49
        $skipfile = has_readme_mahara($1);
50 51 52
        $filename = $1;
        next;
    }
53 54 55 56
    if ($skipfile) {
        next;
    }

57 58 59 60 61 62 63 64 65 66 67
    if (/^@@ -\S+ \+(\d+)/) {
        $lineno = $1 - 1;
        next;
    }
    if (/^ /) {
        $lineno++;
        next;
    }
    if (s/^\+//) {
        $lineno++;
        chomp;
68 69 70
        if (/^([<>])\1{6} |^={7}$/) {
            bad_line("unresolved merge conflict", $_);
        }
71 72 73 74 75 76 77 78 79
        if ($filename !~ /\.(php|js)$/) {
            next;
        }
        if (/\s$/) {
            bad_line("trailing whitespace", $_);
        }
        if (/\t+/) {
            bad_line("TABs should be replaced by 4 spaces.", $_);
        }
Brett Wilkins's avatar
Brett Wilkins committed
80 81
        if (/\}\s*(else|catch)/) {
            bad_line("cuddled elses/catches are against Mahara coding guidelines", $_);
82
        }
83
        if (/\belseif\b/) {
84 85
            bad_line("a single space is requred between an else and an if on the same line", $_);
        }
86
        if (/\b(if|while|for)\(/ || /\b(if|while|for)\s\s+\(/) {
87 88 89
            bad_line("conditional and looping statements should have a space between keywords ".
                     "and the condition brackets", $_);
        }
90 91 92 93
        if (/\b(if|while|for) \(.+\)(\s{2,})?{$/) {
            bad_line("if/while/for constructs should have a space between the closing parenthesis ".
                     "of the expression and the opening brace of the following block", $_);
        }
94 95 96 97 98 99
        if (/\)$/) {#Note no trailing semicolon
            $stack = $_;
        }
        if (/^\s*{/ && $stack ne "") {
            bad_line("opening curly braces do not need their own line.", "$stack\n$_");
            $stack = "";
100 101 102 103 104 105
        }
        if (/require_once\s+\(?/) {
            bad_line("a require_once statement should look like a function call, ".
                     "without a space between the keyword and the bracket.", $_);
        }
    }
106 107 108 109 110 111 112 113 114 115

    #Implement aging of the stack to prevent
    #erroneous matches of seccond condition
    if ($stackage > 0) {
        $stackage = 0;
        $stack = "";
    }
    if ($stack ne "") {
        $stackage++;
    }
116 117
}

118 119 120 121 122 123 124 125
if ($found_bad) {
    print STDERR "*\n";
    print STDERR "* If you are adding a third-party library please also add a README.Mahara which includes:\n";
    print STDERR "* - Version: the version of the plugin\n";
    print STDERR "* - Website: the url of the website/repository to get code/updates from\n";
    print STDERR "* - any changes you have made or 'none'\n";
}

126
exit($found_bad);