Merge branch 'wtf8' of git://github.com/bobtfish/Gitalist into action-blame
[catagits/Gitalist.git] / lib / Gitalist / Controller / Root.pm
CommitLineData
89de6a33 1package Gitalist::Controller::Root;
42fe5d11 2use Moose;
3use namespace::autoclean;
89de6a33 4
42fe5d11 5BEGIN { extends 'Catalyst::Controller' }
89de6a33 6
89de6a33 7__PACKAGE__->config->{namespace} = '';
8
f796a861 9use Sys::Hostname ();
d17ce39c 10use XML::Atom::Feed;
11use XML::Atom::Entry;
f796a861 12use XML::RSS;
d17ce39c 13
89de6a33 14=head1 NAME
15
16Gitalist::Controller::Root - Root Controller for Gitalist
17
18=head1 DESCRIPTION
19
20[enter your description here]
21
22=head1 METHODS
23
24=cut
25
832cbc81 26sub _get_object {
b4b4d0fd 27 my($self, $c, $haveh) = @_;
9dc3b9a5 28
c1f608c8 29 my $h = $haveh || $c->req->param('h') || '';
0ee97fec 30 my $f = $c->req->param('f');
8dbe8024 31
1aad4e81 32 my $m = $c->stash->{Project};
33 my $pd = $m->path;
0ee97fec 34
9dc3b9a5 35 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
a7cc1ede 36 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
37 || ($f && $m->hash_by_path($f))
38 || $m->head_hash
9dc3b9a5 39 # XXX This could definitely use more context.
40 || Carp::croak("Couldn't find a hash for the commit object!");
41
c046a52f 42 my $obj = $m->get_object($hash)
43 or Carp::croak("Couldn't find a object for '$hash' in '$pd'!");
9dc3b9a5 44
c046a52f 45 return $obj;
9dc3b9a5 46}
47
1625cd5f 48=head2 index
49
50Provides the project listing.
51
52=cut
53
86382b95 54sub index :Path :Args(0) {
7bc165b3 55 my ( $self, $c ) = @_;
56
57 $c->detach($c->req->param('a'))
58 if $c->req->param('a');
59
60 my @list = @{ $c->model()->projects };
61 die 'No projects found in '. $c->model->repo_dir
62 unless @list;
63
64 my $search = $c->req->param('s') || '';
65 if($search) {
66 @list = grep {
67 index($_->name, $search) > -1
68 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
69 } @list
70 }
71
72 $c->stash(
73 search_text => $search,
74 projects => \@list,
75 action => 'index',
76 );
04d1d917 77}
78
38a4951e 79sub project_index : Local {
80 my ( $self, $c ) = @_;
81
82 my @list = @{ $c->model()->projects };
83 die 'No projects found in '. $c->model->repo_dir
84 unless @list;
85
86 $c->response->content_type('text/plain');
87 $c->response->body(
88 join "\n", map $_->name, @list
89 );
90 $c->response->status(200);
91}
92
790ce598 93=head2 summary
94
95A summary of what's happening in the repo.
96
97=cut
98
99sub summary : Local {
100 my ( $self, $c ) = @_;
b47ae2c0 101 my $project = $c->stash->{Project};
3bbb1202 102 $c->detach('error_404') unless $project;
832cbc81 103 my $commit = $self->_get_object($c);
c65cccc2 104 my @heads = @{$project->heads};
06281e11 105 my $maxitems = Gitalist->config->{paging}{summary} || 10;
790ce598 106 $c->stash(
107 commit => $commit,
4111e151 108 log_lines => [$project->list_revs(
109 sha1 => $commit->sha1,
06281e11 110 count => $maxitems,
fde5091f 111 )],
4111e151 112 refs => $project->references,
0969b411 113 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
790ce598 114 action => 'summary',
06281e11 115 );
790ce598 116}
117
118=head2 heads
119
120The current list of heads (aka branches) in the repo.
121
122=cut
123
124sub heads : Local {
125 my ( $self, $c ) = @_;
b47ae2c0 126 my $project = $c->stash->{Project};
790ce598 127 $c->stash(
832cbc81 128 commit => $self->_get_object($c),
c65cccc2 129 heads => $project->heads,
790ce598 130 action => 'heads',
131 );
132}
133
ea19a20c 134=head2 tags
135
136The current list of tags in the repo.
137
138=cut
139
140sub tags : Local {
141 my ( $self, $c ) = @_;
142 my $project = $c->stash->{Project};
143 $c->stash(
144 commit => $self->_get_object($c),
145 tags => $project->tags,
146 action => 'tags',
147 );
148}
149
18a8059a 150sub blame : Local {
151 my($self, $c) = @_;
152
153 my $project = $c->stash->{Project};
154 my $h = $c->req->param('h')
155 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
156 || die "No file or sha1 provided.";
157 my $hb = $c->req->param('hb')
158 || $project->head_hash
159 || die "Couldn't discern the corresponding head.";
160 my $filename = $c->req->param('f') || '';
161
18a8059a 162 $c->stash(
6d4fe0d7 163 blame => $project->get_object($hb)->blame($filename),
18a8059a 164 head => $project->get_object($hb),
165 filename => $filename,
166 );
167
168}
169
b5f3d3e7 170sub _blob_objs {
295c9703 171 my ( $self, $c ) = @_;
8bb7649b 172 my $project = $c->stash->{Project};
c8870bd3 173 my $h = $c->req->param('h')
8bb7649b 174 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
c8870bd3 175 || die "No file or sha1 provided.";
176 my $hb = $c->req->param('hb')
8bb7649b 177 || $project->head_hash
c8870bd3 178 || die "Couldn't discern the corresponding head.";
179
f5da8e7a 180 my $filename = $c->req->param('f') || '';
181
b5f3d3e7 182 my $blob = $project->get_object($h);
183 $blob = $project->get_object(
184 $project->hash_by_path($h || $hb, $filename)
185 ) if $blob->type ne 'blob';
186
187 return $blob, $project->get_object($hb), $filename;
188}
189
190=head2 blob
191
192The blob action i.e the contents of a file.
193
194=cut
195
196sub blob : Local {
197 my ( $self, $c ) = @_;
198
199 my($blob, $head, $filename) = $self->_blob_objs($c);
295c9703 200 $c->stash(
b5f3d3e7 201 blob => $blob->content,
202 head => $head,
f5da8e7a 203 filename => $filename,
204 # XXX Hack hack hack, see View::SyntaxHighlight
205 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
c8870bd3 206 action => 'blob',
295c9703 207 );
7e54e579 208
c8a42dd5 209 $c->forward('View::SyntaxHighlight')
210 unless $c->stash->{no_wrapper};
211}
212
b5f3d3e7 213=head2 blob_plain
214
215The plain text version of blob, where file is rendered as is.
216
217=cut
218
c8a42dd5 219sub blob_plain : Local {
220 my($self, $c) = @_;
221
b5f3d3e7 222 my($blob) = $self->_blob_objs($c);
c8a42dd5 223 $c->response->content_type('text/plain; charset=utf-8');
b5f3d3e7 224 $c->response->body($blob->content);
225 $c->response->status(200);
c8a42dd5 226}
227
b5f3d3e7 228=head2 blobdiff_plain
229
230The plain text version of blobdiff.
231
232=cut
233
c8a42dd5 234sub blobdiff_plain : Local {
235 my($self, $c) = @_;
236
237 $c->stash(no_wrapper => 1);
238 $c->response->content_type('text/plain; charset=utf-8');
239
240 $c->forward('blobdiff');
295c9703 241}
242
6cf4366a 243=head2 blobdiff
244
245Exposes a given diff of a blob.
246
247=cut
248
249sub blobdiff : Local {
250 my ( $self, $c ) = @_;
832cbc81 251 my $commit = $self->_get_object($c, $c->req->param('hb'));
6cf4366a 252 my $filename = $c->req->param('f')
253 || croak("No file specified!");
6cfcd548 254 my($tree, $patch) = $c->stash->{Project}->diff(
ad8884fc 255 commit => $commit,
ad8884fc 256 patch => 1,
c8a42dd5 257 parent => $c->req->param('hpb') || undef,
258 file => $filename,
6cf4366a 259 );
260 $c->stash(
261 commit => $commit,
ad8884fc 262 diff => $patch,
592b68ef 263 filename => $filename,
6cf4366a 264 # XXX Hack hack hack, see View::SyntaxHighlight
ad8884fc 265 blobs => [$patch->[0]->{diff}],
6cf4366a 266 language => 'Diff',
267 action => 'blobdiff',
268 );
269
c8a42dd5 270 $c->forward('View::SyntaxHighlight')
271 unless $c->stash->{no_wrapper};
6cf4366a 272}
273
1625cd5f 274=head2 commit
275
47495599 276Exposes a given commit.
1625cd5f 277
278=cut
279
1feb3d6b 280sub commit : Local {
d7c9a32f 281 my ( $self, $c ) = @_;
77edf882 282 my $project = $c->stash->{Project};
832cbc81 283 my $commit = $self->_get_object($c);
b7aca93a 284 $c->stash(
790ce598 285 commit => $commit,
77edf882 286 diff_tree => ($project->diff(commit => $commit))[0],
287 refs => $project->references,
790ce598 288 action => 'commit',
b7aca93a 289 );
d7c9a32f 290}
291
2247133f 292=head2 commitdiff
293
294Exposes a given diff of a commit.
295
296=cut
297
298sub commitdiff : Local {
299 my ( $self, $c ) = @_;
832cbc81 300 my $commit = $self->_get_object($c);
fc948aee 301 my($tree, $patch) = $c->stash->{Project}->diff(
ad8884fc 302 commit => $commit,
fc948aee 303 parent => $c->req->param('hp') || undef,
ad8884fc 304 patch => 1,
305 );
2247133f 306 $c->stash(
f5da8e7a 307 commit => $commit,
ad8884fc 308 diff_tree => $tree,
309 diff => $patch,
f5da8e7a 310 # XXX Hack hack hack, see View::SyntaxHighlight
ad8884fc 311 blobs => [map $_->{diff}, @$patch],
f5da8e7a 312 language => 'Diff',
313 action => 'commitdiff',
2247133f 314 );
f5da8e7a 315
c8a42dd5 316 $c->forward('View::SyntaxHighlight')
317 unless $c->stash->{no_wrapper};
318}
319
320sub commitdiff_plain : Local {
321 my($self, $c) = @_;
322
323 $c->stash(no_wrapper => 1);
324 $c->response->content_type('text/plain; charset=utf-8');
325
326 $c->forward('commitdiff');
2247133f 327}
328
47495599 329=head2 shortlog
330
331Expose an abbreviated log of a given sha1.
332
333=cut
334
335sub shortlog : Local {
336 my ( $self, $c ) = @_;
592b68ef 337
338 my $project = $c->stash->{Project};
c046a52f 339 my $commit = $self->_get_object($c, $c->req->param('hb'));
592b68ef 340 my $filename = $c->req->param('f') || '';
341
fde5091f 342 my %logargs = (
343 sha1 => $commit->sha1,
f740f8a9 344 count => Gitalist->config->{paging}{log} || 25,
592b68ef 345 ($filename ? (file => $filename) : ())
63e220b9 346 );
fde5091f 347
348 my $page = $c->req->param('pg') || 0;
349 $logargs{skip} = $c->req->param('pg') * $logargs{count}
350 if $c->req->param('pg');
351
47495599 352 $c->stash(
790ce598 353 commit => $commit,
f740f8a9 354 log_lines => [$project->list_revs(%logargs)],
355 refs => $project->references,
b4b4d0fd 356 page => $page,
592b68ef 357 filename => $filename,
358 action => 'shortlog',
47495599 359 );
360}
361
790ce598 362=head2 log
363
364Calls shortlog internally. Perhaps that should be reversed ...
365
366=cut
367sub log : Local {
368 $_[0]->shortlog($_[1]);
369 $_[1]->stash->{action} = 'log';
370}
371
c8a42dd5 372# For legacy support.
373sub history : Local {
c046a52f 374 $_[1]->forward('shortlog');
c8a42dd5 375}
376
b3fa97cd 377=head2 tree
378
379The tree of a given commit.
380
381=cut
382
383sub tree : Local {
384 my ( $self, $c ) = @_;
74af77a3 385 my $project = $c->stash->{Project};
832cbc81 386 my $commit = $self->_get_object($c, $c->req->param('hb'));
c046a52f 387 my $filename = $c->req->param('f') || '';
388 my $tree = $filename
389 ? $project->get_object($project->hash_by_path($commit->sha1, $filename))
390 : $project->get_object($commit->tree_sha1)
391 ;
b3fa97cd 392 $c->stash(
790ce598 393 commit => $commit,
b4b4d0fd 394 tree => $tree,
74af77a3 395 tree_list => [$project->list_tree($tree->sha1)],
832cbc81 396 path => $c->req->param('f') || '',
790ce598 397 action => 'tree',
b3fa97cd 398 );
399}
400
9dc3b9a5 401=head2 reflog
402
403Expose the local reflog. This may go away.
404
405=cut
406
407sub reflog : Local {
408 my ( $self, $c ) = @_;
d8abdf1c 409 my @log = $c->stash->{Project}->reflog(
9dc3b9a5 410 '--since=yesterday'
411 );
412
413 $c->stash(
414 log => \@log,
415 action => 'reflog',
416 );
417}
418
ea19a20c 419=head2 search
420
421The action for the search form.
422
423=cut
424
14664e1c 425sub search : Local {
4df2f62f 426 my($self, $c) = @_;
d8abdf1c 427 $c->stash(current_action => 'GitRepos');
428 my $project = $c->stash->{Project};
832cbc81 429 my $commit = $self->_get_object($c);
4df2f62f 430 # Lifted from /shortlog.
431 my %logargs = (
432 sha1 => $commit->sha1,
433 count => Gitalist->config->{paging}{log},
434 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
72d72d4d 435 search => {
436 type => $c->req->param('type'),
437 text => $c->req->param('text'),
438 regexp => $c->req->param('regexp') || 0,
439 },
4df2f62f 440 );
441
442 $c->stash(
443 commit => $commit,
d8abdf1c 444 results => [$project->list_revs(%logargs)],
4df2f62f 445 action => 'search',
446 # This could be added - page => $page,
447 );
14664e1c 448}
449
ea19a20c 450=head2 search_help
451
452Provides some help for the search form.
453
454=cut
455
14664e1c 456sub search_help : Local {
2646511e 457 my ($self, $c) = @_;
458 $c->stash(template => 'search_help.tt2');
6cfcd548 459}
460
ea19a20c 461=head2 atom
462
463Provides an atom feed for a given project.
464
465=cut
466
6cfcd548 467sub atom : Local {
d17ce39c 468 my($self, $c) = @_;
6cfcd548 469
d17ce39c 470 my $feed = XML::Atom::Feed->new;
6cfcd548 471
d17ce39c 472 my $host = lc Sys::Hostname::hostname();
473 $feed->title($host . ' - ' . Gitalist->config->{name});
474 $feed->updated(~~DateTime->now);
475
476 my $project = $c->stash->{Project};
477 my %logargs = (
478 sha1 => $project->head_hash,
479 count => Gitalist->config->{paging}{log} || 25,
480 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
481 );
482
483 my $mk_title = $c->stash->{short_cmt};
484 for my $commit ($project->list_revs(%logargs)) {
485 my $entry = XML::Atom::Entry->new;
486 $entry->title( $mk_title->($commit->comment) );
487 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
488 # XXX Needs work ...
489 $entry->content($commit->comment);
490 $feed->add_entry($entry);
491 }
492
f796a861 493 $c->response->body($feed->as_xml);
e75df318 494 $c->response->content_type('application/atom+xml');
f796a861 495 $c->response->status(200);
6cfcd548 496}
497
ea19a20c 498=head2 rss
499
500Provides an RSS feed for a given project.
501
502=cut
503
6cfcd548 504sub rss : Local {
f796a861 505 my ($self, $c) = @_;
506
507 my $project = $c->stash->{Project};
508
509 my $rss = XML::RSS->new(version => '2.0');
510 $rss->channel(
511 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
512 link => $c->uri_for('summary', {p=>$project->name}),
513 language => 'en',
514 description => $project->description,
515 pubDate => DateTime->now,
516 lastBuildDate => DateTime->now,
517 );
518
519 my %logargs = (
520 sha1 => $project->head_hash,
521 count => Gitalist->config->{paging}{log} || 25,
522 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
523 );
524 my $mk_title = $c->stash->{short_cmt};
525 for my $commit ($project->list_revs(%logargs)) {
526 # XXX Needs work ....
527 $rss->add_item(
528 title => $mk_title->($commit->comment),
529 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
530 description => $commit->comment,
531 );
532 }
533
534 $c->response->body($rss->as_string);
535 $c->response->content_type('application/rss+xml');
536 $c->response->status(200);
14664e1c 537}
538
ea19a20c 539=head2 patch
540
541A raw patch for a given commit.
542
543=cut
544
6cfcd548 545sub patch : Local {
377bf360 546 my ($self, $c) = @_;
61ba8635 547 $c->detach('patches', [1]);
548}
549
ea19a20c 550=head2 patches
551
552The patcheset for a given commit ???
553
554=cut
555
61ba8635 556sub patches : Local {
557 my ($self, $c, $count) = @_;
558 $count ||= Gitalist->config->{patches}{max};
377bf360 559 my $commit = $self->_get_object($c);
560 my $parent = $c->req->param('hp') || undef;
f707d264 561 my $patch = $commit->get_patch( $parent, $count );
377bf360 562 $c->response->body($patch);
563 $c->response->content_type('text/plain');
564 $c->response->status(200);
6cfcd548 565}
566
ea19a20c 567=head2 snapshot
568
569Provides a snapshot of a given commit.
570
571=cut
572
6cfcd548 573sub snapshot : Local {
30db8f5b 574 my ($self, $c) = @_;
63afe2db 575 my $format = $c->req->param('sf') || 'tgz';
30db8f5b 576 die unless $format;
2e79039a 577 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
c0dbe239 578 my @snap = $c->stash->{Project}->snapshot(
579 sha1 => $sha1,
580 format => $format
581 );
30db8f5b 582 $c->response->status(200);
583 $c->response->headers->header( 'Content-Disposition' =>
c0dbe239 584 "attachment; filename=$snap[0]");
585 $c->response->body($snap[1]);
6cfcd548 586}
587
1625cd5f 588=head2 auto
589
590Populate the header and footer. Perhaps not the best location.
591
592=cut
593
04d1d917 594sub auto : Private {
4621ecf0 595 my($self, $c) = @_;
04d1d917 596
da8f4f82 597 my $project = $c->req->param('p');
598 if (defined $project) {
599 eval {
600 $c->stash(Project => $c->model('GitRepos')->project($project));
601 };
602 if ($@) {
603 $c->detach('error_404');
604 }
605 }
606
607 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
4621ecf0 608 $c->stash(
da8f4f82 609 git_version => $a_project->run_cmd('--version'),
610 version => $Gitalist::VERSION,
611
612 # XXX Move these to a plugin!
4621ecf0 613 time_since => sub {
ef11b09e 614 return 'never' unless $_[0];
4621ecf0 615 return age_string(time - $_[0]->epoch);
616 },
617 short_cmt => sub {
618 my $cmt = shift;
619 my($line) = split /\n/, $cmt;
741e110e 620 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
4621ecf0 621 return $line;
622 },
5cd9b9f9 623 abridged_description => sub {
624 join(' ', grep { defined } (split / /, shift)[0..10]);
625 },
4621ecf0 626 );
04d1d917 627}
d9a9b56b 628
77730637 629sub opml : Local {
630 # FIXME - implement snapshot
631 Carp::croak "Not implemented.";
632}
77730637 633
89de6a33 634=head2 end
635
636Attempt to render a view, if needed.
637
638=cut
639
fde5091f 640sub end : ActionClass('RenderView') {
1aad4e81 641 my ($self, $c) = @_;
642 # Give project views the current HEAD.
643 if ($c->stash->{Project}) {
644 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
645 }
1fd8159c 646}
d9a9b56b 647
4111e151 648sub error_404 :Private {
649 my ($self, $c) = @_;
650 $c->response->status(404);
651 $c->stash(
652 title => 'Page not found',
653 content => 'Page not found',
1aad4e81 654 );
4111e151 655}
656
1fd8159c 657sub age_string {
da8f4f82 658 my $age = shift;
659 my $age_str;
660
661 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
662 $age_str = ( int $age / 60 / 60 / 24 / 365 );
663 $age_str .= " years ago";
664 }
665 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
666 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
667 $age_str .= " months ago";
668 }
669 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
670 $age_str = int $age / 60 / 60 / 24 / 7;
671 $age_str .= " weeks ago";
672 }
673 elsif ( $age > 60 * 60 * 24 * 2 ) {
674 $age_str = int $age / 60 / 60 / 24;
675 $age_str .= " days ago";
676 }
677 elsif ( $age > 60 * 60 * 2 ) {
678 $age_str = int $age / 60 / 60;
679 $age_str .= " hours ago";
680 }
681 elsif ( $age > 60 * 2 ) {
682 $age_str = int $age / 60;
683 $age_str .= " min ago";
684 }
685 elsif ( $age > 2 ) {
686 $age_str = int $age;
687 $age_str .= " sec ago";
688 }
689 else {
690 $age_str .= " right now";
691 }
692 return $age_str;
7a88ffa4 693}
694
da8f4f82 695
89de6a33 696=head1 AUTHOR
697
f71a83ab 698Dan Brook
89de6a33 699
700=head1 LICENSE
701
702This library is free software. You can redistribute it and/or modify
703it under the same terms as Perl itself.
704
705=cut
706
42fe5d11 707__PACKAGE__->meta->make_immutable;