1 package Gitalist::Controller::Root;
3 use namespace::autoclean;
5 BEGIN { extends 'Catalyst::Controller' }
8 # Sets the actions in this controller to be registered with no prefix
9 # so they function identically to actions created in MyApp.pm
11 __PACKAGE__->config->{namespace} = '';
15 Gitalist::Controller::Root - Root Controller for Gitalist
19 [enter your description here]
29 use IO::Capture::Stdout;
33 The C<gitweb> shim. It should now only be explicitly accessible by
39 my ( $self, $c ) = @_;
41 # XXX A slippery slope to be sure.
42 if($c->req->param('a')) {
43 my $capture = IO::Capture::Stdout->new();
46 my $action = gitweb::main($c);
55 my $output = join '', $capture->read;
56 $c->stash->{gitweb_output} = $output;
57 $c->stash->{template} = 'gitweb.tt2';
62 my($self, $c, $haveh) = @_;
64 my $h = $haveh || $c->req->param('h') || '';
65 my $f = $c->req->param('f');
67 my $m = $c->stash->{Project};
70 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
71 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
72 || ($f && $m->hash_by_path($f))
74 # XXX This could definitely use more context.
75 || Carp::croak("Couldn't find a hash for the commit object!");
77 my $commit = $m->get_object($hash)
78 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
85 Provides the project listing.
89 sub index :Path :Args(0) {
90 my ( $self, $c ) = @_;
91 $c->detach($c->req->param('a')) if $c->req->param('a');
93 my $list = $c->model()->list_projects;
95 die "No projects found in ". $c->model->repo_dir;
99 searchtext => $c->req->param('searchtext') || '',
107 A summary of what's happening in the repo.
111 sub summary : Local {
112 my ( $self, $c ) = @_;
113 my $project = $c->stash->{Project};
114 my $commit = $self->_get_commit($c);
117 info => $project->info,
118 log_lines => [$project->list_revs(
119 sha1 => $commit->sha1,
120 count => Gitalist->config->{paging}{summary} || 10
122 refs => $project->references,
123 heads => [$project->heads],
130 The current list of heads (aka branches) in the repo.
135 my ( $self, $c ) = @_;
136 my $project = $c->stash->{Project};
138 commit => $self->_get_commit($c),
139 heads => [$project->heads],
146 The blob action i.e the contents of a file.
151 my ( $self, $c ) = @_;
152 my $project = $c->stash->{Project};
153 my $h = $c->req->param('h')
154 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
155 || die "No file or sha1 provided.";
156 my $hb = $c->req->param('hb')
157 || $project->head_hash
158 || die "Couldn't discern the corresponding head.";
160 my $filename = $c->req->param('f') || '';
163 blob => $project->get_object($h)->contents,
164 head => $project->get_object($hb),
165 filename => $filename,
166 # XXX Hack hack hack, see View::SyntaxHighlight
167 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
171 $c->forward('View::SyntaxHighlight');
176 Exposes a given diff of a blob.
180 sub blobdiff : Local {
181 my ( $self, $c ) = @_;
182 my $commit = $self->_get_commit($c, $c->req->param('hb'));
183 my $filename = $c->req->param('f')
184 || croak("No file specified!");
185 my($tree, $patch) = $c->stash->{Project}->diff(
187 parent => $c->req->param('hpb') || '',
194 # XXX Hack hack hack, see View::SyntaxHighlight
195 blobs => [$patch->[0]->{diff}],
197 action => 'blobdiff',
200 $c->forward('View::SyntaxHighlight');
205 Exposes a given commit.
210 my ( $self, $c ) = @_;
211 my $project = $c->stash->{Project};
212 my $commit = $self->_get_commit($c);
215 diff_tree => ($project->diff(commit => $commit))[0],
216 refs => $project->references,
223 Exposes a given diff of a commit.
227 sub commitdiff : Local {
228 my ( $self, $c ) = @_;
229 my $commit = $self->_get_commit($c);
230 my($tree, $patch) = $c->stash->{Project}->diff(
232 parent => $c->req->param('hp') || undef,
239 # XXX Hack hack hack, see View::SyntaxHighlight
240 blobs => [map $_->{diff}, @$patch],
242 action => 'commitdiff',
245 $c->forward('View::SyntaxHighlight');
250 Expose an abbreviated log of a given sha1.
254 sub shortlog : Local {
255 my ( $self, $c ) = @_;
256 my $project = $c->stash->{Project};
257 my $commit = $self->_get_commit($c);
259 sha1 => $commit->sha1,
260 count => Gitalist->config->{paging}{log} || 25,
261 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
264 my $page = $c->req->param('pg') || 0;
265 $logargs{skip} = $c->req->param('pg') * $logargs{count}
266 if $c->req->param('pg');
270 log_lines => [$project->list_revs(%logargs)],
271 refs => $project->references,
272 action => 'shortlog',
279 Calls shortlog internally. Perhaps that should be reversed ...
283 $_[0]->shortlog($_[1]);
284 $_[1]->stash->{action} = 'log';
289 The tree of a given commit.
294 my ( $self, $c ) = @_;
295 my $project = $c->stash->{Project};
296 my $commit = $self->_get_commit($c, $c->req->param('hb'));
297 my $tree = $project->get_object($c->req->param('h') || $commit->tree_sha1);
299 # XXX Useful defaults needed ...
302 tree_list => [$project->list_tree($tree->sha1)],
303 path => $c->req->param('f') || '',
310 Expose the local reflog. This may go away.
315 my ( $self, $c ) = @_;
316 my @log = $c->stash->{Project}->reflog(
328 $c->stash(current_action => 'GitRepos');
329 my $project = $c->stash->{Project};
330 my $commit = $self->_get_commit($c);
331 # Lifted from /shortlog.
333 sha1 => $commit->sha1,
334 count => Gitalist->config->{paging}{log},
335 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
337 type => $c->req->param('type'),
338 text => $c->req->param('text'),
339 regexp => $c->req->param('regexp') || 0,
345 results => [$project->list_revs(%logargs)],
347 # This could be added - page => $page,
351 sub search_help : Local {
352 # FIXME - implement search_help
353 Carp::croak "Not implemented.";
357 # FIXME - implement atom
358 Carp::croak "Not implemented.";
362 # FIXME - implement rss
363 Carp::croak "Not implemented.";
366 sub blobdiff_plain : Local {
367 # FIXME - implement blobdiff_plain
368 Carp::croak "Not implemented.";
371 sub blob_plain : Local {
372 # FIXME - implement blobdiff_plain
373 Carp::croak "Not implemented.";
377 # FIXME - implement patches
378 Carp::croak "Not implemented.";
381 sub patches : Local {
382 # FIXME - implement patches
383 Carp::croak "Not implemented.";
386 sub snapshot : Local {
387 # FIXME - implement snapshot
388 Carp::croak "Not implemented.";
391 sub history : Local {
392 # FIXME - implement history
393 Carp::croak "Not implemented.";
396 sub commitdiff_plain : Local {
397 # FIXME - implement commitdiff_plain
398 Carp::croak "Not implemented.";
405 Populate the header and footer. Perhaps not the best location.
412 # XXX Move these to a plugin!
415 return 'never' unless $_[0];
416 return age_string(time - $_[0]->epoch);
420 my($line) = split /\n/, $cmt;
421 $line =~ s/^(.{70,80}\b).*/$1 …/;
424 abridged_description => sub {
425 join(' ', grep { defined } (split / /, shift)[0..10]);
429 # Yes, this is hideous.
434 # XXX This could probably be dropped altogether.
435 use Gitalist::Util qw(to_utf8);
436 # Formally git_header_html
440 my $title = $c->config->{sitename};
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');
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|/$|) {
458 $c->stash->{version} = $Gitalist::VERSION;
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');
461 $c->stash->{title} = $title;
463 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
465 $c->stash->{project} = $project;
468 my %href_params = $self->feed_info($c);
469 $href_params{'-title'} ||= 'log';
471 foreach my $format qw(RSS Atom) {
472 my $type = lc($format);
475 title => "$project - $href_params{'-title'} - $format feed",
477 # XXX A bit hacky and could do with using gitweb::href() features
478 href => "?a=$type;p=$project",
479 type => "application/$type+xml"
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"
492 title => $c->config->{sitename}." projects list",
493 href => '?a=project_index',
494 type => "text/plain; charset=utf-8"
497 title => $c->config->{sitename}." projects feeds",
499 type => "text/plain; charset=utf-8"
503 $c->stash->{favicon} = $c->config->{favicon};
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},
512 home_link_str => $c->config->{home_link_str},
515 if(defined $project) {
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')
522 Project => $c->model('GitRepos')->project($project),
527 # Formally git_footer_html
531 my $feed_class = 'rss_logo';
534 my $project = $c->req->param('project') || $c->req->param('p');
535 if(defined $project) {
536 (my $pstr = $project) =~ s[/?\.git$][];
537 my $descr = $c->stash->{project_description}
538 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
540 my %href_params = $self->feed_info($c);
542 $feed_class .= ' generic';
544 $href_params{'-title'} ||= 'log';
548 class => $feed_class,
549 title => "$href_params{'-title'} $_ feed",
550 href => "/?p=$project;a=\L$_",
557 class => $feed_class,
559 href => "/?a=$_->[0]",
561 }, [opml=>'OPML'],[project_index=>'TXT'],
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
572 my $format = shift || 'Atom';
573 my %res = (action => lc($format));
575 # feed links are possible only for project views
576 return unless $c->req->param('project');
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;
583 my $hash = $c->req->param('h') || $c->req->param('hash');
584 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
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/(.*)$!)) {
593 # find log type for feed description (title)
595 my $file_name = $c->req->param('f') || $c->req->param('filename');
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);
601 $type = "log of $branch" if (defined $branch);
604 $res{-title} = $type;
605 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
606 $res{'file_name'} = $file_name;
613 Attempt to render a view, if needed.
617 sub end : ActionClass('RenderView') {
619 # Give project views the current HEAD.
620 if ($c->stash->{Project}) {
621 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
625 sub error_404 :Private {
627 $c->response->status(404);
629 title => 'Page not found',
630 content => 'Page not found',
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";
658 $age_str .= " sec ago";
660 $age_str .= " right now";
671 This library is free software. You can redistribute it and/or modify
672 it under the same terms as Perl itself.
676 __PACKAGE__->meta->make_immutable;