Convert summary page to new model.
[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
7#
8# Sets the actions in this controller to be registered with no prefix
9# so they function identically to actions created in MyApp.pm
10#
11__PACKAGE__->config->{namespace} = '';
12
13=head1 NAME
14
15Gitalist::Controller::Root - Root Controller for Gitalist
16
17=head1 DESCRIPTION
18
19[enter your description here]
20
21=head1 METHODS
22
23=cut
24
25=head2 index
26
27=cut
28
89de6a33 29use IO::Capture::Stdout;
d3feefcf 30
1625cd5f 31=head2 run_gitweb
32
f71a83ab 33The C<gitweb> shim. It should now only be explicitly accessible by
34modifying the URL.
1625cd5f 35
36=cut
37
e66db0fb 38sub run_gitweb {
86382b95 39 my ( $self, $c ) = @_;
40
e66db0fb 41 # XXX A slippery slope to be sure.
42 if($c->req->param('a')) {
43 my $capture = IO::Capture::Stdout->new();
44 $capture->start();
45 eval {
46 my $action = gitweb::main($c);
47 $action->();
48 };
49 $capture->stop();
5a2f0948 50
e66db0fb 51 use Data::Dumper;
52 die Dumper($@)
53 if $@;
5a2f0948 54
e66db0fb 55 my $output = join '', $capture->read;
56 $c->stash->{gitweb_output} = $output;
57 $c->stash->{template} = 'gitweb.tt2';
58 }
86382b95 59}
60
9dc3b9a5 61sub _get_commit {
b4b4d0fd 62 my($self, $c, $haveh) = @_;
9dc3b9a5 63
c1f608c8 64 my $h = $haveh || $c->req->param('h') || '';
0ee97fec 65 my $f = $c->req->param('f');
8dbe8024 66
67 # FIXME this can die when everything is migrated
68 my ($m, $pd);
a05ba16e 69 if (defined $c->stash->{current_model} &&
70 $c->stash->{current_model} eq 'GitRepos') {
8dbe8024 71 $m = $c->model()->project($c->stash->{project});
72 $pd = $m->path;
73 } else {
74 $m = $c->model();
75 ($pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
76 }
0ee97fec 77
9dc3b9a5 78 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
a7cc1ede 79 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
80 || ($f && $m->hash_by_path($f))
81 || $m->head_hash
9dc3b9a5 82 # XXX This could definitely use more context.
83 || Carp::croak("Couldn't find a hash for the commit object!");
84
a7cc1ede 85 my $commit = $m->get_object($hash)
86 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
9dc3b9a5 87
88 return $commit;
89}
90
1625cd5f 91=head2 index
92
93Provides the project listing.
94
95=cut
96
86382b95 97sub index :Path :Args(0) {
d5cc37a4 98 my ( $self, $c ) = @_;
7b0f7465 99 $c->stash(current_model => 'GitRepos');
e66db0fb 100 # Leave actions up to gitweb at this point.
101 return $self->run_gitweb($c)
102 if $c->req->param('a');
103
887260eb 104 my $list = $c->model()->list_projects;
d5cc37a4 105 unless(@$list) {
7b0f7465 106 die "No projects found in ". $c->model->repo_dir;
86382b95 107 }
108
d5cc37a4 109 $c->stash(
110 searchtext => $c->req->param('searchtext') || '',
111 projects => $list,
112 action => 'index',
113 );
04d1d917 114}
115
790ce598 116=head2 summary
117
118A summary of what's happening in the repo.
119
120=cut
121
122sub summary : Local {
123 my ( $self, $c ) = @_;
4111e151 124 $c->stash(current_model => 'GitRepos');
125 warn("project is " . $c->stash->{project});
126 my $project;
127 eval {
128 $project = $c->model()->project($c->stash->{project});
129 };
130 if ($@) {
131 $c->detach('error_404');
132 }
9dc3b9a5 133 my $commit = $self->_get_commit($c);
790ce598 134 $c->stash(
135 commit => $commit,
4111e151 136 info => $project->info,
137 log_lines => [$project->list_revs(
138 sha1 => $commit->sha1,
139 count => Gitalist->config->{paging}{summary} || 50
fde5091f 140 )],
4111e151 141 refs => $project->references,
142 heads => [$project->heads],
790ce598 143 action => 'summary',
4111e151 144);
790ce598 145}
146
147=head2 heads
148
149The current list of heads (aka branches) in the repo.
150
151=cut
152
153sub heads : Local {
154 my ( $self, $c ) = @_;
8dbe8024 155 $c->stash( current_model => 'GitRepos' );
156 my $project = $c->model()->project( $c->stash->{project} );
790ce598 157 $c->stash(
9dc3b9a5 158 commit => $self->_get_commit($c),
8dbe8024 159 heads => [$project->heads],
790ce598 160 action => 'heads',
161 );
162}
163
1625cd5f 164=head2 blob
165
166The blob action i.e the contents of a file.
167
168=cut
169
295c9703 170sub blob : Local {
171 my ( $self, $c ) = @_;
172
c8870bd3 173 my $h = $c->req->param('h')
887260eb 174 || $c->model()->hash_by_path($c->req->param('f'))
c8870bd3 175 || die "No file or sha1 provided.";
176 my $hb = $c->req->param('hb')
887260eb 177 || $c->model()->head_hash
c8870bd3 178 || die "Couldn't discern the corresponding head.";
179
f5da8e7a 180 my $filename = $c->req->param('f') || '';
181
295c9703 182 $c->stash(
887260eb 183 blob => $c->model()->get_object($h)->content,
184 head => $c->model()->get_object($hb),
f5da8e7a 185 filename => $filename,
186 # XXX Hack hack hack, see View::SyntaxHighlight
187 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
c8870bd3 188 action => 'blob',
295c9703 189 );
7e54e579 190
c8870bd3 191 $c->forward('View::SyntaxHighlight');
295c9703 192}
193
6cf4366a 194=head2 blobdiff
195
196Exposes a given diff of a blob.
197
198=cut
199
200sub blobdiff : Local {
201 my ( $self, $c ) = @_;
202
203 my $commit = $self->_get_commit($c);
204 my $filename = $c->req->param('f')
205 || croak("No file specified!");
887260eb 206 my($tree, $patch) = $c->model()->diff(
ad8884fc 207 commit => $commit,
208 parent => $c->req->param('hp') || '',
209 file => $filename,
210 patch => 1,
6cf4366a 211 );
212 $c->stash(
213 commit => $commit,
ad8884fc 214 diff => $patch,
6cf4366a 215 # XXX Hack hack hack, see View::SyntaxHighlight
ad8884fc 216 blobs => [$patch->[0]->{diff}],
6cf4366a 217 language => 'Diff',
218 action => 'blobdiff',
219 );
220
221 $c->forward('View::SyntaxHighlight');
222}
223
1625cd5f 224=head2 commit
225
47495599 226Exposes a given commit.
1625cd5f 227
228=cut
229
1feb3d6b 230sub commit : Local {
d7c9a32f 231 my ( $self, $c ) = @_;
b7aca93a 232
9dc3b9a5 233 my $commit = $self->_get_commit($c);
b7aca93a 234 $c->stash(
790ce598 235 commit => $commit,
887260eb 236 diff_tree => ($c->model()->diff(commit => $commit))[0],
1fd8159c 237 refs => $c->model()->references,
790ce598 238 action => 'commit',
b7aca93a 239 );
d7c9a32f 240}
241
2247133f 242=head2 commitdiff
243
244Exposes a given diff of a commit.
245
246=cut
247
248sub commitdiff : Local {
249 my ( $self, $c ) = @_;
250
9dc3b9a5 251 my $commit = $self->_get_commit($c);
887260eb 252 my($tree, $patch) = $c->model()->diff(
ad8884fc 253 commit => $commit,
254 parent => $c->req->param('hp') || '',
255 patch => 1,
256 );
2247133f 257 $c->stash(
f5da8e7a 258 commit => $commit,
ad8884fc 259 diff_tree => $tree,
260 diff => $patch,
f5da8e7a 261 # XXX Hack hack hack, see View::SyntaxHighlight
ad8884fc 262 blobs => [map $_->{diff}, @$patch],
f5da8e7a 263 language => 'Diff',
264 action => 'commitdiff',
2247133f 265 );
f5da8e7a 266
267 $c->forward('View::SyntaxHighlight');
2247133f 268}
269
47495599 270=head2 shortlog
271
272Expose an abbreviated log of a given sha1.
273
274=cut
275
276sub shortlog : Local {
277 my ( $self, $c ) = @_;
278
63e220b9 279 my $commit = $self->_get_commit($c);
fde5091f 280 my %logargs = (
281 sha1 => $commit->sha1,
282 count => Gitalist->config->{paging}{log},
63e220b9 283 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
284 );
fde5091f 285
286 my $page = $c->req->param('pg') || 0;
287 $logargs{skip} = $c->req->param('pg') * $logargs{count}
288 if $c->req->param('pg');
289
47495599 290 $c->stash(
790ce598 291 commit => $commit,
887260eb 292 log_lines => [$c->model()->list_revs(%logargs)],
293 refs => $c->model()->references,
790ce598 294 action => 'shortlog',
b4b4d0fd 295 page => $page,
47495599 296 );
297}
298
790ce598 299=head2 log
300
301Calls shortlog internally. Perhaps that should be reversed ...
302
303=cut
304sub log : Local {
305 $_[0]->shortlog($_[1]);
306 $_[1]->stash->{action} = 'log';
307}
308
b3fa97cd 309=head2 tree
310
311The tree of a given commit.
312
313=cut
314
315sub tree : Local {
316 my ( $self, $c ) = @_;
317
b4b4d0fd 318 my $commit = $self->_get_commit($c, $c->req->param('hb'));
887260eb 319 my $tree = $c->model()->get_object($c->req->param('h') || $commit->tree_sha1);
b3fa97cd 320 $c->stash(
321 # XXX Useful defaults needed ...
790ce598 322 commit => $commit,
b4b4d0fd 323 tree => $tree,
887260eb 324 tree_list => [$c->model()->list_tree($tree->sha1)],
b4b4d0fd 325 path => $c->req->param('f') || '',
790ce598 326 action => 'tree',
b3fa97cd 327 );
328}
329
9dc3b9a5 330=head2 reflog
331
332Expose the local reflog. This may go away.
333
334=cut
335
336sub reflog : Local {
337 my ( $self, $c ) = @_;
338
887260eb 339 my @log = $c->model()->reflog(
9dc3b9a5 340 '--since=yesterday'
341 );
342
343 $c->stash(
344 log => \@log,
345 action => 'reflog',
346 );
347}
348
14664e1c 349sub search : Local {
4df2f62f 350 my($self, $c) = @_;
351
352 my $commit = $self->_get_commit($c);
353 # Lifted from /shortlog.
354 my %logargs = (
355 sha1 => $commit->sha1,
356 count => Gitalist->config->{paging}{log},
357 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
358 search => {
359 type => $c->req->param('type'),
360 text => $c->req->param('text'),
361 regexp => $c->req->param('regexp') || 0,
362 }
363 );
364
365 $c->stash(
366 commit => $commit,
887260eb 367 results => [$c->model()->list_revs(%logargs)],
4df2f62f 368 action => 'search',
369 # This could be added - page => $page,
370 );
14664e1c 371}
372
373sub search_help : Local {
374 Carp::croak "Not implemented.";
375}
376
1625cd5f 377=head2 auto
378
379Populate the header and footer. Perhaps not the best location.
380
381=cut
382
04d1d917 383sub auto : Private {
4621ecf0 384 my($self, $c) = @_;
04d1d917 385
4621ecf0 386 # XXX Move these to a plugin!
387 $c->stash(
388 time_since => sub {
ef11b09e 389 return 'never' unless $_[0];
4621ecf0 390 return age_string(time - $_[0]->epoch);
391 },
392 short_cmt => sub {
393 my $cmt = shift;
394 my($line) = split /\n/, $cmt;
395 $line =~ s/^(.{70,80}\b).*/$1 …/;
396 return $line;
397 },
5cd9b9f9 398 abridged_description => sub {
399 join(' ', grep { defined } (split / /, shift)[0..10]);
400 },
4621ecf0 401 );
402
403 # Yes, this is hideous.
404 $self->header($c);
405 $self->footer($c);
04d1d917 406}
407
c5065c66 408# XXX This could probably be dropped altogether.
04d1d917 409use Gitalist::Util qw(to_utf8);
04d1d917 410# Formally git_header_html
411sub header {
412 my($self, $c) = @_;
413
c5065c66 414 my $title = $c->config->{sitename};
04d1d917 415
416 my $project = $c->req->param('project') || $c->req->param('p');
417 my $action = $c->req->param('action') || $c->req->param('a');
418 my $file_name = $c->req->param('filename') || $c->req->param('f');
c5065c66 419 if(defined $project) {
420 $title .= " - " . to_utf8($project);
421 if (defined $action) {
422 $title .= "/$action";
423 if (defined $file_name) {
424 $title .= " - " . $file_name;
425 if ($action eq "tree" && $file_name !~ m|/$|) {
426 $title .= "/";
427 }
428 }
429 }
430 }
431
432 $c->stash->{version} = $c->config->{version};
887260eb 433 $c->stash->{git_version} = $c->model()->run_cmd('--version');
c5065c66 434 $c->stash->{title} = $title;
04d1d917 435
436 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
c5065c66 437 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
04d1d917 438
c5065c66 439 $c->stash->{project} = $project;
04d1d917 440 my @links;
c5065c66 441 if($project) {
442 my %href_params = $self->feed_info($c);
443 $href_params{'-title'} ||= 'log';
04d1d917 444
c5065c66 445 foreach my $format qw(RSS Atom) {
446 my $type = lc($format);
04d1d917 447 push @links, {
c5065c66 448 rel => 'alternate',
449 title => "$project - $href_params{'-title'} - $format feed",
450
451 # XXX A bit hacky and could do with using gitweb::href() features
452 href => "?a=$type;p=$project",
453 type => "application/$type+xml"
04d1d917 454 }, {
c5065c66 455 rel => 'alternate',
456
457 # XXX This duplication also feels a bit awkward
458 title => "$project - $href_params{'-title'} - $format feed (no merges)",
459 href => "?a=$type;p=$project;opt=--no-merges",
460 type => "application/$type+xml"
04d1d917 461 };
c5065c66 462 }
463 } else {
04d1d917 464 push @links, {
c5065c66 465 rel => "alternate",
466 title => $c->config->{sitename}." projects list",
467 href => '?a=project_index',
468 type => "text/plain; charset=utf-8"
469 }, {
470 rel => "alternate",
471 title => $c->config->{sitename}." projects feeds",
472 href => '?a=opml',
473 type => "text/plain; charset=utf-8"
474 };
475 }
04d1d917 476
c5065c66 477 $c->stash->{favicon} = $c->config->{favicon};
04d1d917 478
c5065c66 479 # </head><body>
86382b95 480
c5065c66 481 $c->stash(
482 logo_url => $c->config->{logo_url},
483 logo_label => $c->config->{logo_label},
484 logo_img => $c->config->{logo},
485 home_link => $c->config->{home_link},
04d1d917 486 home_link_str => $c->config->{home_link_str},
c5065c66 487 );
04d1d917 488
c5065c66 489 if(defined $project) {
04d1d917 490 $c->stash(
14664e1c 491 search_text => ( $c->req->param('s') || $c->req->param('searchtext') || ''),
295c9703 492 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
c5065c66 493 || $c->req->param('h') || $c->req->param('hash')
494 || 'HEAD' ),
495 );
496 }
04d1d917 497}
498
499# Formally git_footer_html
500sub footer {
501 my($self, $c) = @_;
502
c5065c66 503 my $feed_class = 'rss_logo';
04d1d917 504
505 my @feeds;
506 my $project = $c->req->param('project') || $c->req->param('p');
c5065c66 507 if(defined $project) {
d5cc37a4 508 (my $pstr = $project) =~ s[/?\.git$][];
887260eb 509 my $descr = $c->model()->project_info($project)->{description};
c5065c66 510 $c->stash->{project_description} = defined $descr
511 ? $descr
512 : '';
04d1d917 513
c5065c66 514 my %href_params = $self->feed_info($c);
515 if (!%href_params) {
516 $feed_class .= ' generic';
517 }
518 $href_params{'-title'} ||= 'log';
04d1d917 519
520 @feeds = [
521 map +{
522 class => $feed_class,
523 title => "$href_params{'-title'} $_ feed",
524 href => "/?p=$project;a=\L$_",
525 name => lc $_,
c5065c66 526 }, qw(RSS Atom)
527 ];
528 } else {
04d1d917 529 @feeds = [
530 map {
531 class => $feed_class,
c5065c66 532 title => '',
533 href => "/?a=$_->[0]",
534 name => $_->[1],
535 }, [opml=>'OPML'],[project_index=>'TXT'],
536 ];
537 }
89de6a33 538}
539
04d1d917 540# XXX This feels wrong here, should probably be refactored.
541# returns hash to be passed to href to generate gitweb URL
542# in -title key it returns description of link
543sub feed_info {
544 my($self, $c) = @_;
545
c5065c66 546 my $format = shift || 'Atom';
547 my %res = (action => lc($format));
04d1d917 548
c5065c66 549 # feed links are possible only for project views
550 return unless $c->req->param('project');
04d1d917 551
c5065c66 552 # some views should link to OPML, or to generic project feed,
553 # or don't have specific feed yet (so they should use generic)
554 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
555
556 my $branch;
04d1d917 557 my $hash = $c->req->param('h') || $c->req->param('hash');
558 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
c5065c66 559
560 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
561 # from tag links; this also makes possible to detect branch links
562 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
563 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
564 $branch = $1;
565 }
566
567 # find log type for feed description (title)
568 my $type = 'log';
04d1d917 569 my $file_name = $c->req->param('f') || $c->req->param('filename');
c5065c66 570 if (defined $file_name) {
571 $type = "history of $file_name";
572 $type .= "/" if $c->req->param('action') eq 'tree';
573 $type .= " on '$branch'" if (defined $branch);
574 } else {
575 $type = "log of $branch" if (defined $branch);
576 }
577
578 $res{-title} = $type;
579 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
580 $res{'file_name'} = $file_name;
581
582 return %res;
04d1d917 583}
d9a9b56b 584
89de6a33 585=head2 end
586
587Attempt to render a view, if needed.
588
589=cut
590
fde5091f 591sub end : ActionClass('RenderView') {
1fd8159c 592 my ($self, $c) = @_;
593 # Give project views the current HEAD.
594 if ($c->stash->{project}) {
a05ba16e 595 if ($c->stash->{current_model} &&
596 $c->stash->{current_model} eq 'GitRepos') {
8dbe8024 597 my $project = $c->model()->project($c->stash->{project});
598 $c->stash->{HEAD} = $project->head_hash;
599 } else {
600 $c->stash->{HEAD} = $c->model()->head_hash;
601 }
1fd8159c 602 }
1fd8159c 603}
d9a9b56b 604
4111e151 605sub error_404 :Private {
606 my ($self, $c) = @_;
607 $c->response->status(404);
608 $c->stash(
609 title => 'Page not found',
610 content => 'Page not found',
611 )
612}
613
1fd8159c 614sub age_string {
615 my $age = shift;
616 my $age_str;
617
618 if ($age > 60*60*24*365*2) {
619 $age_str = (int $age/60/60/24/365);
620 $age_str .= " years ago";
621 } elsif ($age > 60*60*24*(365/12)*2) {
622 $age_str = int $age/60/60/24/(365/12);
623 $age_str .= " months ago";
624 } elsif ($age > 60*60*24*7*2) {
625 $age_str = int $age/60/60/24/7;
626 $age_str .= " weeks ago";
627 } elsif ($age > 60*60*24*2) {
628 $age_str = int $age/60/60/24;
629 $age_str .= " days ago";
630 } elsif ($age > 60*60*2) {
631 $age_str = int $age/60/60;
632 $age_str .= " hours ago";
633 } elsif ($age > 60*2) {
634 $age_str = int $age/60;
635 $age_str .= " min ago";
636 } elsif ($age > 2) {
637 $age_str = int $age;
638 $age_str .= " sec ago";
639 } else {
640 $age_str .= " right now";
641 }
642 return $age_str;
7a88ffa4 643}
644
89de6a33 645=head1 AUTHOR
646
f71a83ab 647Dan Brook
89de6a33 648
649=head1 LICENSE
650
651This library is free software. You can redistribute it and/or modify
652it under the same terms as Perl itself.
653
654=cut
655
42fe5d11 656__PACKAGE__->meta->make_immutable;