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