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