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 # info => $project->info,
130 log_lines => [$project->list_revs(
131 sha1 => $commit->sha1,
134 refs => $project->references,
135 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
142 The current list of heads (aka branches) in the repo.
147 my ( $self, $c ) = @_;
148 my $project = $c->stash->{Project};
150 commit => $self->_get_object($c),
151 heads => $project->heads,
158 The blob action i.e the contents of a file.
163 my ( $self, $c ) = @_;
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)->content,
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 my $commit = $self->_get_object($c, $c->req->param('hb'));
195 my $filename = $c->req->param('f')
196 || croak("No file specified!");
197 my($tree, $patch) = $c->stash->{Project}->diff(
199 parent => $c->req->param('hpb') || '',
206 # XXX Hack hack hack, see View::SyntaxHighlight
207 blobs => [$patch->[0]->{diff}],
209 action => 'blobdiff',
212 $c->forward('View::SyntaxHighlight');
217 Exposes a given commit.
222 my ( $self, $c ) = @_;
223 my $project = $c->stash->{Project};
224 my $commit = $self->_get_object($c);
227 diff_tree => ($project->diff(commit => $commit))[0],
228 refs => $project->references,
235 Exposes a given diff of a commit.
239 sub commitdiff : Local {
240 my ( $self, $c ) = @_;
241 my $commit = $self->_get_object($c);
242 my($tree, $patch) = $c->stash->{Project}->diff(
244 parent => $c->req->param('hp') || undef,
251 # XXX Hack hack hack, see View::SyntaxHighlight
252 blobs => [map $_->{diff}, @$patch],
254 action => 'commitdiff',
257 $c->forward('View::SyntaxHighlight');
262 Expose an abbreviated log of a given sha1.
266 sub shortlog : Local {
267 my ( $self, $c ) = @_;
268 my $project = $c->stash->{Project};
269 my $commit = $self->_get_object($c);
271 sha1 => $commit->sha1,
272 count => Gitalist->config->{paging}{log} || 25,
273 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
276 my $page = $c->req->param('pg') || 0;
277 $logargs{skip} = $c->req->param('pg') * $logargs{count}
278 if $c->req->param('pg');
282 log_lines => [$project->list_revs(%logargs)],
283 refs => $project->references,
284 action => 'shortlog',
291 Calls shortlog internally. Perhaps that should be reversed ...
295 $_[0]->shortlog($_[1]);
296 $_[1]->stash->{action} = 'log';
301 The tree of a given commit.
306 my ( $self, $c ) = @_;
307 my $project = $c->stash->{Project};
308 my $commit = $self->_get_object($c, $c->req->param('hb'));
309 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
313 tree_list => [$project->list_tree($tree->sha1)],
314 path => $c->req->param('f') || '',
321 Expose the local reflog. This may go away.
326 my ( $self, $c ) = @_;
327 my @log = $c->stash->{Project}->reflog(
339 $c->stash(current_action => 'GitRepos');
340 my $project = $c->stash->{Project};
341 my $commit = $self->_get_object($c);
342 # Lifted from /shortlog.
344 sha1 => $commit->sha1,
345 count => Gitalist->config->{paging}{log},
346 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
348 type => $c->req->param('type'),
349 text => $c->req->param('text'),
350 regexp => $c->req->param('regexp') || 0,
356 results => [$project->list_revs(%logargs)],
358 # This could be added - page => $page,
362 sub search_help : Local {
364 $c->stash(template => 'search_help.tt2');
368 # FIXME - implement atom
369 Carp::croak "Not implemented.";
373 # FIXME - implement rss
374 Carp::croak "Not implemented.";
377 sub blobdiff_plain : Local {
378 # FIXME - implement blobdiff_plain
379 Carp::croak "Not implemented.";
382 sub blob_plain : Local {
383 # FIXME - implement blobdiff_plain
384 Carp::croak "Not implemented.";
389 $c->detach('patches', [1]);
392 sub patches : Local {
393 my ($self, $c, $count) = @_;
394 $count ||= Gitalist->config->{patches}{max};
395 my $commit = $self->_get_object($c);
396 my $parent = $c->req->param('hp') || undef;
397 my $patch = $commit->get_patch( $parent, $count );
398 $c->response->body($patch);
399 $c->response->content_type('text/plain');
400 $c->response->status(200);
403 sub snapshot : Local {
404 # FIXME - implement snapshot
405 Carp::croak "Not implemented.";
408 sub commitdiff_plain : Local {
409 # FIXME - implement commitdiff_plain
410 Carp::croak "Not implemented.";
415 Populate the header and footer. Perhaps not the best location.
422 # XXX Move these to a plugin!
425 return 'never' unless $_[0];
426 return age_string(time - $_[0]->epoch);
430 my($line) = split /\n/, $cmt;
431 $line =~ s/^(.{70,80}\b).*/$1 …/;
434 abridged_description => sub {
435 join(' ', grep { defined } (split / /, shift)[0..10]);
439 # Yes, this is hideous.
444 # XXX This could probably be dropped altogether.
445 use Gitalist::Util qw(to_utf8);
446 # Formally git_header_html
450 my $title = $c->config->{sitename};
452 my $project = $c->req->param('project') || $c->req->param('p');
453 my $action = $c->req->param('action') || $c->req->param('a');
454 my $file_name = $c->req->param('filename') || $c->req->param('f');
455 if(defined $project) {
456 $title .= " - " . to_utf8($project);
457 if (defined $action) {
458 $title .= "/$action";
459 if (defined $file_name) {
460 $title .= " - " . $file_name;
461 if ($action eq "tree" && $file_name !~ m|/$|) {
468 $c->stash->{version} = $Gitalist::VERSION;
469 # check git's version by running it on the first project in the list.
470 $c->stash->{title} = $title;
472 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
474 $c->stash->{project} = $project;
477 my %href_params = $self->feed_info($c);
478 $href_params{'-title'} ||= 'log';
480 foreach my $format qw(RSS Atom) {
481 my $type = lc($format);
484 title => "$project - $href_params{'-title'} - $format feed",
486 # XXX A bit hacky and could do with using gitweb::href() features
487 href => "?a=$type;p=$project",
488 type => "application/$type+xml"
492 # XXX This duplication also feels a bit awkward
493 title => "$project - $href_params{'-title'} - $format feed (no merges)",
494 href => "?a=$type;p=$project;opt=--no-merges",
495 type => "application/$type+xml"
501 title => $c->config->{sitename}." projects list",
502 href => '?a=project_index',
503 type => "text/plain; charset=utf-8"
506 title => $c->config->{sitename}." projects feeds",
508 type => "text/plain; charset=utf-8"
512 $c->stash->{favicon} = $c->config->{favicon};
517 logo_url => $c->config->{logo_url},
518 logo_label => $c->config->{logo_label},
519 logo_img => $c->config->{logo},
520 home_link => $c->config->{home_link},
521 home_link_str => $c->config->{home_link_str},
524 if (defined $project) {
526 $c->stash(Project => $c->model('GitRepos')->project($project));
529 $c->detach('error_404');
532 search_text => ( $c->req->param('s') ||
533 $c->req->param('searchtext') || ''),
534 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
535 || $c->req->param('h') || $c->req->param('hash')
539 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
540 $c->stash->{git_version} = $a_project->run_cmd('--version');
543 # Formally git_footer_html
547 my $feed_class = 'rss_logo';
550 my $project = $c->req->param('project') || $c->req->param('p');
551 if(defined $project) {
552 (my $pstr = $project) =~ s[/?\.git$][];
553 my $descr = $c->stash->{project_description}
554 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
556 my %href_params = $self->feed_info($c);
558 $feed_class .= ' generic';
560 $href_params{'-title'} ||= 'log';
564 class => $feed_class,
565 title => "$href_params{'-title'} $_ feed",
566 href => "/?p=$project;a=\L$_",
573 class => $feed_class,
575 href => "/?a=$_->[0]",
577 }, [opml=>'OPML'],[project_index=>'TXT'],
582 # XXX This feels wrong here, should probably be refactored.
583 # returns hash to be passed to href to generate gitweb URL
584 # in -title key it returns description of link
588 my $format = shift || 'Atom';
589 my %res = (action => lc($format));
591 # feed links are possible only for project views
592 return unless $c->req->param('project');
594 # some views should link to OPML, or to generic project feed,
595 # or don't have specific feed yet (so they should use generic)
596 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
599 my $hash = $c->req->param('h') || $c->req->param('hash');
600 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
602 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
603 # from tag links; this also makes possible to detect branch links
604 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
605 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
609 # find log type for feed description (title)
611 my $file_name = $c->req->param('f') || $c->req->param('filename');
612 if (defined $file_name) {
613 $type = "history of $file_name";
614 $type .= "/" if $c->req->param('action') eq 'tree';
615 $type .= " on '$branch'" if (defined $branch);
617 $type = "log of $branch" if (defined $branch);
620 $res{-title} = $type;
621 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
622 $res{'file_name'} = $file_name;
629 Attempt to render a view, if needed.
633 sub end : ActionClass('RenderView') {
635 # Give project views the current HEAD.
636 if ($c->stash->{Project}) {
637 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
641 sub error_404 :Private {
643 $c->response->status(404);
645 title => 'Page not found',
646 content => 'Page not found',
654 if ($age > 60*60*24*365*2) {
655 $age_str = (int $age/60/60/24/365);
656 $age_str .= " years ago";
657 } elsif ($age > 60*60*24*(365/12)*2) {
658 $age_str = int $age/60/60/24/(365/12);
659 $age_str .= " months ago";
660 } elsif ($age > 60*60*24*7*2) {
661 $age_str = int $age/60/60/24/7;
662 $age_str .= " weeks ago";
663 } elsif ($age > 60*60*24*2) {
664 $age_str = int $age/60/60/24;
665 $age_str .= " days ago";
666 } elsif ($age > 60*60*2) {
667 $age_str = int $age/60/60;
668 $age_str .= " hours ago";
669 } elsif ($age > 60*2) {
670 $age_str = int $age/60;
671 $age_str .= " min ago";
674 $age_str .= " sec ago";
676 $age_str .= " right now";
687 This library is free software. You can redistribute it and/or modify
688 it under the same terms as Perl itself.
692 __PACKAGE__->meta->make_immutable;