Fix the path in the /blob & /blame header.
[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
a7cc1ede 42 my $commit = $m->get_object($hash)
43 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
9dc3b9a5 44
45 return $commit;
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};
339 my $commit = $self->_get_object($c);
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 {
374 $_[0]->shortlog(@_[1 .. $#_]);
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'));
387 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
b3fa97cd 388 $c->stash(
790ce598 389 commit => $commit,
b4b4d0fd 390 tree => $tree,
74af77a3 391 tree_list => [$project->list_tree($tree->sha1)],
832cbc81 392 path => $c->req->param('f') || '',
790ce598 393 action => 'tree',
b3fa97cd 394 );
395}
396
9dc3b9a5 397=head2 reflog
398
399Expose the local reflog. This may go away.
400
401=cut
402
403sub reflog : Local {
404 my ( $self, $c ) = @_;
d8abdf1c 405 my @log = $c->stash->{Project}->reflog(
9dc3b9a5 406 '--since=yesterday'
407 );
408
409 $c->stash(
410 log => \@log,
411 action => 'reflog',
412 );
413}
414
ea19a20c 415=head2 search
416
417The action for the search form.
418
419=cut
420
14664e1c 421sub search : Local {
4df2f62f 422 my($self, $c) = @_;
d8abdf1c 423 $c->stash(current_action => 'GitRepos');
424 my $project = $c->stash->{Project};
832cbc81 425 my $commit = $self->_get_object($c);
4df2f62f 426 # Lifted from /shortlog.
427 my %logargs = (
428 sha1 => $commit->sha1,
429 count => Gitalist->config->{paging}{log},
430 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
72d72d4d 431 search => {
432 type => $c->req->param('type'),
433 text => $c->req->param('text'),
434 regexp => $c->req->param('regexp') || 0,
435 },
4df2f62f 436 );
437
438 $c->stash(
439 commit => $commit,
d8abdf1c 440 results => [$project->list_revs(%logargs)],
4df2f62f 441 action => 'search',
442 # This could be added - page => $page,
443 );
14664e1c 444}
445
ea19a20c 446=head2 search_help
447
448Provides some help for the search form.
449
450=cut
451
14664e1c 452sub search_help : Local {
2646511e 453 my ($self, $c) = @_;
454 $c->stash(template => 'search_help.tt2');
6cfcd548 455}
456
ea19a20c 457=head2 atom
458
459Provides an atom feed for a given project.
460
461=cut
462
6cfcd548 463sub atom : Local {
d17ce39c 464 my($self, $c) = @_;
6cfcd548 465
d17ce39c 466 my $feed = XML::Atom::Feed->new;
6cfcd548 467
d17ce39c 468 my $host = lc Sys::Hostname::hostname();
469 $feed->title($host . ' - ' . Gitalist->config->{name});
470 $feed->updated(~~DateTime->now);
471
472 my $project = $c->stash->{Project};
473 my %logargs = (
474 sha1 => $project->head_hash,
475 count => Gitalist->config->{paging}{log} || 25,
476 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
477 );
478
479 my $mk_title = $c->stash->{short_cmt};
480 for my $commit ($project->list_revs(%logargs)) {
481 my $entry = XML::Atom::Entry->new;
482 $entry->title( $mk_title->($commit->comment) );
483 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
484 # XXX Needs work ...
485 $entry->content($commit->comment);
486 $feed->add_entry($entry);
487 }
488
f796a861 489 $c->response->body($feed->as_xml);
e75df318 490 $c->response->content_type('application/atom+xml');
f796a861 491 $c->response->status(200);
6cfcd548 492}
493
ea19a20c 494=head2 rss
495
496Provides an RSS feed for a given project.
497
498=cut
499
6cfcd548 500sub rss : Local {
f796a861 501 my ($self, $c) = @_;
502
503 my $project = $c->stash->{Project};
504
505 my $rss = XML::RSS->new(version => '2.0');
506 $rss->channel(
507 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
508 link => $c->uri_for('summary', {p=>$project->name}),
509 language => 'en',
510 description => $project->description,
511 pubDate => DateTime->now,
512 lastBuildDate => DateTime->now,
513 );
514
515 my %logargs = (
516 sha1 => $project->head_hash,
517 count => Gitalist->config->{paging}{log} || 25,
518 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
519 );
520 my $mk_title = $c->stash->{short_cmt};
521 for my $commit ($project->list_revs(%logargs)) {
522 # XXX Needs work ....
523 $rss->add_item(
524 title => $mk_title->($commit->comment),
525 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
526 description => $commit->comment,
527 );
528 }
529
530 $c->response->body($rss->as_string);
531 $c->response->content_type('application/rss+xml');
532 $c->response->status(200);
14664e1c 533}
534
ea19a20c 535=head2 patch
536
537A raw patch for a given commit.
538
539=cut
540
6cfcd548 541sub patch : Local {
377bf360 542 my ($self, $c) = @_;
61ba8635 543 $c->detach('patches', [1]);
544}
545
ea19a20c 546=head2 patches
547
548The patcheset for a given commit ???
549
550=cut
551
61ba8635 552sub patches : Local {
553 my ($self, $c, $count) = @_;
554 $count ||= Gitalist->config->{patches}{max};
377bf360 555 my $commit = $self->_get_object($c);
556 my $parent = $c->req->param('hp') || undef;
f707d264 557 my $patch = $commit->get_patch( $parent, $count );
377bf360 558 $c->response->body($patch);
559 $c->response->content_type('text/plain');
560 $c->response->status(200);
6cfcd548 561}
562
ea19a20c 563=head2 snapshot
564
565Provides a snapshot of a given commit.
566
567=cut
568
6cfcd548 569sub snapshot : Local {
30db8f5b 570 my ($self, $c) = @_;
63afe2db 571 my $format = $c->req->param('sf') || 'tgz';
30db8f5b 572 die unless $format;
2e79039a 573 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
c0dbe239 574 my @snap = $c->stash->{Project}->snapshot(
575 sha1 => $sha1,
576 format => $format
577 );
30db8f5b 578 $c->response->status(200);
579 $c->response->headers->header( 'Content-Disposition' =>
c0dbe239 580 "attachment; filename=$snap[0]");
581 $c->response->body($snap[1]);
6cfcd548 582}
583
1625cd5f 584=head2 auto
585
586Populate the header and footer. Perhaps not the best location.
587
588=cut
589
04d1d917 590sub auto : Private {
4621ecf0 591 my($self, $c) = @_;
04d1d917 592
da8f4f82 593 my $project = $c->req->param('p');
594 if (defined $project) {
595 eval {
596 $c->stash(Project => $c->model('GitRepos')->project($project));
597 };
598 if ($@) {
599 $c->detach('error_404');
600 }
601 }
602
603 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
4621ecf0 604 $c->stash(
da8f4f82 605 git_version => $a_project->run_cmd('--version'),
606 version => $Gitalist::VERSION,
607
608 # XXX Move these to a plugin!
4621ecf0 609 time_since => sub {
ef11b09e 610 return 'never' unless $_[0];
4621ecf0 611 return age_string(time - $_[0]->epoch);
612 },
613 short_cmt => sub {
614 my $cmt = shift;
615 my($line) = split /\n/, $cmt;
741e110e 616 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
4621ecf0 617 return $line;
618 },
5cd9b9f9 619 abridged_description => sub {
620 join(' ', grep { defined } (split / /, shift)[0..10]);
621 },
4621ecf0 622 );
04d1d917 623}
d9a9b56b 624
77730637 625sub opml : Local {
626 # FIXME - implement snapshot
627 Carp::croak "Not implemented.";
628}
77730637 629
89de6a33 630=head2 end
631
632Attempt to render a view, if needed.
633
634=cut
635
fde5091f 636sub end : ActionClass('RenderView') {
1aad4e81 637 my ($self, $c) = @_;
638 # Give project views the current HEAD.
639 if ($c->stash->{Project}) {
640 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
641 }
1fd8159c 642}
d9a9b56b 643
4111e151 644sub error_404 :Private {
645 my ($self, $c) = @_;
646 $c->response->status(404);
647 $c->stash(
648 title => 'Page not found',
649 content => 'Page not found',
1aad4e81 650 );
4111e151 651}
652
1fd8159c 653sub age_string {
da8f4f82 654 my $age = shift;
655 my $age_str;
656
657 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
658 $age_str = ( int $age / 60 / 60 / 24 / 365 );
659 $age_str .= " years ago";
660 }
661 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
662 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
663 $age_str .= " months ago";
664 }
665 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
666 $age_str = int $age / 60 / 60 / 24 / 7;
667 $age_str .= " weeks ago";
668 }
669 elsif ( $age > 60 * 60 * 24 * 2 ) {
670 $age_str = int $age / 60 / 60 / 24;
671 $age_str .= " days ago";
672 }
673 elsif ( $age > 60 * 60 * 2 ) {
674 $age_str = int $age / 60 / 60;
675 $age_str .= " hours ago";
676 }
677 elsif ( $age > 60 * 2 ) {
678 $age_str = int $age / 60;
679 $age_str .= " min ago";
680 }
681 elsif ( $age > 2 ) {
682 $age_str = int $age;
683 $age_str .= " sec ago";
684 }
685 else {
686 $age_str .= " right now";
687 }
688 return $age_str;
7a88ffa4 689}
690
da8f4f82 691
89de6a33 692=head1 AUTHOR
693
f71a83ab 694Dan Brook
89de6a33 695
696=head1 LICENSE
697
698This library is free software. You can redistribute it and/or modify
699it under the same terms as Perl itself.
700
701=cut
702
42fe5d11 703__PACKAGE__->meta->make_immutable;