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);
116 my @heads = $project->heads;
117 my $maxitems = Gitalist->config->{paging}{summary} || 10;
120 info => $project->info,
121 log_lines => [$project->list_revs(
122 sha1 => $commit->sha1,
125 refs => $project->references,
126 heads => [ @heads[0 .. $maxitems] ],
133 The current list of heads (aka branches) in the repo.
138 my ( $self, $c ) = @_;
139 my $project = $c->stash->{Project};
141 commit => $self->_get_commit($c),
142 heads => [$project->heads],
149 The blob action i.e the contents of a file.
154 my ( $self, $c ) = @_;
155 my $project = $c->stash->{Project};
156 my $h = $c->req->param('h')
157 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
158 || die "No file or sha1 provided.";
159 my $hb = $c->req->param('hb')
160 || $project->head_hash
161 || die "Couldn't discern the corresponding head.";
163 my $filename = $c->req->param('f') || '';
166 blob => $project->get_object($h)->contents,
167 head => $project->get_object($hb),
168 filename => $filename,
169 # XXX Hack hack hack, see View::SyntaxHighlight
170 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
174 $c->forward('View::SyntaxHighlight');
179 Exposes a given diff of a blob.
183 sub blobdiff : Local {
184 my ( $self, $c ) = @_;
185 my $commit = $self->_get_commit($c, $c->req->param('hb'));
186 my $filename = $c->req->param('f')
187 || croak("No file specified!");
188 my($tree, $patch) = $c->stash->{Project}->diff(
190 parent => $c->req->param('hpb') || '',
197 # XXX Hack hack hack, see View::SyntaxHighlight
198 blobs => [$patch->[0]->{diff}],
200 action => 'blobdiff',
203 $c->forward('View::SyntaxHighlight');
208 Exposes a given commit.
213 my ( $self, $c ) = @_;
214 my $project = $c->stash->{Project};
215 my $commit = $self->_get_commit($c);
218 diff_tree => ($project->diff(commit => $commit))[0],
219 refs => $project->references,
226 Exposes a given diff of a commit.
230 sub commitdiff : Local {
231 my ( $self, $c ) = @_;
232 my $commit = $self->_get_commit($c);
233 my($tree, $patch) = $c->stash->{Project}->diff(
235 parent => $c->req->param('hp') || undef,
242 # XXX Hack hack hack, see View::SyntaxHighlight
243 blobs => [map $_->{diff}, @$patch],
245 action => 'commitdiff',
248 $c->forward('View::SyntaxHighlight');
253 Expose an abbreviated log of a given sha1.
257 sub shortlog : Local {
258 my ( $self, $c ) = @_;
259 my $project = $c->stash->{Project};
260 my $commit = $self->_get_commit($c);
262 sha1 => $commit->sha1,
263 count => Gitalist->config->{paging}{log} || 25,
264 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
267 my $page = $c->req->param('pg') || 0;
268 $logargs{skip} = $c->req->param('pg') * $logargs{count}
269 if $c->req->param('pg');
273 log_lines => [$project->list_revs(%logargs)],
274 refs => $project->references,
275 action => 'shortlog',
282 Calls shortlog internally. Perhaps that should be reversed ...
286 $_[0]->shortlog($_[1]);
287 $_[1]->stash->{action} = 'log';
292 The tree of a given commit.
297 my ( $self, $c ) = @_;
298 my $project = $c->stash->{Project};
299 my $commit = $self->_get_commit($c, $c->req->param('hb'));
300 my $tree = $project->get_object($c->req->param('h') || $commit->tree_sha1);
302 # XXX Useful defaults needed ...
305 tree_list => [$project->list_tree($tree->sha1)],
306 path => $c->req->param('f') || '',
313 Expose the local reflog. This may go away.
318 my ( $self, $c ) = @_;
319 my @log = $c->stash->{Project}->reflog(
331 $c->stash(current_action => 'GitRepos');
332 my $project = $c->stash->{Project};
333 my $commit = $self->_get_commit($c);
334 # Lifted from /shortlog.
336 sha1 => $commit->sha1,
337 count => Gitalist->config->{paging}{log},
338 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
340 type => $c->req->param('type'),
341 text => $c->req->param('text'),
342 regexp => $c->req->param('regexp') || 0,
348 results => [$project->list_revs(%logargs)],
350 # This could be added - page => $page,
354 sub search_help : Local {
355 # FIXME - implement search_help
356 Carp::croak "Not implemented.";
360 # FIXME - implement atom
361 Carp::croak "Not implemented.";
365 # FIXME - implement rss
366 Carp::croak "Not implemented.";
369 sub blobdiff_plain : Local {
370 # FIXME - implement blobdiff_plain
371 Carp::croak "Not implemented.";
374 sub blob_plain : Local {
375 # FIXME - implement blobdiff_plain
376 Carp::croak "Not implemented.";
380 # FIXME - implement patches
381 Carp::croak "Not implemented.";
384 sub patches : Local {
385 # FIXME - implement patches
386 Carp::croak "Not implemented.";
389 sub snapshot : Local {
390 # FIXME - implement snapshot
391 Carp::croak "Not implemented.";
394 sub history : Local {
395 # FIXME - implement history
396 Carp::croak "Not implemented.";
399 sub commitdiff_plain : Local {
400 # FIXME - implement commitdiff_plain
401 Carp::croak "Not implemented.";
408 Populate the header and footer. Perhaps not the best location.
415 # XXX Move these to a plugin!
418 return 'never' unless $_[0];
419 return age_string(time - $_[0]->epoch);
423 my($line) = split /\n/, $cmt;
424 $line =~ s/^(.{70,80}\b).*/$1 …/;
427 abridged_description => sub {
428 join(' ', grep { defined } (split / /, shift)[0..10]);
432 # Yes, this is hideous.
437 # XXX This could probably be dropped altogether.
438 use Gitalist::Util qw(to_utf8);
439 # Formally git_header_html
443 my $title = $c->config->{sitename};
445 my $project = $c->req->param('project') || $c->req->param('p');
446 my $action = $c->req->param('action') || $c->req->param('a');
447 my $file_name = $c->req->param('filename') || $c->req->param('f');
448 if(defined $project) {
449 $title .= " - " . to_utf8($project);
450 if (defined $action) {
451 $title .= "/$action";
452 if (defined $file_name) {
453 $title .= " - " . $file_name;
454 if ($action eq "tree" && $file_name !~ m|/$|) {
461 $c->stash->{version} = $Gitalist::VERSION;
462 # check git's version by running it on the first project in the list.
463 $c->stash->{title} = $title;
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 $c->stash(Project => $c->model('GitRepos')->project($project));
522 $c->detach('error_404');
525 search_text => ( $c->req->param('s') ||
526 $c->req->param('searchtext') || ''),
527 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
528 || $c->req->param('h') || $c->req->param('hash')
532 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
533 $c->stash->{git_version} = $a_project->run_cmd('--version');
536 # Formally git_footer_html
540 my $feed_class = 'rss_logo';
543 my $project = $c->req->param('project') || $c->req->param('p');
544 if(defined $project) {
545 (my $pstr = $project) =~ s[/?\.git$][];
546 my $descr = $c->stash->{project_description}
547 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
549 my %href_params = $self->feed_info($c);
551 $feed_class .= ' generic';
553 $href_params{'-title'} ||= 'log';
557 class => $feed_class,
558 title => "$href_params{'-title'} $_ feed",
559 href => "/?p=$project;a=\L$_",
566 class => $feed_class,
568 href => "/?a=$_->[0]",
570 }, [opml=>'OPML'],[project_index=>'TXT'],
575 # XXX This feels wrong here, should probably be refactored.
576 # returns hash to be passed to href to generate gitweb URL
577 # in -title key it returns description of link
581 my $format = shift || 'Atom';
582 my %res = (action => lc($format));
584 # feed links are possible only for project views
585 return unless $c->req->param('project');
587 # some views should link to OPML, or to generic project feed,
588 # or don't have specific feed yet (so they should use generic)
589 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
592 my $hash = $c->req->param('h') || $c->req->param('hash');
593 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
595 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
596 # from tag links; this also makes possible to detect branch links
597 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
598 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
602 # find log type for feed description (title)
604 my $file_name = $c->req->param('f') || $c->req->param('filename');
605 if (defined $file_name) {
606 $type = "history of $file_name";
607 $type .= "/" if $c->req->param('action') eq 'tree';
608 $type .= " on '$branch'" if (defined $branch);
610 $type = "log of $branch" if (defined $branch);
613 $res{-title} = $type;
614 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
615 $res{'file_name'} = $file_name;
622 Attempt to render a view, if needed.
626 sub end : ActionClass('RenderView') {
628 # Give project views the current HEAD.
629 if ($c->stash->{Project}) {
630 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
634 sub error_404 :Private {
636 $c->response->status(404);
638 title => 'Page not found',
639 content => 'Page not found',
647 if ($age > 60*60*24*365*2) {
648 $age_str = (int $age/60/60/24/365);
649 $age_str .= " years ago";
650 } elsif ($age > 60*60*24*(365/12)*2) {
651 $age_str = int $age/60/60/24/(365/12);
652 $age_str .= " months ago";
653 } elsif ($age > 60*60*24*7*2) {
654 $age_str = int $age/60/60/24/7;
655 $age_str .= " weeks ago";
656 } elsif ($age > 60*60*24*2) {
657 $age_str = int $age/60/60/24;
658 $age_str .= " days ago";
659 } elsif ($age > 60*60*2) {
660 $age_str = int $age/60/60;
661 $age_str .= " hours ago";
662 } elsif ($age > 60*2) {
663 $age_str = int $age/60;
664 $age_str .= " min ago";
667 $age_str .= " sec ago";
669 $age_str .= " right now";
680 This library is free software. You can redistribute it and/or modify
681 it under the same terms as Perl itself.
685 __PACKAGE__->meta->make_immutable;