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