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;
21 Gitalist::Controller::Root - Root Controller for Gitalist
25 [enter your description here]
37 The C<gitweb> shim. It should now only be explicitly accessible by
43 my ( $self, $c ) = @_;
45 # XXX A slippery slope to be sure.
46 if($c->req->param('a')) {
47 my $capture = IO::Capture::Stdout->new();
50 my $action = gitweb::main($c);
59 my $output = join '', $capture->read;
60 $c->stash->{gitweb_output} = $output;
61 $c->stash->{template} = 'gitweb.tt2';
66 my($self, $c, $haveh) = @_;
68 my $h = $haveh || $c->req->param('h') || '';
69 my $f = $c->req->param('f');
71 my $m = $c->stash->{Project};
74 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
75 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
76 || ($f && $m->hash_by_path($f))
78 # XXX This could definitely use more context.
79 || Carp::croak("Couldn't find a hash for the commit object!");
81 my $commit = $m->get_object($hash)
82 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
89 Provides the project listing.
93 sub index :Path :Args(0) {
94 my ( $self, $c ) = @_;
96 $c->detach($c->req->param('a'))
97 if $c->req->param('a');
99 my @list = @{ $c->model()->projects };
100 die 'No projects found in '. $c->model->repo_dir
103 my $search = $c->req->param('s') || '';
106 index($_->name, $search) > -1
107 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
112 search_text => $search,
120 A summary of what's happening in the repo.
124 sub summary : Local {
125 my ( $self, $c ) = @_;
126 my $project = $c->stash->{Project};
127 $c->detach('error_404') unless $project;
128 my $commit = $self->_get_object($c);
129 my @heads = @{$project->heads};
130 my $maxitems = Gitalist->config->{paging}{summary} || 10;
133 log_lines => [$project->list_revs(
134 sha1 => $commit->sha1,
137 refs => $project->references,
138 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
145 The current list of heads (aka branches) in the repo.
150 my ( $self, $c ) = @_;
151 my $project = $c->stash->{Project};
153 commit => $self->_get_object($c),
154 heads => $project->heads,
161 The blob action i.e the contents of a file.
166 my ( $self, $c ) = @_;
167 my $project = $c->stash->{Project};
168 my $h = $c->req->param('h')
169 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
170 || die "No file or sha1 provided.";
171 my $hb = $c->req->param('hb')
172 || $project->head_hash
173 || die "Couldn't discern the corresponding head.";
175 my $filename = $c->req->param('f') || '';
178 blob => $project->get_object($h)->content,
179 head => $project->get_object($hb),
180 filename => $filename,
181 # XXX Hack hack hack, see View::SyntaxHighlight
182 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
186 $c->forward('View::SyntaxHighlight')
187 unless $c->stash->{no_wrapper};
190 sub blob_plain : Local {
193 $c->stash(no_wrapper => 1);
194 $c->response->content_type('text/plain; charset=utf-8');
199 sub blobdiff_plain : Local {
202 $c->stash(no_wrapper => 1);
203 $c->response->content_type('text/plain; charset=utf-8');
205 $c->forward('blobdiff');
211 Exposes a given diff of a blob.
215 sub blobdiff : Local {
216 my ( $self, $c ) = @_;
217 my $commit = $self->_get_object($c, $c->req->param('hb'));
218 my $filename = $c->req->param('f')
219 || croak("No file specified!");
220 my($tree, $patch) = $c->stash->{Project}->diff(
223 parent => $c->req->param('hpb') || undef,
229 # XXX Hack hack hack, see View::SyntaxHighlight
230 blobs => [$patch->[0]->{diff}],
232 action => 'blobdiff',
235 $c->forward('View::SyntaxHighlight')
236 unless $c->stash->{no_wrapper};
241 Exposes a given commit.
246 my ( $self, $c ) = @_;
247 my $project = $c->stash->{Project};
248 my $commit = $self->_get_object($c);
251 diff_tree => ($project->diff(commit => $commit))[0],
252 refs => $project->references,
259 Exposes a given diff of a commit.
263 sub commitdiff : Local {
264 my ( $self, $c ) = @_;
265 my $commit = $self->_get_object($c);
266 my($tree, $patch) = $c->stash->{Project}->diff(
268 parent => $c->req->param('hp') || undef,
275 # XXX Hack hack hack, see View::SyntaxHighlight
276 blobs => [map $_->{diff}, @$patch],
278 action => 'commitdiff',
281 $c->forward('View::SyntaxHighlight')
282 unless $c->stash->{no_wrapper};
285 sub commitdiff_plain : Local {
288 $c->stash(no_wrapper => 1);
289 $c->response->content_type('text/plain; charset=utf-8');
291 $c->forward('commitdiff');
296 Expose an abbreviated log of a given sha1.
300 sub shortlog : Local {
301 my ( $self, $c ) = @_;
302 my $project = $c->stash->{Project};
303 my $commit = $self->_get_object($c);
305 sha1 => $commit->sha1,
306 count => Gitalist->config->{paging}{log} || 25,
307 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
310 my $page = $c->req->param('pg') || 0;
311 $logargs{skip} = $c->req->param('pg') * $logargs{count}
312 if $c->req->param('pg');
316 log_lines => [$project->list_revs(%logargs)],
317 refs => $project->references,
318 action => 'shortlog',
325 Calls shortlog internally. Perhaps that should be reversed ...
329 $_[0]->shortlog($_[1]);
330 $_[1]->stash->{action} = 'log';
333 # For legacy support.
334 sub history : Local {
335 $_[0]->shortlog(@_[1 .. $#_]);
340 The tree of a given commit.
345 my ( $self, $c ) = @_;
346 my $project = $c->stash->{Project};
347 my $commit = $self->_get_object($c, $c->req->param('hb'));
348 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
352 tree_list => [$project->list_tree($tree->sha1)],
353 path => $c->req->param('f') || '',
360 Expose the local reflog. This may go away.
365 my ( $self, $c ) = @_;
366 my @log = $c->stash->{Project}->reflog(
378 $c->stash(current_action => 'GitRepos');
379 my $project = $c->stash->{Project};
380 my $commit = $self->_get_object($c);
381 # Lifted from /shortlog.
383 sha1 => $commit->sha1,
384 count => Gitalist->config->{paging}{log},
385 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
387 type => $c->req->param('type'),
388 text => $c->req->param('text'),
389 regexp => $c->req->param('regexp') || 0,
395 results => [$project->list_revs(%logargs)],
397 # This could be added - page => $page,
401 sub search_help : Local {
403 $c->stash(template => 'search_help.tt2');
409 my $feed = XML::Atom::Feed->new;
411 my $host = lc Sys::Hostname::hostname();
412 $feed->title($host . ' - ' . Gitalist->config->{name});
413 $feed->updated(~~DateTime->now);
415 my $project = $c->stash->{Project};
417 sha1 => $project->head_hash,
418 count => Gitalist->config->{paging}{log} || 25,
419 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
422 my $mk_title = $c->stash->{short_cmt};
423 for my $commit ($project->list_revs(%logargs)) {
424 my $entry = XML::Atom::Entry->new;
425 $entry->title( $mk_title->($commit->comment) );
426 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
428 $entry->content($commit->comment);
429 $feed->add_entry($entry);
432 $c->response->body($feed->as_xml);
433 $c->response->content_type('application/atom+xml');
434 $c->response->status(200);
440 my $project = $c->stash->{Project};
442 my $rss = XML::RSS->new(version => '2.0');
444 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
445 link => $c->uri_for('summary', {p=>$project->name}),
447 description => $project->description,
448 pubDate => DateTime->now,
449 lastBuildDate => DateTime->now,
453 sha1 => $project->head_hash,
454 count => Gitalist->config->{paging}{log} || 25,
455 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
457 my $mk_title = $c->stash->{short_cmt};
458 for my $commit ($project->list_revs(%logargs)) {
459 # XXX Needs work ....
461 title => $mk_title->($commit->comment),
462 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
463 description => $commit->comment,
467 $c->response->body($rss->as_string);
468 $c->response->content_type('application/rss+xml');
469 $c->response->status(200);
474 $c->detach('patches', [1]);
477 sub patches : Local {
478 my ($self, $c, $count) = @_;
479 $count ||= Gitalist->config->{patches}{max};
480 my $commit = $self->_get_object($c);
481 my $parent = $c->req->param('hp') || undef;
482 my $patch = $commit->get_patch( $parent, $count );
483 $c->response->body($patch);
484 $c->response->content_type('text/plain');
485 $c->response->status(200);
488 sub snapshot : Local {
489 # FIXME - implement snapshot
490 Carp::croak "Not implemented.";
495 Populate the header and footer. Perhaps not the best location.
502 # XXX Move these to a plugin!
505 return 'never' unless $_[0];
506 return age_string(time - $_[0]->epoch);
510 my($line) = split /\n/, $cmt;
511 $line =~ s/^(.{70,80}\b).*/$1 …/;
514 abridged_description => sub {
515 join(' ', grep { defined } (split / /, shift)[0..10]);
519 # Yes, this is hideous.
524 # XXX This could probably be dropped altogether.
525 use Gitalist::Util qw(to_utf8);
526 # Formally git_header_html
530 my $title = $c->config->{sitename};
532 my $project = $c->req->param('project') || $c->req->param('p');
533 my $action = $c->req->param('action') || $c->req->param('a');
534 my $file_name = $c->req->param('filename') || $c->req->param('f');
535 if(defined $project) {
536 $title .= " - " . to_utf8($project);
537 if (defined $action) {
538 $title .= "/$action";
539 if (defined $file_name) {
540 $title .= " - " . $file_name;
541 if ($action eq "tree" && $file_name !~ m|/$|) {
548 $c->stash->{version} = $Gitalist::VERSION;
549 # check git's version by running it on the first project in the list.
550 $c->stash->{title} = $title;
552 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
554 $c->stash->{project} = $project;
557 my %href_params = $self->feed_info($c);
558 $href_params{'-title'} ||= 'log';
560 foreach my $format qw(RSS Atom) {
561 my $type = lc($format);
564 title => "$project - $href_params{'-title'} - $format feed",
566 # XXX A bit hacky and could do with using gitweb::href() features
567 href => "?a=$type;p=$project",
568 type => "application/$type+xml"
572 # XXX This duplication also feels a bit awkward
573 title => "$project - $href_params{'-title'} - $format feed (no merges)",
574 href => "?a=$type;p=$project;opt=--no-merges",
575 type => "application/$type+xml"
581 title => $c->config->{sitename}." projects list",
582 href => '?a=project_index',
583 type => "text/plain; charset=utf-8"
586 title => $c->config->{sitename}." projects feeds",
588 type => "text/plain; charset=utf-8"
592 $c->stash->{favicon} = $c->config->{favicon};
597 logo_url => $c->config->{logo_url},
598 logo_label => $c->config->{logo_label},
599 logo_img => $c->config->{logo},
600 home_link => $c->config->{home_link},
601 home_link_str => $c->config->{home_link_str},
604 if (defined $project) {
606 $c->stash(Project => $c->model('GitRepos')->project($project));
609 $c->detach('error_404');
612 search_text => ( $c->req->param('s') ||
613 $c->req->param('searchtext') || ''),
614 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
615 || $c->req->param('h') || $c->req->param('hash')
619 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
620 $c->stash->{git_version} = $a_project->run_cmd('--version');
623 # Formally git_footer_html
627 my $feed_class = 'rss_logo';
630 my $project = $c->req->param('project') || $c->req->param('p');
631 if(defined $project) {
632 (my $pstr = $project) =~ s[/?\.git$][];
633 my $descr = $c->stash->{project_description}
634 = $c->stash->{Project} ? $c->stash->{Project}->description : '';
636 my %href_params = $self->feed_info($c);
638 $feed_class .= ' generic';
640 $href_params{'-title'} ||= 'log';
644 class => $feed_class,
645 title => "$href_params{'-title'} $_ feed",
646 href => "/?p=$project;a=\L$_",
653 class => $feed_class,
655 href => "/?a=$_->[0]",
657 }, [opml=>'OPML'],[project_index=>'TXT'],
662 # XXX This feels wrong here, should probably be refactored.
663 # returns hash to be passed to href to generate gitweb URL
664 # in -title key it returns description of link
668 my $format = shift || 'Atom';
669 my %res = (action => lc($format));
671 # feed links are possible only for project views
672 return unless $c->req->param('project');
674 # some views should link to OPML, or to generic project feed,
675 # or don't have specific feed yet (so they should use generic)
676 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
679 my $hash = $c->req->param('h') || $c->req->param('hash');
680 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
682 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
683 # from tag links; this also makes possible to detect branch links
684 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
685 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
689 # find log type for feed description (title)
691 my $file_name = $c->req->param('f') || $c->req->param('filename');
692 if (defined $file_name) {
693 $type = "history of $file_name";
694 $type .= "/" if $c->req->param('action') eq 'tree';
695 $type .= " on '$branch'" if (defined $branch);
697 $type = "log of $branch" if (defined $branch);
700 $res{-title} = $type;
701 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
702 $res{'file_name'} = $file_name;
709 Attempt to render a view, if needed.
713 sub end : ActionClass('RenderView') {
715 # Give project views the current HEAD.
716 if ($c->stash->{Project}) {
717 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
721 sub error_404 :Private {
723 $c->response->status(404);
725 title => 'Page not found',
726 content => 'Page not found',
734 if ($age > 60*60*24*365*2) {
735 $age_str = (int $age/60/60/24/365);
736 $age_str .= " years ago";
737 } elsif ($age > 60*60*24*(365/12)*2) {
738 $age_str = int $age/60/60/24/(365/12);
739 $age_str .= " months ago";
740 } elsif ($age > 60*60*24*7*2) {
741 $age_str = int $age/60/60/24/7;
742 $age_str .= " weeks ago";
743 } elsif ($age > 60*60*24*2) {
744 $age_str = int $age/60/60/24;
745 $age_str .= " days ago";
746 } elsif ($age > 60*60*2) {
747 $age_str = int $age/60/60;
748 $age_str .= " hours ago";
749 } elsif ($age > 60*2) {
750 $age_str = int $age/60;
751 $age_str .= " min ago";
754 $age_str .= " sec ago";
756 $age_str .= " right now";
767 This library is free software. You can redistribute it and/or modify
768 it under the same terms as Perl itself.
772 __PACKAGE__->meta->make_immutable;