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