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 ) = @_;
164 my $h = $c->req->param('h')
165 || $c->model()->hash_by_path($c->req->param('f'))
166 || die "No file or sha1 provided.";
167 my $hb = $c->req->param('hb')
168 || $c->model()->head_hash
169 || die "Couldn't discern the corresponding head.";
171 my $filename = $c->req->param('f') || '';
174 blob => $c->model()->get_object($h)->content,
175 head => $c->model()->get_object($hb),
176 filename => $filename,
177 # XXX Hack hack hack, see View::SyntaxHighlight
178 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
182 $c->forward('View::SyntaxHighlight');
187 Exposes a given diff of a blob.
191 sub blobdiff : Local {
192 my ( $self, $c ) = @_;
194 my $commit = $self->_get_commit($c);
195 my $filename = $c->req->param('f')
196 || croak("No file specified!");
197 my($tree, $patch) = $c->model()->diff(
199 parent => $c->req->param('hp') || '',
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 $c->stash(current_model => 'GitRepos');
224 my $project = $c->stash->{Project};
225 my $commit = $self->_get_commit($c);
228 diff_tree => ($project->diff(commit => $commit))[0],
229 refs => $project->references,
236 Exposes a given diff of a commit.
240 sub commitdiff : Local {
241 my ( $self, $c ) = @_;
243 my $commit = $self->_get_commit($c);
244 my($tree, $patch) = $c->model()->diff(
246 parent => $c->req->param('hp') || '',
253 # XXX Hack hack hack, see View::SyntaxHighlight
254 blobs => [map $_->{diff}, @$patch],
256 action => 'commitdiff',
259 $c->forward('View::SyntaxHighlight');
264 Expose an abbreviated log of a given sha1.
268 sub shortlog : Local {
269 my ( $self, $c ) = @_;
270 $c->stash(current_model => 'GitRepos');
271 my $project = $c->stash->{Project};
272 my $commit = $self->_get_commit($c);
274 sha1 => $commit->sha1,
275 count => Gitalist->config->{paging}{log} || 25,
276 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
279 my $page = $c->req->param('pg') || 0;
280 $logargs{skip} = $c->req->param('pg') * $logargs{count}
281 if $c->req->param('pg');
285 log_lines => [$project->list_revs(%logargs)],
286 refs => $project->references,
287 action => 'shortlog',
294 Calls shortlog internally. Perhaps that should be reversed ...
298 $_[0]->shortlog($_[1]);
299 $_[1]->stash->{action} = 'log';
304 The tree of a given commit.
309 my ( $self, $c ) = @_;
311 my $commit = $self->_get_commit($c, $c->req->param('hb'));
312 my $tree = $c->model()->get_object($c->req->param('h') || $commit->tree_sha1);
314 # XXX Useful defaults needed ...
317 tree_list => [$c->model()->list_tree($tree->sha1)],
318 path => $c->req->param('f') || '',
325 Expose the local reflog. This may go away.
330 my ( $self, $c ) = @_;
332 my @log = $c->model()->reflog(
345 my $commit = $self->_get_commit($c);
346 # Lifted from /shortlog.
348 sha1 => $commit->sha1,
349 count => Gitalist->config->{paging}{log},
350 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
352 type => $c->req->param('type'),
353 text => $c->req->param('text'),
354 regexp => $c->req->param('regexp') || 0,
360 results => [$c->model()->list_revs(%logargs)],
362 # This could be added - page => $page,
366 sub search_help : Local {
367 Carp::croak "Not implemented.";
372 Populate the header and footer. Perhaps not the best location.
379 # XXX Move these to a plugin!
382 return 'never' unless $_[0];
383 return age_string(time - $_[0]->epoch);
387 my($line) = split /\n/, $cmt;
388 $line =~ s/^(.{70,80}\b).*/$1 …/;
391 abridged_description => sub {
392 join(' ', grep { defined } (split / /, shift)[0..10]);
396 # Yes, this is hideous.
401 # XXX This could probably be dropped altogether.
402 use Gitalist::Util qw(to_utf8);
403 # Formally git_header_html
407 my $title = $c->config->{sitename};
409 my $project = $c->req->param('project') || $c->req->param('p');
410 my $action = $c->req->param('action') || $c->req->param('a');
411 my $file_name = $c->req->param('filename') || $c->req->param('f');
412 if(defined $project) {
413 $title .= " - " . to_utf8($project);
414 if (defined $action) {
415 $title .= "/$action";
416 if (defined $file_name) {
417 $title .= " - " . $file_name;
418 if ($action eq "tree" && $file_name !~ m|/$|) {
425 $c->stash->{version} = $c->config->{version};
426 $c->stash->{git_version} = $c->model()->run_cmd('--version');
427 $c->stash->{title} = $title;
429 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
430 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
432 $c->stash->{project} = $project;
435 my %href_params = $self->feed_info($c);
436 $href_params{'-title'} ||= 'log';
438 foreach my $format qw(RSS Atom) {
439 my $type = lc($format);
442 title => "$project - $href_params{'-title'} - $format feed",
444 # XXX A bit hacky and could do with using gitweb::href() features
445 href => "?a=$type;p=$project",
446 type => "application/$type+xml"
450 # XXX This duplication also feels a bit awkward
451 title => "$project - $href_params{'-title'} - $format feed (no merges)",
452 href => "?a=$type;p=$project;opt=--no-merges",
453 type => "application/$type+xml"
459 title => $c->config->{sitename}." projects list",
460 href => '?a=project_index',
461 type => "text/plain; charset=utf-8"
464 title => $c->config->{sitename}." projects feeds",
466 type => "text/plain; charset=utf-8"
470 $c->stash->{favicon} = $c->config->{favicon};
475 logo_url => $c->config->{logo_url},
476 logo_label => $c->config->{logo_label},
477 logo_img => $c->config->{logo},
478 home_link => $c->config->{home_link},
479 home_link_str => $c->config->{home_link_str},
482 if(defined $project) {
484 search_text => ( $c->req->param('s') ||
485 $c->req->param('searchtext') || ''),
486 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
487 || $c->req->param('h') || $c->req->param('hash')
489 Project => $c->model('GitRepos')->project($project),
494 # Formally git_footer_html
498 my $feed_class = 'rss_logo';
501 my $project = $c->req->param('project') || $c->req->param('p');
502 if(defined $project) {
503 (my $pstr = $project) =~ s[/?\.git$][];
504 my $descr = $c->model()->project_info($project)->{description};
505 $c->stash->{project_description} = defined $descr
509 my %href_params = $self->feed_info($c);
511 $feed_class .= ' generic';
513 $href_params{'-title'} ||= 'log';
517 class => $feed_class,
518 title => "$href_params{'-title'} $_ feed",
519 href => "/?p=$project;a=\L$_",
526 class => $feed_class,
528 href => "/?a=$_->[0]",
530 }, [opml=>'OPML'],[project_index=>'TXT'],
535 # XXX This feels wrong here, should probably be refactored.
536 # returns hash to be passed to href to generate gitweb URL
537 # in -title key it returns description of link
541 my $format = shift || 'Atom';
542 my %res = (action => lc($format));
544 # feed links are possible only for project views
545 return unless $c->req->param('project');
547 # some views should link to OPML, or to generic project feed,
548 # or don't have specific feed yet (so they should use generic)
549 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
552 my $hash = $c->req->param('h') || $c->req->param('hash');
553 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
555 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
556 # from tag links; this also makes possible to detect branch links
557 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
558 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
562 # find log type for feed description (title)
564 my $file_name = $c->req->param('f') || $c->req->param('filename');
565 if (defined $file_name) {
566 $type = "history of $file_name";
567 $type .= "/" if $c->req->param('action') eq 'tree';
568 $type .= " on '$branch'" if (defined $branch);
570 $type = "log of $branch" if (defined $branch);
573 $res{-title} = $type;
574 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
575 $res{'file_name'} = $file_name;
582 Attempt to render a view, if needed.
586 sub end : ActionClass('RenderView') {
588 # Give project views the current HEAD.
589 if ($c->stash->{project}) {
590 if ($c->stash->{current_model} &&
591 $c->stash->{current_model} eq 'GitRepos') {
592 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
594 $c->stash->{HEAD} = $c->model()->head_hash;
599 sub error_404 :Private {
601 $c->response->status(404);
603 title => 'Page not found',
604 content => 'Page not found',
612 if ($age > 60*60*24*365*2) {
613 $age_str = (int $age/60/60/24/365);
614 $age_str .= " years ago";
615 } elsif ($age > 60*60*24*(365/12)*2) {
616 $age_str = int $age/60/60/24/(365/12);
617 $age_str .= " months ago";
618 } elsif ($age > 60*60*24*7*2) {
619 $age_str = int $age/60/60/24/7;
620 $age_str .= " weeks ago";
621 } elsif ($age > 60*60*24*2) {
622 $age_str = int $age/60/60/24;
623 $age_str .= " days ago";
624 } elsif ($age > 60*60*2) {
625 $age_str = int $age/60/60;
626 $age_str .= " hours ago";
627 } elsif ($age > 60*2) {
628 $age_str = int $age/60;
629 $age_str .= " min ago";
632 $age_str .= " sec ago";
634 $age_str .= " right now";
645 This library is free software. You can redistribute it and/or modify
646 it under the same terms as Perl itself.
650 __PACKAGE__->meta->make_immutable;