Jump to content

User:AnomieBOT/source/hooks/pre-commit-hook.sh

From Wikipedia, the free encyclopedia
#!/bin/bash

git stash --keep-index -u -q

# First, check pod syntax
stop=0
echo -e "\n== Checking POD syntax";
for f in `find . -name \*.p[lm] -exec grep -l '^=' {} + | sort`; do
    perl -MPod::Checker -we '
        package Foo;
        @ISA=qw/Pod::Checker/;

        $skipwarn = 0;
        my $checker = new Foo;
        my $res = $checker->parse_from_file($ARGV[0]);
        exit 1 if($checker->num_errors()>0 || $checker->num_warnings()>$skipwarn);

        use Data::Dumper;
        sub poderror {
                my $self=shift;
                my $m=$_[0]->{"-msg"};
                if($m eq "Unknown interior-sequence \x27A\x27" ||
                   $m=~/^\(section\) in \x27.*\x27 deprecated$/ ||
                   $m=~/^Non-ASCII character seen before =encoding in/ ||
                   $m eq "Deleting unknown formatting code A<>"
                ){
                    $skipwarn++;
                } else {
                    #warn Dumper(\@_);
                    $self->SUPER::poderror(@_);
                }
        }
    ' "$f" || stop=1

    perl -nwe '
        BEGIN {
            $inpod=0; $eq=0;
        }
        unless(/^=/){ $eq=0; next; }
        die "$ARGV: All POD commands must begin a new paragraph (line $.)\n" if $eq;
        $eq=1;
        if(/^=pod(\s|$)/){ $inpod=1; next; }
        if(/^=cut(\s|$)/){ $inpod=0; next; }
        die "$ARGV: All POD blocks should start with \x27=pod\x27 (line $.)\n" unless $inpod;
    ' "$f" || stop=1
done

[ $stop -eq 0 ] && echo "Syntax OK";

echo ""

perl -we '
        use lib "./";
        use AnomieBOT::API;
        AnomieBOT::API::load("tasks/SourceUploader.pm");
        use tasks::SourceUploader::Pod;
        $l=0;
        $ret=0;
        %out=();
        print "== Checking metadata ==\n";
        while(@ARGV){
            $file=shift;
            $l=length($file) if length($file)>$l;
            open X, "<:utf8", $file || die "Could not open $file: $!\n";
            {
                local $/=undef;
                $txt=<X>;
            }
            close X;

            $parser=tasks::SourceUploader::Pod->new;
            my $x="";
            $parser->output_string(\$x);
            $parser->parse_string_document($txt);
            $section=tasks::SourceUploader::determine_task_section($file, $parser->metadata);
            if($section =~ /^99 Invalid metadata/){
                $ret=1;
            } else {
                $txt=~/package (.+?);/;
                $botnum=$1->approved;
                $section.=" $botnum" if $botnum>0;
            }
            push @{$out{$section}}, $file;
        }
        $l+=2;
        foreach $s (sort keys %out){
            ($section=$s)=~s/^\d+ //;
            printf "%-${l}s %s\n", "$_: ", $section foreach (@{$out{$s}});
        }
        exit $ret;
' `find tasks -name \*.pm -exec grep -l '^=begin metadata' {} + | sort` || stop=1

git reset --hard -q
git stash pop --index -q

[ $stop -eq 0 ] || exit 1;

if git diff --cached --name-status | grep -q  'M	ChangeLog'; then
    true
else
    echo -e "\nPlease update the ChangeLog"
    exit 1
fi

exit 0