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()->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 $c->detach('error_404') unless $project;
115 my $commit = $self->_get_commit($c);
118 info => $project->info,
119 log_lines => [$project->list_revs(
120 sha1 => $commit->sha1,
121 count => Gitalist->config->{paging}{summary} || 10
123 refs => $project->references,
124 heads => [$project->heads],
131 The current list of heads (aka branches) in the repo.
136 my ( $self, $c ) = @_;
137 my $project = $c->stash->{Project};
139 commit => $self->_get_commit($c),
140 heads => [$project->heads],
147 The blob action i.e the contents of a file.
152 my ( $self, $c ) = @_;
153 my $project = $c->stash->{Project};
154 my $h = $c->req->param('h')
155 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
156 || die "No file or sha1 provided.";
157 my $hb = $c->req->param('hb')
158 || $project->head_hash
159 || die "Couldn't discern the corresponding head.";
161 my $filename = $c->req->param('f') || '';
164 blob => $project->get_object($h)->contents,
165 head => $project->get_object($hb),
166 filename => $filename,
167 # XXX Hack hack hack, see View::SyntaxHighlight
168 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
172 $c->forward('View::SyntaxHighlight');
177 Exposes a given diff of a blob.
181 sub blobdiff : Local {
182 my ( $self, $c ) = @_;
183 my $commit = $self->_get_commit($c, $c->req->param('hb'));
184 my $filename = $c->req->param('f')
185 || croak("No file specified!");
186 my($tree, $patch) = $c->stash->{Project}->diff(
188 parent => $c->req->param('hpb') || '',
195 # XXX Hack hack hack, see View::SyntaxHighlight
196 blobs => [$patch->[0]->{diff}],
198 action => 'blobdiff',
201 $c->forward('View::SyntaxHighlight');
206 Exposes a given commit.
211 my ( $self, $c ) = @_;
212 my $project = $c->stash->{Project};
213 my $commit = $self->_get_commit($c);
216 diff_tree => ($project->diff(commit => $commit))[0],
217 refs => $project->references,
224 Exposes a given diff of a commit.
228 sub commitdiff : Local {
229 my ( $self, $c ) = @_;
230 my $commit = $self->_get_commit($c);
231 my($tree, $patch) = $c->stash->{Project}->diff(
233 parent => $c->req->param('hp') || undef,
240 # XXX Hack hack hack, see View::SyntaxHighlight
241 blobs => [map $_->{diff}, @$patch],
243 action => 'commitdiff',
246 $c->forward('View::SyntaxHighlight');
251 Expose an abbreviated log of a given sha1.
255 sub shortlog : Local {
256 my ( $self, $c ) = @_;
257 my $project = $c->stash->{Project};
258 my $commit = $self->_get_commit($c);
260 sha1 => $commit->sha1,
261 count => Gitalist->config->{paging}{log} || 25,
262 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
265 my $page = $c->req->param('pg') || 0;
266 $logargs{skip} = $c->req->param('pg') * $logargs{count}
267 if $c->req->param('pg');
271 log_lines => [$project->list_revs(%logargs)],
272 refs => $project->references,
273 action => 'shortlog',
280 Calls shortlog internally. Perhaps that should be reversed ...
284 $_[0]->shortlog($_[1]);
285 $_[1]->stash->{action} = 'log';
290 The tree of a given commit.
295 my ( $self, $c ) = @_;
296 my $project = $c->stash->{Project};
297 my $commit = $self->_get_commit($c, $c->req->param('hb'));
298 my $tree = $project->get_object($c->req->param('h') || $commit->tree_sha1);
300 # XXX Useful defaults needed ...
303 tree_list => [$project->list_tree($tree->sha1)],
304 path => $c->req->param('f') || '',
311 Expose the local reflog. This may go away.
316 my ( $self, $c ) = @_;
317 my @log = $c->stash->{Project}->reflog(
329 $c->stash(current_action => 'GitRepos');
330 my $project = $c->stash->{Project};
331 my $commit = $self->_get_commit($c);
332 # Lifted from /shortlog.
334 sha1 => $commit->sha1,
335 count => Gitalist->config->{paging}{log},
336 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
338 type => $c->req->param('type'),
339 text => $c->req->param('text'),
340 regexp => $c->req->param('regexp') || 0,
346 results => [$project->list_revs(%logargs)],
348 # This could be added - page => $page,
352 sub search_help : Local {
353 # FIXME - implement search_help
354 Carp::croak "Not implemented.";
358 # FIXME - implement atom
359 Carp::croak "Not implemented.";
363 # FIXME - implement rss
364 Carp::croak "Not implemented.";
367 sub blobdiff_plain : Local {
368 # FIXME - implement blobdiff_plain
369 Carp::croak "Not implemented.";
372 sub blob_plain : Local {
373 # FIXME - implement blobdiff_plain
374 Carp::croak "Not implemented.";
378 # FIXME - implement patches
379 Carp::croak "Not implemented.";
382 sub patches : Local {
383 # FIXME - implement patches
384 Carp::croak "Not implemented.";
387 sub snapshot : Local {
388 # FIXME - implement snapshot
389 Carp::croak "Not implemented.";
392 sub history : Local {
393 # FIXME - implement history
394 Carp::croak "Not implemented.";
397 sub commitdiff_plain : Local {
398 # FIXME - implement commitdiff_plain
399 Carp::croak "Not implemented.";
406 Populate the header and footer. Perhaps not the best location.
413 # XXX Move these to a plugin!
416 return 'never' unless $_[0];
417 return age_string(time - $_[0]->epoch);
421 my($line) = split /\n/, $cmt;
422 $line =~ s/^(.{70,80}\b).*/$1 …/;
425 abridged_description => sub {
426 join(' ', grep { defined } (split / /, shift)[0..10]);
430 # Yes, this is hideous.
435 # XXX This could probably be dropped altogether.
436 use Gitalist::Util qw(to_utf8);
437 # Formally git_header_html
441 my $title = $c->config->{sitename};
443 my $project = $c->req->param('project') || $c->req->param('p');
444 my $action = $c->req->param('action') || $c->req->param('a');
445 my $file_name = $c->req->param('filename') || $c->req->param('f');
446 if(defined $project) {
447 $title .= " - " . to_utf8($project);
448 if (defined $action) {
449 $title .= "/$action";
450 if (defined $file_name) {
451 $title .= " - " . $file_name;
452 if ($action eq "tree" && $file_name !~ m|/$|) {
459 $c->stash->{version} = $Gitalist::VERSION;
460 # check git's version by running it on the first project in the list.
461 $c->stash->{git_version} = $c->model()->projects->[0]->run_cmd('--version');
462 $c->stash->{title} = $title;
464 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
466 $c->stash->{project} = $project;
469 my %href_params = $self->feed_info($c);
470 $href_params{'-title'} ||= 'log';
472 foreach my $format qw(RSS Atom) {
473 my $type = lc($format);
476 title => "$project - $href_params{'-title'} - $format feed",
478 # XXX A bit hacky and could do with using gitweb::href() features
479 href => "?a=$type;p=$project",
480 type => "application/$type+xml"
484 # XXX This duplication also feels a bit awkward
485 title => "$project - $href_params{'-title'} - $format feed (no merges)",
486 href => "?a=$type;p=$project;opt=--no-merges",
487 type => "application/$type+xml"
493 title => $c->config->{sitename}." projects list",
494 href => '?a=project_index',
495 type => "text/plain; charset=utf-8"
498 title => $c->config->{sitename}." projects feeds",
500 type => "text/plain; charset=utf-8"
504 $c->stash->{favicon} = $c->config->{favicon};
509 logo_url => $c->config->{logo_url},
510 logo_label => $c->config->{logo_label},
511 logo_img => $c->config->{logo},
512 home_link => $c->config->{home_link},
513 home_link_str => $c->config->{home_link_str},
516 if (defined $project) {
518 $c->stash(Project => $c->model('GitRepos')->project($project));
521 $c->detach('error_404');
524 search_text => ( $c->req->param('s') ||
525 $c->req->param('searchtext') || ''),
526 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
527 || $c->req->param('h') || $c->req->param('hash')
533 # Formally git_footer_html
537 my $feed_class = 'rss_logo';
540 my $project = $c->req->param('project') || $c->req->param('p');
541 if(defined $project) {
542 (my $pstr = $project) =~ s[/?\.git$][];
543 my $descr = $c->stash->{project_description}
544 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
546 my %href_params = $self->feed_info($c);
548 $feed_class .= ' generic';
550 $href_params{'-title'} ||= 'log';
554 class => $feed_class,
555 title => "$href_params{'-title'} $_ feed",
556 href => "/?p=$project;a=\L$_",
563 class => $feed_class,
565 href => "/?a=$_->[0]",
567 }, [opml=>'OPML'],[project_index=>'TXT'],
572 # XXX This feels wrong here, should probably be refactored.
573 # returns hash to be passed to href to generate gitweb URL
574 # in -title key it returns description of link
578 my $format = shift || 'Atom';
579 my %res = (action => lc($format));
581 # feed links are possible only for project views
582 return unless $c->req->param('project');
584 # some views should link to OPML, or to generic project feed,
585 # or don't have specific feed yet (so they should use generic)
586 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
589 my $hash = $c->req->param('h') || $c->req->param('hash');
590 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
592 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
593 # from tag links; this also makes possible to detect branch links
594 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
595 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
599 # find log type for feed description (title)
601 my $file_name = $c->req->param('f') || $c->req->param('filename');
602 if (defined $file_name) {
603 $type = "history of $file_name";
604 $type .= "/" if $c->req->param('action') eq 'tree';
605 $type .= " on '$branch'" if (defined $branch);
607 $type = "log of $branch" if (defined $branch);
610 $res{-title} = $type;
611 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
612 $res{'file_name'} = $file_name;
619 Attempt to render a view, if needed.
623 sub end : ActionClass('RenderView') {
625 # Give project views the current HEAD.
626 if ($c->stash->{Project}) {
627 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
631 sub error_404 :Private {
633 $c->response->status(404);
635 title => 'Page not found',
636 content => 'Page not found',
644 if ($age > 60*60*24*365*2) {
645 $age_str = (int $age/60/60/24/365);
646 $age_str .= " years ago";
647 } elsif ($age > 60*60*24*(365/12)*2) {
648 $age_str = int $age/60/60/24/(365/12);
649 $age_str .= " months ago";
650 } elsif ($age > 60*60*24*7*2) {
651 $age_str = int $age/60/60/24/7;
652 $age_str .= " weeks ago";
653 } elsif ($age > 60*60*24*2) {
654 $age_str = int $age/60/60/24;
655 $age_str .= " days ago";
656 } elsif ($age > 60*60*2) {
657 $age_str = int $age/60/60;
658 $age_str .= " hours ago";
659 } elsif ($age > 60*2) {
660 $age_str = int $age/60;
661 $age_str .= " min ago";
664 $age_str .= " sec ago";
666 $age_str .= " right now";
677 This library is free software. You can redistribute it and/or modify
678 it under the same terms as Perl itself.
682 __PACKAGE__->meta->make_immutable;