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 ) = @_;
92 $c->detach($c->req->param('a'))
93 if $c->req->param('a');
95 my @list = @{ $c->model()->projects };
96 die 'No projects found in '. $c->model->repo_dir
99 my $search = $c->req->param('s') || '';
102 index($_->name, $search) > -1
103 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
108 search_text => $search,
116 A summary of what's happening in the repo.
120 sub summary : Local {
121 my ( $self, $c ) = @_;
122 my $project = $c->stash->{Project};
123 $c->detach('error_404') unless $project;
124 my $commit = $self->_get_object($c);
125 my @heads = @{$project->heads};
126 my $maxitems = Gitalist->config->{paging}{summary} || 10;
129 log_lines => [$project->list_revs(
130 sha1 => $commit->sha1,
133 refs => $project->references,
134 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
141 The current list of heads (aka branches) in the repo.
146 my ( $self, $c ) = @_;
147 my $project = $c->stash->{Project};
149 commit => $self->_get_object($c),
150 heads => $project->heads,
157 The blob action i.e the contents of a file.
162 my ( $self, $c ) = @_;
163 my $project = $c->stash->{Project};
164 my $h = $c->req->param('h')
165 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
166 || die "No file or sha1 provided.";
167 my $hb = $c->req->param('hb')
168 || $project->head_hash
169 || die "Couldn't discern the corresponding head.";
171 my $filename = $c->req->param('f') || '';
174 blob => $project->get_object($h)->content,
175 head => $project->get_object($hb),
176 filename => $filename,
177 # XXX Hack hack hack, see View::SyntaxHighlight
178 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
182 $c->forward('View::SyntaxHighlight')
183 unless $c->stash->{no_wrapper};
186 sub blob_plain : Local {
189 $c->stash(no_wrapper => 1);
190 $c->response->content_type('text/plain; charset=utf-8');
195 sub blobdiff_plain : Local {
198 $c->stash(no_wrapper => 1);
199 $c->response->content_type('text/plain; charset=utf-8');
201 $c->forward('blobdiff');
207 Exposes a given diff of a blob.
211 sub blobdiff : Local {
212 my ( $self, $c ) = @_;
213 my $commit = $self->_get_object($c, $c->req->param('hb'));
214 my $filename = $c->req->param('f')
215 || croak("No file specified!");
216 my($tree, $patch) = $c->stash->{Project}->diff(
219 parent => $c->req->param('hpb') || undef,
225 # XXX Hack hack hack, see View::SyntaxHighlight
226 blobs => [$patch->[0]->{diff}],
228 action => 'blobdiff',
231 $c->forward('View::SyntaxHighlight')
232 unless $c->stash->{no_wrapper};
237 Exposes a given commit.
242 my ( $self, $c ) = @_;
243 my $project = $c->stash->{Project};
244 my $commit = $self->_get_object($c);
247 diff_tree => ($project->diff(commit => $commit))[0],
248 refs => $project->references,
255 Exposes a given diff of a commit.
259 sub commitdiff : Local {
260 my ( $self, $c ) = @_;
261 my $commit = $self->_get_object($c);
262 my($tree, $patch) = $c->stash->{Project}->diff(
264 parent => $c->req->param('hp') || undef,
271 # XXX Hack hack hack, see View::SyntaxHighlight
272 blobs => [map $_->{diff}, @$patch],
274 action => 'commitdiff',
277 $c->forward('View::SyntaxHighlight')
278 unless $c->stash->{no_wrapper};
281 sub commitdiff_plain : Local {
284 $c->stash(no_wrapper => 1);
285 $c->response->content_type('text/plain; charset=utf-8');
287 $c->forward('commitdiff');
292 Expose an abbreviated log of a given sha1.
296 sub shortlog : Local {
297 my ( $self, $c ) = @_;
298 my $project = $c->stash->{Project};
299 my $commit = $self->_get_object($c);
301 sha1 => $commit->sha1,
302 count => Gitalist->config->{paging}{log} || 25,
303 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
306 my $page = $c->req->param('pg') || 0;
307 $logargs{skip} = $c->req->param('pg') * $logargs{count}
308 if $c->req->param('pg');
312 log_lines => [$project->list_revs(%logargs)],
313 refs => $project->references,
314 action => 'shortlog',
321 Calls shortlog internally. Perhaps that should be reversed ...
325 $_[0]->shortlog($_[1]);
326 $_[1]->stash->{action} = 'log';
329 # For legacy support.
330 sub history : Local {
331 $_[0]->shortlog(@_[1 .. $#_]);
336 The tree of a given commit.
341 my ( $self, $c ) = @_;
342 my $project = $c->stash->{Project};
343 my $commit = $self->_get_object($c, $c->req->param('hb'));
344 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
348 tree_list => [$project->list_tree($tree->sha1)],
349 path => $c->req->param('f') || '',
356 Expose the local reflog. This may go away.
361 my ( $self, $c ) = @_;
362 my @log = $c->stash->{Project}->reflog(
374 $c->stash(current_action => 'GitRepos');
375 my $project = $c->stash->{Project};
376 my $commit = $self->_get_object($c);
377 # Lifted from /shortlog.
379 sha1 => $commit->sha1,
380 count => Gitalist->config->{paging}{log},
381 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
383 type => $c->req->param('type'),
384 text => $c->req->param('text'),
385 regexp => $c->req->param('regexp') || 0,
391 results => [$project->list_revs(%logargs)],
393 # This could be added - page => $page,
397 sub search_help : Local {
398 # FIXME - implement search_help
399 Carp::croak "Not implemented.";
403 # FIXME - implement atom
404 Carp::croak "Not implemented.";
408 # FIXME - implement rss
409 Carp::croak "Not implemented.";
413 # FIXME - implement patches
414 Carp::croak "Not implemented.";
417 sub patches : Local {
418 # FIXME - implement patches
419 Carp::croak "Not implemented.";
422 sub snapshot : Local {
423 # FIXME - implement snapshot
424 Carp::croak "Not implemented.";
429 Populate the header and footer. Perhaps not the best location.
436 # XXX Move these to a plugin!
439 return 'never' unless $_[0];
440 return age_string(time - $_[0]->epoch);
444 my($line) = split /\n/, $cmt;
445 $line =~ s/^(.{70,80}\b).*/$1 …/;
448 abridged_description => sub {
449 join(' ', grep { defined } (split / /, shift)[0..10]);
453 # Yes, this is hideous.
458 # XXX This could probably be dropped altogether.
459 use Gitalist::Util qw(to_utf8);
460 # Formally git_header_html
464 my $title = $c->config->{sitename};
466 my $project = $c->req->param('project') || $c->req->param('p');
467 my $action = $c->req->param('action') || $c->req->param('a');
468 my $file_name = $c->req->param('filename') || $c->req->param('f');
469 if(defined $project) {
470 $title .= " - " . to_utf8($project);
471 if (defined $action) {
472 $title .= "/$action";
473 if (defined $file_name) {
474 $title .= " - " . $file_name;
475 if ($action eq "tree" && $file_name !~ m|/$|) {
482 $c->stash->{version} = $Gitalist::VERSION;
483 # check git's version by running it on the first project in the list.
484 $c->stash->{title} = $title;
486 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
488 $c->stash->{project} = $project;
491 my %href_params = $self->feed_info($c);
492 $href_params{'-title'} ||= 'log';
494 foreach my $format qw(RSS Atom) {
495 my $type = lc($format);
498 title => "$project - $href_params{'-title'} - $format feed",
500 # XXX A bit hacky and could do with using gitweb::href() features
501 href => "?a=$type;p=$project",
502 type => "application/$type+xml"
506 # XXX This duplication also feels a bit awkward
507 title => "$project - $href_params{'-title'} - $format feed (no merges)",
508 href => "?a=$type;p=$project;opt=--no-merges",
509 type => "application/$type+xml"
515 title => $c->config->{sitename}." projects list",
516 href => '?a=project_index',
517 type => "text/plain; charset=utf-8"
520 title => $c->config->{sitename}." projects feeds",
522 type => "text/plain; charset=utf-8"
526 $c->stash->{favicon} = $c->config->{favicon};
531 logo_url => $c->config->{logo_url},
532 logo_label => $c->config->{logo_label},
533 logo_img => $c->config->{logo},
534 home_link => $c->config->{home_link},
535 home_link_str => $c->config->{home_link_str},
538 if (defined $project) {
540 $c->stash(Project => $c->model('GitRepos')->project($project));
543 $c->detach('error_404');
546 search_text => ( $c->req->param('s') ||
547 $c->req->param('searchtext') || ''),
548 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
549 || $c->req->param('h') || $c->req->param('hash')
553 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
554 $c->stash->{git_version} = $a_project->run_cmd('--version');
557 # Formally git_footer_html
561 my $feed_class = 'rss_logo';
564 my $project = $c->req->param('project') || $c->req->param('p');
565 if(defined $project) {
566 (my $pstr = $project) =~ s[/?\.git$][];
567 my $descr = $c->stash->{project_description}
568 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
570 my %href_params = $self->feed_info($c);
572 $feed_class .= ' generic';
574 $href_params{'-title'} ||= 'log';
578 class => $feed_class,
579 title => "$href_params{'-title'} $_ feed",
580 href => "/?p=$project;a=\L$_",
587 class => $feed_class,
589 href => "/?a=$_->[0]",
591 }, [opml=>'OPML'],[project_index=>'TXT'],
596 # XXX This feels wrong here, should probably be refactored.
597 # returns hash to be passed to href to generate gitweb URL
598 # in -title key it returns description of link
602 my $format = shift || 'Atom';
603 my %res = (action => lc($format));
605 # feed links are possible only for project views
606 return unless $c->req->param('project');
608 # some views should link to OPML, or to generic project feed,
609 # or don't have specific feed yet (so they should use generic)
610 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
613 my $hash = $c->req->param('h') || $c->req->param('hash');
614 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
616 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
617 # from tag links; this also makes possible to detect branch links
618 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
619 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
623 # find log type for feed description (title)
625 my $file_name = $c->req->param('f') || $c->req->param('filename');
626 if (defined $file_name) {
627 $type = "history of $file_name";
628 $type .= "/" if $c->req->param('action') eq 'tree';
629 $type .= " on '$branch'" if (defined $branch);
631 $type = "log of $branch" if (defined $branch);
634 $res{-title} = $type;
635 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
636 $res{'file_name'} = $file_name;
643 Attempt to render a view, if needed.
647 sub end : ActionClass('RenderView') {
649 # Give project views the current HEAD.
650 if ($c->stash->{Project}) {
651 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
655 sub error_404 :Private {
657 $c->response->status(404);
659 title => 'Page not found',
660 content => 'Page not found',
668 if ($age > 60*60*24*365*2) {
669 $age_str = (int $age/60/60/24/365);
670 $age_str .= " years ago";
671 } elsif ($age > 60*60*24*(365/12)*2) {
672 $age_str = int $age/60/60/24/(365/12);
673 $age_str .= " months ago";
674 } elsif ($age > 60*60*24*7*2) {
675 $age_str = int $age/60/60/24/7;
676 $age_str .= " weeks ago";
677 } elsif ($age > 60*60*24*2) {
678 $age_str = int $age/60/60/24;
679 $age_str .= " days ago";
680 } elsif ($age > 60*60*2) {
681 $age_str = int $age/60/60;
682 $age_str .= " hours ago";
683 } elsif ($age > 60*2) {
684 $age_str = int $age/60;
685 $age_str .= " min ago";
688 $age_str .= " sec ago";
690 $age_str .= " right now";
701 This library is free software. You can redistribute it and/or modify
702 it under the same terms as Perl itself.
706 __PACKAGE__->meta->make_immutable;