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