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