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