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 # FIXME this can die when everything is migrated
69 if (defined $c->stash->{current_model} &&
70 $c->stash->{current_model} eq 'GitRepos') {
71 $m = $c->stash->{Project};
75 ($pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
78 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
79 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
80 || ($f && $m->hash_by_path($f))
82 # XXX This could definitely use more context.
83 || Carp::croak("Couldn't find a hash for the commit object!");
85 my $commit = $m->get_object($hash)
86 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
93 Provides the project listing.
97 sub index :Path :Args(0) {
98 my ( $self, $c ) = @_;
99 $c->detach($c->req->param('a')) if $c->req->param('a');
100 $c->stash(current_model => 'GitRepos');
102 my $list = $c->model()->list_projects;
104 die "No projects found in ". $c->model->repo_dir;
108 searchtext => $c->req->param('searchtext') || '',
116 A summary of what's happening in the repo.
120 sub summary : Local {
121 my ( $self, $c ) = @_;
122 $c->stash(current_model => 'GitRepos');
123 my $project = $c->stash->{Project};
124 my $commit = $self->_get_commit($c);
127 info => $project->info,
128 log_lines => [$project->list_revs(
129 sha1 => $commit->sha1,
130 count => Gitalist->config->{paging}{summary} || 10
132 refs => $project->references,
133 heads => [$project->heads],
140 The current list of heads (aka branches) in the repo.
145 my ( $self, $c ) = @_;
146 $c->stash( current_model => 'GitRepos' );
147 my $project = $c->stash->{Project};
149 commit => $self->_get_commit($c),
150 heads => [$project->heads],
157 The blob action i.e the contents of a file.
162 my ( $self, $c ) = @_;
163 $c->stash(current_model => 'GitRepos');
164 my $project = $c->stash->{Project};
165 my $h = $c->req->param('h')
166 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
167 || die "No file or sha1 provided.";
168 my $hb = $c->req->param('hb')
169 || $project->head_hash
170 || die "Couldn't discern the corresponding head.";
172 my $filename = $c->req->param('f') || '';
175 blob => $project->get_object($h)->contents,
176 head => $project->get_object($hb),
177 filename => $filename,
178 # XXX Hack hack hack, see View::SyntaxHighlight
179 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
183 $c->forward('View::SyntaxHighlight');
188 Exposes a given diff of a blob.
192 sub blobdiff : Local {
193 my ( $self, $c ) = @_;
194 $c->stash(current_model => 'GitRepos');
195 my $commit = $self->_get_commit($c, $c->req->param('hb'));
196 my $filename = $c->req->param('f')
197 || croak("No file specified!");
198 my($tree, $patch) = $c->stash->{Project}->diff(
200 parent => $c->req->param('hpb') || '',
207 # XXX Hack hack hack, see View::SyntaxHighlight
208 blobs => [$patch->[0]->{diff}],
210 action => 'blobdiff',
213 $c->forward('View::SyntaxHighlight');
218 Exposes a given commit.
223 my ( $self, $c ) = @_;
224 $c->stash(current_model => 'GitRepos');
225 my $project = $c->stash->{Project};
226 my $commit = $self->_get_commit($c);
229 diff_tree => ($project->diff(commit => $commit))[0],
230 refs => $project->references,
237 Exposes a given diff of a commit.
241 sub commitdiff : Local {
242 my ( $self, $c ) = @_;
243 $c->stash(current_model => 'GitRepos');
244 my $commit = $self->_get_commit($c);
245 my($tree, $patch) = $c->stash->{Project}->diff(
247 parent => $c->req->param('hp') || undef,
254 # XXX Hack hack hack, see View::SyntaxHighlight
255 blobs => [map $_->{diff}, @$patch],
257 action => 'commitdiff',
260 $c->forward('View::SyntaxHighlight');
265 Expose an abbreviated log of a given sha1.
269 sub shortlog : Local {
270 my ( $self, $c ) = @_;
271 $c->stash(current_model => 'GitRepos');
272 my $project = $c->stash->{Project};
273 my $commit = $self->_get_commit($c);
275 sha1 => $commit->sha1,
276 count => Gitalist->config->{paging}{log} || 25,
277 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
280 my $page = $c->req->param('pg') || 0;
281 $logargs{skip} = $c->req->param('pg') * $logargs{count}
282 if $c->req->param('pg');
286 log_lines => [$project->list_revs(%logargs)],
287 refs => $project->references,
288 action => 'shortlog',
295 Calls shortlog internally. Perhaps that should be reversed ...
299 $_[0]->shortlog($_[1]);
300 $_[1]->stash->{action} = 'log';
305 The tree of a given commit.
310 my ( $self, $c ) = @_;
311 $c->stash(current_model => 'GitRepos');
312 my $project = $c->stash->{Project};
313 my $commit = $self->_get_commit($c, $c->req->param('hb'));
314 my $tree = $project->get_object($c->req->param('h') || $commit->tree_sha1);
316 # XXX Useful defaults needed ...
319 tree_list => [$project->list_tree($tree->sha1)],
320 path => $c->req->param('f') || '',
327 Expose the local reflog. This may go away.
332 my ( $self, $c ) = @_;
333 $c->stash(current_model => 'GitRepos');
334 my @log = $c->stash->{Project}->reflog(
346 $c->stash(current_action => 'GitRepos');
347 my $project = $c->stash->{Project};
348 my $commit = $self->_get_commit($c);
349 # Lifted from /shortlog.
351 sha1 => $commit->sha1,
352 count => Gitalist->config->{paging}{log},
353 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
355 type => $c->req->param('type'),
356 text => $c->req->param('text'),
357 regexp => $c->req->param('regexp') || 0,
363 results => [$project->list_revs(%logargs)],
365 # This could be added - page => $page,
369 sub search_help : Local {
370 # FIXME - implement search_help
371 Carp::croak "Not implemented.";
375 # FIXME - implement atom
376 Carp::croak "Not implemented.";
380 # FIXME - implement rss
381 Carp::croak "Not implemented.";
384 sub blobdiff_plain : Local {
385 # FIXME - implement blobdiff_plain
386 Carp::croak "Not implemented.";
389 sub blob_plain : Local {
390 # FIXME - implement blobdiff_plain
391 Carp::croak "Not implemented.";
395 # FIXME - implement patches
396 Carp::croak "Not implemented.";
399 sub patches : Local {
400 # FIXME - implement patches
401 Carp::croak "Not implemented.";
404 sub snapshot : Local {
405 # FIXME - implement snapshot
406 Carp::croak "Not implemented.";
409 sub history : Local {
410 # FIXME - implement history
411 Carp::croak "Not implemented.";
414 sub commitdiff_plain : Local {
415 # FIXME - implement commitdiff_plain
416 Carp::croak "Not implemented.";
423 Populate the header and footer. Perhaps not the best location.
430 # XXX Move these to a plugin!
433 return 'never' unless $_[0];
434 return age_string(time - $_[0]->epoch);
438 my($line) = split /\n/, $cmt;
439 $line =~ s/^(.{70,80}\b).*/$1 …/;
442 abridged_description => sub {
443 join(' ', grep { defined } (split / /, shift)[0..10]);
447 # Yes, this is hideous.
452 # XXX This could probably be dropped altogether.
453 use Gitalist::Util qw(to_utf8);
454 # Formally git_header_html
458 my $title = $c->config->{sitename};
460 my $project = $c->req->param('project') || $c->req->param('p');
461 my $action = $c->req->param('action') || $c->req->param('a');
462 my $file_name = $c->req->param('filename') || $c->req->param('f');
463 if(defined $project) {
464 $title .= " - " . to_utf8($project);
465 if (defined $action) {
466 $title .= "/$action";
467 if (defined $file_name) {
468 $title .= " - " . $file_name;
469 if ($action eq "tree" && $file_name !~ m|/$|) {
476 $c->stash->{version} = $c->config->{version};
477 $c->stash->{git_version} = $c->model()->run_cmd('--version');
478 $c->stash->{title} = $title;
480 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
481 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
483 $c->stash->{project} = $project;
486 my %href_params = $self->feed_info($c);
487 $href_params{'-title'} ||= 'log';
489 foreach my $format qw(RSS Atom) {
490 my $type = lc($format);
493 title => "$project - $href_params{'-title'} - $format feed",
495 # XXX A bit hacky and could do with using gitweb::href() features
496 href => "?a=$type;p=$project",
497 type => "application/$type+xml"
501 # XXX This duplication also feels a bit awkward
502 title => "$project - $href_params{'-title'} - $format feed (no merges)",
503 href => "?a=$type;p=$project;opt=--no-merges",
504 type => "application/$type+xml"
510 title => $c->config->{sitename}." projects list",
511 href => '?a=project_index',
512 type => "text/plain; charset=utf-8"
515 title => $c->config->{sitename}." projects feeds",
517 type => "text/plain; charset=utf-8"
521 $c->stash->{favicon} = $c->config->{favicon};
526 logo_url => $c->config->{logo_url},
527 logo_label => $c->config->{logo_label},
528 logo_img => $c->config->{logo},
529 home_link => $c->config->{home_link},
530 home_link_str => $c->config->{home_link_str},
533 if(defined $project) {
535 search_text => ( $c->req->param('s') ||
536 $c->req->param('searchtext') || ''),
537 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
538 || $c->req->param('h') || $c->req->param('hash')
540 Project => $c->model('GitRepos')->project($project),
545 # Formally git_footer_html
549 my $feed_class = 'rss_logo';
552 my $project = $c->req->param('project') || $c->req->param('p');
553 if(defined $project) {
554 (my $pstr = $project) =~ s[/?\.git$][];
555 my $descr = $c->model()->project_info($project)->{description};
556 $c->stash->{project_description} = defined $descr
560 my %href_params = $self->feed_info($c);
562 $feed_class .= ' generic';
564 $href_params{'-title'} ||= 'log';
568 class => $feed_class,
569 title => "$href_params{'-title'} $_ feed",
570 href => "/?p=$project;a=\L$_",
577 class => $feed_class,
579 href => "/?a=$_->[0]",
581 }, [opml=>'OPML'],[project_index=>'TXT'],
586 # XXX This feels wrong here, should probably be refactored.
587 # returns hash to be passed to href to generate gitweb URL
588 # in -title key it returns description of link
592 my $format = shift || 'Atom';
593 my %res = (action => lc($format));
595 # feed links are possible only for project views
596 return unless $c->req->param('project');
598 # some views should link to OPML, or to generic project feed,
599 # or don't have specific feed yet (so they should use generic)
600 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
603 my $hash = $c->req->param('h') || $c->req->param('hash');
604 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
606 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
607 # from tag links; this also makes possible to detect branch links
608 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
609 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
613 # find log type for feed description (title)
615 my $file_name = $c->req->param('f') || $c->req->param('filename');
616 if (defined $file_name) {
617 $type = "history of $file_name";
618 $type .= "/" if $c->req->param('action') eq 'tree';
619 $type .= " on '$branch'" if (defined $branch);
621 $type = "log of $branch" if (defined $branch);
624 $res{-title} = $type;
625 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
626 $res{'file_name'} = $file_name;
633 Attempt to render a view, if needed.
637 sub end : ActionClass('RenderView') {
639 # Give project views the current HEAD.
640 if ($c->stash->{project}) {
641 if ($c->stash->{current_model} &&
642 $c->stash->{current_model} eq 'GitRepos') {
643 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
645 $c->stash->{HEAD} = $c->model()->head_hash;
650 sub error_404 :Private {
652 $c->response->status(404);
654 title => 'Page not found',
655 content => 'Page not found',
663 if ($age > 60*60*24*365*2) {
664 $age_str = (int $age/60/60/24/365);
665 $age_str .= " years ago";
666 } elsif ($age > 60*60*24*(365/12)*2) {
667 $age_str = int $age/60/60/24/(365/12);
668 $age_str .= " months ago";
669 } elsif ($age > 60*60*24*7*2) {
670 $age_str = int $age/60/60/24/7;
671 $age_str .= " weeks ago";
672 } elsif ($age > 60*60*24*2) {
673 $age_str = int $age/60/60/24;
674 $age_str .= " days ago";
675 } elsif ($age > 60*60*2) {
676 $age_str = int $age/60/60;
677 $age_str .= " hours ago";
678 } elsif ($age > 60*2) {
679 $age_str = int $age/60;
680 $age_str .= " min ago";
683 $age_str .= " sec ago";
685 $age_str .= " right now";
696 This library is free software. You can redistribute it and/or modify
697 it under the same terms as Perl itself.
701 __PACKAGE__->meta->make_immutable;