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} = '';
13 use IO::Capture::Stdout;
20 Gitalist::Controller::Root - Root Controller for Gitalist
24 [enter your description here]
36 The C<gitweb> shim. It should now only be explicitly accessible by
42 my ( $self, $c ) = @_;
44 # XXX A slippery slope to be sure.
45 if($c->req->param('a')) {
46 my $capture = IO::Capture::Stdout->new();
49 my $action = gitweb::main($c);
58 my $output = join '', $capture->read;
59 $c->stash->{gitweb_output} = $output;
60 $c->stash->{template} = 'gitweb.tt2';
65 my($self, $c, $haveh) = @_;
67 my $h = $haveh || $c->req->param('h') || '';
68 my $f = $c->req->param('f');
70 my $m = $c->stash->{Project};
73 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
74 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
75 || ($f && $m->hash_by_path($f))
77 # XXX This could definitely use more context.
78 || Carp::croak("Couldn't find a hash for the commit object!");
80 my $commit = $m->get_object($hash)
81 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
88 Provides the project listing.
92 sub index :Path :Args(0) {
93 my ( $self, $c ) = @_;
95 $c->detach($c->req->param('a'))
96 if $c->req->param('a');
98 my @list = @{ $c->model()->projects };
99 die 'No projects found in '. $c->model->repo_dir
102 my $search = $c->req->param('s') || '';
105 index($_->name, $search) > -1
106 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
111 search_text => $search,
119 A summary of what's happening in the repo.
123 sub summary : Local {
124 my ( $self, $c ) = @_;
125 my $project = $c->stash->{Project};
126 $c->detach('error_404') unless $project;
127 my $commit = $self->_get_object($c);
128 my @heads = @{$project->heads};
129 my $maxitems = Gitalist->config->{paging}{summary} || 10;
132 log_lines => [$project->list_revs(
133 sha1 => $commit->sha1,
136 refs => $project->references,
137 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
144 The current list of heads (aka branches) in the repo.
149 my ( $self, $c ) = @_;
150 my $project = $c->stash->{Project};
152 commit => $self->_get_object($c),
153 heads => $project->heads,
160 The blob action i.e the contents of a file.
165 my ( $self, $c ) = @_;
166 my $project = $c->stash->{Project};
167 my $h = $c->req->param('h')
168 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
169 || die "No file or sha1 provided.";
170 my $hb = $c->req->param('hb')
171 || $project->head_hash
172 || die "Couldn't discern the corresponding head.";
174 my $filename = $c->req->param('f') || '';
177 blob => $project->get_object($h)->content,
178 head => $project->get_object($hb),
179 filename => $filename,
180 # XXX Hack hack hack, see View::SyntaxHighlight
181 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
185 $c->forward('View::SyntaxHighlight')
186 unless $c->stash->{no_wrapper};
189 sub blob_plain : Local {
192 $c->stash(no_wrapper => 1);
193 $c->response->content_type('text/plain; charset=utf-8');
198 sub blobdiff_plain : Local {
201 $c->stash(no_wrapper => 1);
202 $c->response->content_type('text/plain; charset=utf-8');
204 $c->forward('blobdiff');
210 Exposes a given diff of a blob.
214 sub blobdiff : Local {
215 my ( $self, $c ) = @_;
216 my $commit = $self->_get_object($c, $c->req->param('hb'));
217 my $filename = $c->req->param('f')
218 || croak("No file specified!");
219 my($tree, $patch) = $c->stash->{Project}->diff(
222 parent => $c->req->param('hpb') || undef,
228 # XXX Hack hack hack, see View::SyntaxHighlight
229 blobs => [$patch->[0]->{diff}],
231 action => 'blobdiff',
234 $c->forward('View::SyntaxHighlight')
235 unless $c->stash->{no_wrapper};
240 Exposes a given commit.
245 my ( $self, $c ) = @_;
246 my $project = $c->stash->{Project};
247 my $commit = $self->_get_object($c);
250 diff_tree => ($project->diff(commit => $commit))[0],
251 refs => $project->references,
258 Exposes a given diff of a commit.
262 sub commitdiff : Local {
263 my ( $self, $c ) = @_;
264 my $commit = $self->_get_object($c);
265 my($tree, $patch) = $c->stash->{Project}->diff(
267 parent => $c->req->param('hp') || undef,
274 # XXX Hack hack hack, see View::SyntaxHighlight
275 blobs => [map $_->{diff}, @$patch],
277 action => 'commitdiff',
280 $c->forward('View::SyntaxHighlight')
281 unless $c->stash->{no_wrapper};
284 sub commitdiff_plain : Local {
287 $c->stash(no_wrapper => 1);
288 $c->response->content_type('text/plain; charset=utf-8');
290 $c->forward('commitdiff');
295 Expose an abbreviated log of a given sha1.
299 sub shortlog : Local {
300 my ( $self, $c ) = @_;
301 my $project = $c->stash->{Project};
302 my $commit = $self->_get_object($c);
304 sha1 => $commit->sha1,
305 count => Gitalist->config->{paging}{log} || 25,
306 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
309 my $page = $c->req->param('pg') || 0;
310 $logargs{skip} = $c->req->param('pg') * $logargs{count}
311 if $c->req->param('pg');
315 log_lines => [$project->list_revs(%logargs)],
316 refs => $project->references,
317 action => 'shortlog',
324 Calls shortlog internally. Perhaps that should be reversed ...
328 $_[0]->shortlog($_[1]);
329 $_[1]->stash->{action} = 'log';
332 # For legacy support.
333 sub history : Local {
334 $_[0]->shortlog(@_[1 .. $#_]);
339 The tree of a given commit.
344 my ( $self, $c ) = @_;
345 my $project = $c->stash->{Project};
346 my $commit = $self->_get_object($c, $c->req->param('hb'));
347 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
351 tree_list => [$project->list_tree($tree->sha1)],
352 path => $c->req->param('f') || '',
359 Expose the local reflog. This may go away.
364 my ( $self, $c ) = @_;
365 my @log = $c->stash->{Project}->reflog(
377 $c->stash(current_action => 'GitRepos');
378 my $project = $c->stash->{Project};
379 my $commit = $self->_get_object($c);
380 # Lifted from /shortlog.
382 sha1 => $commit->sha1,
383 count => Gitalist->config->{paging}{log},
384 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
386 type => $c->req->param('type'),
387 text => $c->req->param('text'),
388 regexp => $c->req->param('regexp') || 0,
394 results => [$project->list_revs(%logargs)],
396 # This could be added - page => $page,
400 sub search_help : Local {
402 $c->stash(template => 'search_help.tt2');
408 my $feed = XML::Atom::Feed->new;
410 my $host = lc Sys::Hostname::hostname();
411 $feed->title($host . ' - ' . Gitalist->config->{name});
412 $feed->updated(~~DateTime->now);
414 my $project = $c->stash->{Project};
416 sha1 => $project->head_hash,
417 count => Gitalist->config->{paging}{log} || 25,
418 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
421 my $mk_title = $c->stash->{short_cmt};
422 for my $commit ($project->list_revs(%logargs)) {
423 my $entry = XML::Atom::Entry->new;
424 $entry->title( $mk_title->($commit->comment) );
425 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
427 $entry->content($commit->comment);
428 $feed->add_entry($entry);
432 feed => $feed->as_xml,
435 $c->response->content_type('application/atom+xml')
439 # FIXME - implement rss
440 Carp::croak "Not implemented.";
445 $c->detach('patches', [1]);
448 sub patches : Local {
449 my ($self, $c, $count) = @_;
450 $count ||= Gitalist->config->{patches}{max};
451 my $commit = $self->_get_object($c);
452 my $parent = $c->req->param('hp') || undef;
453 my $patch = $commit->get_patch( $parent, $count );
454 $c->response->body($patch);
455 $c->response->content_type('text/plain');
456 $c->response->status(200);
459 sub snapshot : Local {
461 my $format = $c->req->param('snapshot_format') || 'tar';
463 my $commit = $self->_get_object($c);
464 $c->response->status(200);
465 $c->response->headers->header( 'Content-Disposition' =>
466 'attachment; filename=export.tgz');
468 $c->response->body($commit->snapshot($format));
473 Populate the header and footer. Perhaps not the best location.
480 # XXX Move these to a plugin!
483 return 'never' unless $_[0];
484 return age_string(time - $_[0]->epoch);
488 my($line) = split /\n/, $cmt;
489 $line =~ s/^(.{70,80}\b).*/$1 …/;
492 abridged_description => sub {
493 join(' ', grep { defined } (split / /, shift)[0..10]);
497 # Yes, this is hideous.
502 # XXX This could probably be dropped altogether.
503 use Gitalist::Util qw(to_utf8);
504 # Formally git_header_html
508 my $title = $c->config->{sitename};
510 my $project = $c->req->param('project') || $c->req->param('p');
511 my $action = $c->req->param('action') || $c->req->param('a');
512 my $file_name = $c->req->param('filename') || $c->req->param('f');
513 if(defined $project) {
514 $title .= " - " . to_utf8($project);
515 if (defined $action) {
516 $title .= "/$action";
517 if (defined $file_name) {
518 $title .= " - " . $file_name;
519 if ($action eq "tree" && $file_name !~ m|/$|) {
526 $c->stash->{version} = $Gitalist::VERSION;
527 # check git's version by running it on the first project in the list.
528 $c->stash->{title} = $title;
530 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
532 $c->stash->{project} = $project;
535 my %href_params = $self->feed_info($c);
536 $href_params{'-title'} ||= 'log';
538 foreach my $format qw(RSS Atom) {
539 my $type = lc($format);
542 title => "$project - $href_params{'-title'} - $format feed",
544 # XXX A bit hacky and could do with using gitweb::href() features
545 href => "?a=$type;p=$project",
546 type => "application/$type+xml"
550 # XXX This duplication also feels a bit awkward
551 title => "$project - $href_params{'-title'} - $format feed (no merges)",
552 href => "?a=$type;p=$project;opt=--no-merges",
553 type => "application/$type+xml"
559 title => $c->config->{sitename}." projects list",
560 href => '?a=project_index',
561 type => "text/plain; charset=utf-8"
564 title => $c->config->{sitename}." projects feeds",
566 type => "text/plain; charset=utf-8"
570 $c->stash->{favicon} = $c->config->{favicon};
575 logo_url => $c->config->{logo_url},
576 logo_label => $c->config->{logo_label},
577 logo_img => $c->config->{logo},
578 home_link => $c->config->{home_link},
579 home_link_str => $c->config->{home_link_str},
582 if (defined $project) {
584 $c->stash(Project => $c->model('GitRepos')->project($project));
587 $c->detach('error_404');
590 search_text => ( $c->req->param('s') ||
591 $c->req->param('searchtext') || ''),
592 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
593 || $c->req->param('h') || $c->req->param('hash')
597 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
598 $c->stash->{git_version} = $a_project->run_cmd('--version');
601 # Formally git_footer_html
605 my $feed_class = 'rss_logo';
608 my $project = $c->req->param('project') || $c->req->param('p');
609 if(defined $project) {
610 (my $pstr = $project) =~ s[/?\.git$][];
611 my $descr = $c->stash->{project_description}
612 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
614 my %href_params = $self->feed_info($c);
616 $feed_class .= ' generic';
618 $href_params{'-title'} ||= 'log';
622 class => $feed_class,
623 title => "$href_params{'-title'} $_ feed",
624 href => "/?p=$project;a=\L$_",
631 class => $feed_class,
633 href => "/?a=$_->[0]",
635 }, [opml=>'OPML'],[project_index=>'TXT'],
640 # XXX This feels wrong here, should probably be refactored.
641 # returns hash to be passed to href to generate gitweb URL
642 # in -title key it returns description of link
646 my $format = shift || 'Atom';
647 my %res = (action => lc($format));
649 # feed links are possible only for project views
650 return unless $c->req->param('project');
652 # some views should link to OPML, or to generic project feed,
653 # or don't have specific feed yet (so they should use generic)
654 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
657 my $hash = $c->req->param('h') || $c->req->param('hash');
658 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
660 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
661 # from tag links; this also makes possible to detect branch links
662 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
663 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
667 # find log type for feed description (title)
669 my $file_name = $c->req->param('f') || $c->req->param('filename');
670 if (defined $file_name) {
671 $type = "history of $file_name";
672 $type .= "/" if $c->req->param('action') eq 'tree';
673 $type .= " on '$branch'" if (defined $branch);
675 $type = "log of $branch" if (defined $branch);
678 $res{-title} = $type;
679 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
680 $res{'file_name'} = $file_name;
687 Attempt to render a view, if needed.
691 sub end : ActionClass('RenderView') {
693 # Give project views the current HEAD.
694 if ($c->stash->{Project}) {
695 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
699 sub error_404 :Private {
701 $c->response->status(404);
703 title => 'Page not found',
704 content => 'Page not found',
712 if ($age > 60*60*24*365*2) {
713 $age_str = (int $age/60/60/24/365);
714 $age_str .= " years ago";
715 } elsif ($age > 60*60*24*(365/12)*2) {
716 $age_str = int $age/60/60/24/(365/12);
717 $age_str .= " months ago";
718 } elsif ($age > 60*60*24*7*2) {
719 $age_str = int $age/60/60/24/7;
720 $age_str .= " weeks ago";
721 } elsif ($age > 60*60*24*2) {
722 $age_str = int $age/60/60/24;
723 $age_str .= " days ago";
724 } elsif ($age > 60*60*2) {
725 $age_str = int $age/60/60;
726 $age_str .= " hours ago";
727 } elsif ($age > 60*2) {
728 $age_str = int $age/60;
729 $age_str .= " min ago";
732 $age_str .= " sec ago";
734 $age_str .= " right now";
745 This library is free software. You can redistribute it and/or modify
746 it under the same terms as Perl itself.
750 __PACKAGE__->meta->make_immutable;