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);
115 my @heads = $project->heads;
116 my $maxitems = Gitalist->config->{paging}{summary} || 10;
119 info => $project->info,
120 log_lines => [$project->list_revs(
121 sha1 => $commit->sha1,
124 refs => $project->references,
125 heads => [ @heads[0 .. $maxitems] ],
132 The current list of heads (aka branches) in the repo.
137 my ( $self, $c ) = @_;
138 my $project = $c->stash->{Project};
140 commit => $self->_get_commit($c),
141 heads => [$project->heads],
148 The blob action i.e the contents of a file.
153 my ( $self, $c ) = @_;
154 my $project = $c->stash->{Project};
155 my $h = $c->req->param('h')
156 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
157 || die "No file or sha1 provided.";
158 my $hb = $c->req->param('hb')
159 || $project->head_hash
160 || die "Couldn't discern the corresponding head.";
162 my $filename = $c->req->param('f') || '';
165 blob => $project->get_object($h)->contents,
166 head => $project->get_object($hb),
167 filename => $filename,
168 # XXX Hack hack hack, see View::SyntaxHighlight
169 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
173 $c->forward('View::SyntaxHighlight');
178 Exposes a given diff of a blob.
182 sub blobdiff : Local {
183 my ( $self, $c ) = @_;
184 my $commit = $self->_get_commit($c, $c->req->param('hb'));
185 my $filename = $c->req->param('f')
186 || croak("No file specified!");
187 my($tree, $patch) = $c->stash->{Project}->diff(
189 parent => $c->req->param('hpb') || '',
196 # XXX Hack hack hack, see View::SyntaxHighlight
197 blobs => [$patch->[0]->{diff}],
199 action => 'blobdiff',
202 $c->forward('View::SyntaxHighlight');
207 Exposes a given commit.
212 my ( $self, $c ) = @_;
213 my $project = $c->stash->{Project};
214 my $commit = $self->_get_commit($c);
217 diff_tree => ($project->diff(commit => $commit))[0],
218 refs => $project->references,
225 Exposes a given diff of a commit.
229 sub commitdiff : Local {
230 my ( $self, $c ) = @_;
231 my $commit = $self->_get_commit($c);
232 my($tree, $patch) = $c->stash->{Project}->diff(
234 parent => $c->req->param('hp') || undef,
241 # XXX Hack hack hack, see View::SyntaxHighlight
242 blobs => [map $_->{diff}, @$patch],
244 action => 'commitdiff',
247 $c->forward('View::SyntaxHighlight');
252 Expose an abbreviated log of a given sha1.
256 sub shortlog : Local {
257 my ( $self, $c ) = @_;
258 my $project = $c->stash->{Project};
259 my $commit = $self->_get_commit($c);
261 sha1 => $commit->sha1,
262 count => Gitalist->config->{paging}{log} || 25,
263 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
266 my $page = $c->req->param('pg') || 0;
267 $logargs{skip} = $c->req->param('pg') * $logargs{count}
268 if $c->req->param('pg');
272 log_lines => [$project->list_revs(%logargs)],
273 refs => $project->references,
274 action => 'shortlog',
281 Calls shortlog internally. Perhaps that should be reversed ...
285 $_[0]->shortlog($_[1]);
286 $_[1]->stash->{action} = 'log';
291 The tree of a given commit.
296 my ( $self, $c ) = @_;
297 my $project = $c->stash->{Project};
298 my $commit = $self->_get_commit($c, $c->req->param('hb'));
299 my $tree = $project->get_object($c->req->param('h') || $commit->tree_sha1);
301 # XXX Useful defaults needed ...
304 tree_list => [$project->list_tree($tree->sha1)],
305 path => $c->req->param('f') || '',
312 Expose the local reflog. This may go away.
317 my ( $self, $c ) = @_;
318 my @log = $c->stash->{Project}->reflog(
330 $c->stash(current_action => 'GitRepos');
331 my $project = $c->stash->{Project};
332 my $commit = $self->_get_commit($c);
333 # Lifted from /shortlog.
335 sha1 => $commit->sha1,
336 count => Gitalist->config->{paging}{log},
337 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
339 type => $c->req->param('type'),
340 text => $c->req->param('text'),
341 regexp => $c->req->param('regexp') || 0,
347 results => [$project->list_revs(%logargs)],
349 # This could be added - page => $page,
353 sub search_help : Local {
354 # FIXME - implement search_help
355 Carp::croak "Not implemented.";
359 # FIXME - implement atom
360 Carp::croak "Not implemented.";
364 # FIXME - implement rss
365 Carp::croak "Not implemented.";
368 sub blobdiff_plain : Local {
369 # FIXME - implement blobdiff_plain
370 Carp::croak "Not implemented.";
373 sub blob_plain : Local {
374 # FIXME - implement blobdiff_plain
375 Carp::croak "Not implemented.";
379 # FIXME - implement patches
380 Carp::croak "Not implemented.";
383 sub patches : Local {
384 # FIXME - implement patches
385 Carp::croak "Not implemented.";
388 sub snapshot : Local {
389 # FIXME - implement snapshot
390 Carp::croak "Not implemented.";
393 sub history : Local {
394 # FIXME - implement history
395 Carp::croak "Not implemented.";
398 sub commitdiff_plain : Local {
399 # FIXME - implement commitdiff_plain
400 Carp::croak "Not implemented.";
407 Populate the header and footer. Perhaps not the best location.
414 # XXX Move these to a plugin!
417 return 'never' unless $_[0];
418 return age_string(time - $_[0]->epoch);
422 my($line) = split /\n/, $cmt;
423 $line =~ s/^(.{70,80}\b).*/$1 …/;
426 abridged_description => sub {
427 join(' ', grep { defined } (split / /, shift)[0..10]);
431 # Yes, this is hideous.
436 # XXX This could probably be dropped altogether.
437 use Gitalist::Util qw(to_utf8);
438 # Formally git_header_html
442 my $title = $c->config->{sitename};
444 my $project = $c->req->param('project') || $c->req->param('p');
445 my $action = $c->req->param('action') || $c->req->param('a');
446 my $file_name = $c->req->param('filename') || $c->req->param('f');
447 if(defined $project) {
448 $title .= " - " . to_utf8($project);
449 if (defined $action) {
450 $title .= "/$action";
451 if (defined $file_name) {
452 $title .= " - " . $file_name;
453 if ($action eq "tree" && $file_name !~ m|/$|) {
460 $c->stash->{version} = $Gitalist::VERSION;
461 $c->stash->{git_version} = $c->model('GitRepos')->run_cmd('--version');
462 $c->stash->{title} = $title;
464 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
465 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
467 $c->stash->{project} = $project;
470 my %href_params = $self->feed_info($c);
471 $href_params{'-title'} ||= 'log';
473 foreach my $format qw(RSS Atom) {
474 my $type = lc($format);
477 title => "$project - $href_params{'-title'} - $format feed",
479 # XXX A bit hacky and could do with using gitweb::href() features
480 href => "?a=$type;p=$project",
481 type => "application/$type+xml"
485 # XXX This duplication also feels a bit awkward
486 title => "$project - $href_params{'-title'} - $format feed (no merges)",
487 href => "?a=$type;p=$project;opt=--no-merges",
488 type => "application/$type+xml"
494 title => $c->config->{sitename}." projects list",
495 href => '?a=project_index',
496 type => "text/plain; charset=utf-8"
499 title => $c->config->{sitename}." projects feeds",
501 type => "text/plain; charset=utf-8"
505 $c->stash->{favicon} = $c->config->{favicon};
510 logo_url => $c->config->{logo_url},
511 logo_label => $c->config->{logo_label},
512 logo_img => $c->config->{logo},
513 home_link => $c->config->{home_link},
514 home_link_str => $c->config->{home_link_str},
517 if(defined $project) {
519 search_text => ( $c->req->param('s') ||
520 $c->req->param('searchtext') || ''),
521 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
522 || $c->req->param('h') || $c->req->param('hash')
524 Project => $c->model('GitRepos')->project($project),
529 # Formally git_footer_html
533 my $feed_class = 'rss_logo';
536 my $project = $c->req->param('project') || $c->req->param('p');
537 if(defined $project) {
538 (my $pstr = $project) =~ s[/?\.git$][];
539 my $descr = $c->stash->{project_description}
540 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
542 my %href_params = $self->feed_info($c);
544 $feed_class .= ' generic';
546 $href_params{'-title'} ||= 'log';
550 class => $feed_class,
551 title => "$href_params{'-title'} $_ feed",
552 href => "/?p=$project;a=\L$_",
559 class => $feed_class,
561 href => "/?a=$_->[0]",
563 }, [opml=>'OPML'],[project_index=>'TXT'],
568 # XXX This feels wrong here, should probably be refactored.
569 # returns hash to be passed to href to generate gitweb URL
570 # in -title key it returns description of link
574 my $format = shift || 'Atom';
575 my %res = (action => lc($format));
577 # feed links are possible only for project views
578 return unless $c->req->param('project');
580 # some views should link to OPML, or to generic project feed,
581 # or don't have specific feed yet (so they should use generic)
582 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
585 my $hash = $c->req->param('h') || $c->req->param('hash');
586 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
588 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
589 # from tag links; this also makes possible to detect branch links
590 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
591 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
595 # find log type for feed description (title)
597 my $file_name = $c->req->param('f') || $c->req->param('filename');
598 if (defined $file_name) {
599 $type = "history of $file_name";
600 $type .= "/" if $c->req->param('action') eq 'tree';
601 $type .= " on '$branch'" if (defined $branch);
603 $type = "log of $branch" if (defined $branch);
606 $res{-title} = $type;
607 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
608 $res{'file_name'} = $file_name;
615 Attempt to render a view, if needed.
619 sub end : ActionClass('RenderView') {
621 # Give project views the current HEAD.
622 if ($c->stash->{Project}) {
623 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
627 sub error_404 :Private {
629 $c->response->status(404);
631 title => 'Page not found',
632 content => 'Page not found',
640 if ($age > 60*60*24*365*2) {
641 $age_str = (int $age/60/60/24/365);
642 $age_str .= " years ago";
643 } elsif ($age > 60*60*24*(365/12)*2) {
644 $age_str = int $age/60/60/24/(365/12);
645 $age_str .= " months ago";
646 } elsif ($age > 60*60*24*7*2) {
647 $age_str = int $age/60/60/24/7;
648 $age_str .= " weeks ago";
649 } elsif ($age > 60*60*24*2) {
650 $age_str = int $age/60/60/24;
651 $age_str .= " days ago";
652 } elsif ($age > 60*60*2) {
653 $age_str = int $age/60/60;
654 $age_str .= " hours ago";
655 } elsif ($age > 60*2) {
656 $age_str = int $age/60;
657 $age_str .= " min ago";
660 $age_str .= " sec ago";
662 $age_str .= " right now";
673 This library is free software. You can redistribute it and/or modify
674 it under the same terms as Perl itself.
678 __PACKAGE__->meta->make_immutable;