summaryrefslogtreecommitdiffstats
path: root/lib/Crawl/Bot/Plugin/Commit.pm
blob: 9344ddecb9f2762629fe648e25b276645d92d58b (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
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
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 => 1,
);

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

# Maximum number of commits to announce from a large batch.  If there
# are exactly announce_limits + 1 new commits, all of them will be
# announced, to avoid a useless "and 1 more commit" message.
has announce_limit => (
    is      => 'rw',
    isa     => 'Int',
    default => 10,
);

has colour_announce => (
    is      => 'rw',
    isa     => 'Bool',
    default => 1,
);

has colour_query => (
    is      => 'rw',
    isa     => 'Bool',
    default => 1,
);

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 };
    },
);

my %colour_codes = (
    author => 3,
    author_query => 7,
    committer => 2,
    branch => 7,
    stats => 10,
    url => 13
);

sub make_commit_uri {
    my $self = shift;
    my $commit = shift;
    return "http://s-z.org/neil/git/?p=crawl.git;a=commitdiff;h=$commit";
}

sub make_branch_uri {
    my $self = shift;
    my $branch = shift;
    return "http://s-z.org/neil/git/?p=crawl.git;a=log;h=refs/heads/$branch";
}

sub colour {
    my $self = shift;
    my $context = shift;
    my $type = shift;

    if ($context eq "announce") {
        return "" unless $self->colour_announce;
    } elsif ($context eq "query") {
        return "" unless $self->colour_query;
    }

    if ($type eq "reset") {
        return "\017";
    } elsif ($type eq "colon" or $type eq "rev") {
        return "\002";
    } else {
        my $code = $colour_codes{$type};
        return defined($code) ? sprintf("\003%02d", $code) : "";
    }
}

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} || "$abbr";
            my $committer = "";
            if ($commit->{committer} ne $commit->{author}) {
                    $committer = " {" . $commit->{committer} . "}";
            }

            $self->say(@keys,
                sprintf(
                    "%s%s%s%s%s%s * %s%s%s:%s %s%s%s %s(%s, %s file%s, %s+ %s-)%s %s%s%s",
                    $self->colour(query => "author_query"), $commit->{author},
                    $self->colour(query => "reset"),
                    $self->colour(query => "committer"), $committer,
                    $self->colour(query => "reset"),
                    $self->colour(query => "rev"), $rev,
                    $self->colour(query => "colon"),
                    $self->colour(query => "reset"),
                    $self->colour(query => "subject"), $commit->{subject},
                    $self->colour(query => "reset"),
                    $self->colour(query => "stats"),
                    $commit->{date}, $commit->{nfiles}, $pl,
                    $commit->{nins}, $commit->{ndel},
                    $self->colour(query => "reset"),
                    $self->colour(query => "url"),
                    $self->make_commit_uri($abbr),
                    $self->colour(query => "reset"),
                )
            );
        } else {
            my $ev = $? >> 8;
            $self->say(@keys, "Could not find commit $rev (git returned $ev)");
        }
    } elsif ($args->{body} =~ /^\%branch\s+(.*)$/) {
        $self->say(@keys, "Branch $1: " . $self->make_branch_uri($1));
    }
}

sub tick {
    my $self = shift;
    my $dir = pushd($self->checkout);
    warn "fetch";
    system('git fetch');
    for my $branch ($self->branches) {
        my $old_head = $self->head($branch) || '';
        warn "rev-parse $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 $exclude_old = $old_head ? "^$old_head" : "";
        warn "rev-list $head $exclude_old $exclude_master";
        my @revs = split /\n/, `git rev-list $head $exclude_old $exclude_master`;
        warn "rev-list done";

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

        # Announce playable branches in ##crawl, all branches in ##crawl-dev.
        my $say;
        if ($branch =~ /^(?:master|stone_soup-.*)$/) {
            $say = sub {
                $self->say_all(@_)
            }
        } else {
            $say = sub {
                $self->say_main(@_)
            }
        }

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

                if ($self->abbrev_cherry_picks)
                {
                    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";

                    $say->("Cherry-picked $cherry_picks commit$pl into $branch")
                        if $cherry_picks > 0;
                }

                my $count = 0;
                for my $rev (reverse @revs) {
                        # If it's just one more than the announce limit, don't
                        # bother with the message and announce the last commit
                        # anyway.
                        if (++$count > $self->announce_limit and scalar @revs > $count) {
                            $say->("... and " . (scalar @revs - $count + 1) . " more commits");
                            last;
                        }
                        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} || "$abbr";
                        my $committer = "";
                        if ($commit->{committer} ne $commit->{author}) {
                                $committer = " {" . $commit->{committer} . "}";
                        }

                        $say->(
                            sprintf(
                                "%s%s%s%s%s%s %s%s%s* %s%s%s:%s %s%s%s %s(%s, %s file%s, %s+ %s-)%s %s%s%s",
                                $self->colour(announce => "author"), $commit->{author},
                                $self->colour(announce => "reset"),
                                $self->colour(announce => "committer"), $committer,
                                $self->colour(announce => "reset"),
                                $self->colour(announce => "branch"), $br,
                                $self->colour(announce => "reset"),
                                $self->colour(announce => "rev"), $revname,
                                $self->colour(announce => "colon"),
                                $self->colour(announce => "reset"),
                                $self->colour(announce => "subject"), $commit->{subject},
                                $self->colour(announce => "reset"),
                                $self->colour(announce => "stats"),
                                $commit->{date}, $commit->{nfiles}, $pl,
                                $commit->{nins}, $commit->{ndel},
                                $self->colour(announce => "reset"),
                                $self->colour(announce => "url"),
                                $self->make_commit_uri($abbr),
                                $self->colour(announce => "reset"),
                            )
                        );
                }
        }

        $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%cN%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(.*?)\x00(.*)/s or return undef;
    my ($hash, $author, $committer, $subject, $body, $date, $stat) = ($1, $2, $3, $4, $5, $6, $7);

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

__PACKAGE__->meta->make_immutable;
no Moose;

1;