summaryrefslogtreecommitdiffstats
path: root/lib/Crawl/Bot/Plugin/Commit.pm
blob: 725a2c2206e4f128a00e9395b2c3043f65beb27c (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
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
package Crawl::Bot::Plugin::Commit;
use Moose;
use autodie;
use File::pushd;
extends 'Crawl::Bot::Plugin';

has repo_uri => (
    is      => 'ro',
    isa     => 'Str',
    default => 'git://gitorious.org/crawl/crawl.git',
);

has announce_commits => (
    is      => 'rw',
    isa     => 'Bool',
    default => 0,
);

has checkout => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => sub {
        my $self = shift;
        my $checkout = File::Spec->catdir($self->data_dir, 'crawl-ref');
        mkdir $checkout unless -d $checkout;
        my $dir = pushd($checkout);
        if (!-f 'HEAD') {
            system('git clone --mirror ' . $self->repo_uri . " $checkout");
        }
        $checkout;
    },
);

has heads => (
    traits  => [qw(Hash)],
    isa     => 'HashRef[Str]',
    lazy    => 1,
    handles => {
        has_branch => 'exists',
        head       => 'accessor',
    },
    default => sub {
        my $self = shift;
        my $dir = pushd($self->checkout);
        return { map { $_ => `git rev-parse $_` } $self->branches };
    },
);

sub said {
    my $self = shift;
    my ($args) = @_;

    my @keys = (who => $args->{who}, channel => $args->{channel}, "body");

    if ($args->{body} =~ /^%git(?:\s+(.*))?$/) {
        my $rev = $1;
        $rev = "HEAD" unless $rev;
        my $commit = $self->parse_commit($rev);
        if (defined $commit) {
            my $abbr = substr($commit->{hash}, 0, 12);
            my $pl = ($commit->{nfiles} == 1 ? "" : "s");

            my $rev = $commit->{revname} || "r$abbr";

            $self->say(@keys, "$commit->{author} * $rev: $commit->{subject} "
                . "($commit->{date}, $commit->{nfiles} file$pl, "
                . "$commit->{nins}+ $commit->{ndel}-) "
                . "https://gitorious.org/crawl/crawl/commit/$abbr"
            );
        } else {
            my $ev = $? >> 8;
            $self->say(@keys, "Could not find commit $rev (git returned $ev)");
        }
    }
}

sub tick {
    my $self = shift;
    my $dir = pushd($self->checkout);
    system('git fetch');
    for my $branch ($self->branches) {
        my $old_head = $self->head($branch) || '';
        my $head = `git rev-parse $branch`;
        chomp ($old_head, $head);
        next if $old_head eq $head;

        # Exclude merges from master into other branches.
        my $exclude_master = $branch eq "master" ? "" : "^master";
        my @revs = split /\n/, `git rev-list $head ^$old_head $exclude_master`;

        if (!$self->has_branch($branch)) {
            my $nrev = scalar @revs;
            my $pl = $nrev == 1 ? "" : "s";
            $self->say_all("New branch created: $branch ($nrev commit$pl)");
        }

        if ($self->announce_commits) {
                my %commits = map { $_, $self->parse_commit($_) } @revs;

                my $cherry_picks = @revs;
                @revs = grep { $commits{$_}->{subject} !~ /\(cherry picked from /
                        && $commits{$_}->{body}    !~ /\(cherry picked from / } @revs;
                $cherry_picks -= @revs;
                my $pl = $cherry_picks == 1 ? "" : "s";
                $self->say_all("Cherry-picked $cherry_picks commit$pl into $branch")
                if $cherry_picks > 0;

                for my $rev (@revs) {
                        my $commit = $commits{$rev};
                        my $br = $branch eq "master" ? "" : "[$branch] ";

                        my $abbr = substr($commit->{hash}, 0, 12);
                        my $pl = ($commit->{nfiles} == 1 ? "" : "s");

                        my $revname = $commit->{revname} || "r$abbr";

                        $self->say_all("$commit->{author} $br* $revname: $commit->{subject} "
                                . "($commit->{date}, $commit->{nfiles} file$pl, "
                                . "$commit->{nins}+ $commit->{ndel}-) "
                                . "https://gitorious.org/crawl/crawl/commit/$abbr"
                        );
                }
        }

        $self->head($branch => $head);
    }
}

sub branches {
    my $self = shift;
    my $dir = pushd($self->checkout);
    return map { s/^[ \*]*//; $_ } split /\n/, `git branch`;
}

sub parse_commit {
    my $self = shift;
    my ($rev) = @_;
    my $dir = pushd($self->checkout);

    CORE::open(F, "-|:encoding(UTF-8)", qw(git log -1 --shortstat --pretty=format:%H%x00%aN%x00%s%x00%b%x00%ar%x00), $rev) or return undef;
    local $/ = undef;
    my $info = <F>;
    CORE::close(F) or return undef;

    my $revname;

    if (CORE::open(F, "-|:encoding(UTF-8)", qw(git describe), $rev)) {
        $revname = <F>;
        $revname =~ s/\n.*//;
        CORE::close(F);
    }

    $info =~ /(.*?)\x00(.*?)\x00(.*?)\x00(.*?)\x00(.*?)\x00(.*)/s or return undef;
    my ($hash, $author, $subject, $body, $date, $stat) = ($1, $2, $3, $4, $5, $6);

    my ($nfiles, $nins, $ndel);
    ($stat =~ /(\d+) files changed/) and $nfiles = $1;
    ($stat =~ /(\d+) insertions/) and $nins = $1;
    ($stat =~ /(\d+) deletions/) and $ndel = $1;
    return {
        hash    => $hash,
        author  => $author,
        subject => $subject,
        body    => $body,
        date    => $date,
        nfiles  => $nfiles,
        nins    => $nins,
        ndel    => $ndel,
        revname => $revname,
    };
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;