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');
68 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
69 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
70 || ($f && $m->hash_by_path($f))
72 # XXX This could definitely use more context.
73 || Carp::croak("Couldn't find a hash for the commit object!");
76 (my $pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
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 ) = @_;
91 $c->stash(current_model => 'GitRepos');
92 # Leave actions up to gitweb at this point.
93 return $self->run_gitweb($c)
94 if $c->req->param('a');
96 my $list = $c->model()->list_projects;
98 die "No projects found in ". $c->model->repo_dir;
102 searchtext => $c->req->param('searchtext') || '',
110 A summary of what's happening in the repo.
114 sub summary : Local {
115 my ( $self, $c ) = @_;
117 my $commit = $self->_get_commit($c);
120 info => $c->model()->project_info($c->model()->project),
121 log_lines => [$c->model()->list_revs(
122 sha1 => $commit->sha1, count => Gitalist->config->{paging}{summary}
124 refs => $c->model()->references,
125 heads => [$c->model()->heads],
132 The current list of heads (aka branches) in the repo.
137 my ( $self, $c ) = @_;
140 commit => $self->_get_commit($c),
141 heads => [$c->model()->heads],
148 The blob action i.e the contents of a file.
153 my ( $self, $c ) = @_;
155 my $h = $c->req->param('h')
156 || $c->model()->hash_by_path($c->req->param('f'))
157 || die "No file or sha1 provided.";
158 my $hb = $c->req->param('hb')
159 || $c->model()->head_hash
160 || die "Couldn't discern the corresponding head.";
162 my $filename = $c->req->param('f') || '';
165 blob => $c->model()->get_object($h)->content,
166 head => $c->model()->get_object($hb),
167 filename => $filename,
168 # XXX Hack hack hack, see View::SyntaxHighlight
169 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
173 $c->forward('View::SyntaxHighlight');
178 Exposes a given diff of a blob.
182 sub blobdiff : Local {
183 my ( $self, $c ) = @_;
185 my $commit = $self->_get_commit($c);
186 my $filename = $c->req->param('f')
187 || croak("No file specified!");
188 my($tree, $patch) = $c->model()->diff(
190 parent => $c->req->param('hp') || '',
197 # XXX Hack hack hack, see View::SyntaxHighlight
198 blobs => [$patch->[0]->{diff}],
200 action => 'blobdiff',
203 $c->forward('View::SyntaxHighlight');
208 Exposes a given commit.
213 my ( $self, $c ) = @_;
215 my $commit = $self->_get_commit($c);
218 diff_tree => ($c->model()->diff(commit => $commit))[0],
219 refs => $c->model()->references,
226 Exposes a given diff of a commit.
230 sub commitdiff : Local {
231 my ( $self, $c ) = @_;
233 my $commit = $self->_get_commit($c);
234 my($tree, $patch) = $c->model()->diff(
236 parent => $c->req->param('hp') || '',
243 # XXX Hack hack hack, see View::SyntaxHighlight
244 blobs => [map $_->{diff}, @$patch],
246 action => 'commitdiff',
249 $c->forward('View::SyntaxHighlight');
254 Expose an abbreviated log of a given sha1.
258 sub shortlog : Local {
259 my ( $self, $c ) = @_;
261 my $commit = $self->_get_commit($c);
263 sha1 => $commit->sha1,
264 count => Gitalist->config->{paging}{log},
265 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
268 my $page = $c->req->param('pg') || 0;
269 $logargs{skip} = $c->req->param('pg') * $logargs{count}
270 if $c->req->param('pg');
274 log_lines => [$c->model()->list_revs(%logargs)],
275 refs => $c->model()->references,
276 action => 'shortlog',
283 Calls shortlog internally. Perhaps that should be reversed ...
287 $_[0]->shortlog($_[1]);
288 $_[1]->stash->{action} = 'log';
293 The tree of a given commit.
298 my ( $self, $c ) = @_;
300 my $commit = $self->_get_commit($c, $c->req->param('hb'));
301 my $tree = $c->model()->get_object($c->req->param('h') || $commit->tree_sha1);
303 # XXX Useful defaults needed ...
306 tree_list => [$c->model()->list_tree($tree->sha1)],
307 path => $c->req->param('f') || '',
314 Expose the local reflog. This may go away.
319 my ( $self, $c ) = @_;
321 my @log = $c->model()->reflog(
334 my $commit = $self->_get_commit($c);
335 # Lifted from /shortlog.
337 sha1 => $commit->sha1,
338 count => Gitalist->config->{paging}{log},
339 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
341 type => $c->req->param('type'),
342 text => $c->req->param('text'),
343 regexp => $c->req->param('regexp') || 0,
349 results => [$c->model()->list_revs(%logargs)],
351 # This could be added - page => $page,
355 sub search_help : Local {
356 Carp::croak "Not implemented.";
361 Populate the header and footer. Perhaps not the best location.
368 # XXX Move these to a plugin!
371 return age_string(time - $_[0]->epoch);
375 my($line) = split /\n/, $cmt;
376 $line =~ s/^(.{70,80}\b).*/$1 …/;
381 # Yes, this is hideous.
386 # XXX This could probably be dropped altogether.
387 use Gitalist::Util qw(to_utf8);
388 # Formally git_header_html
392 my $title = $c->config->{sitename};
394 my $project = $c->req->param('project') || $c->req->param('p');
395 my $action = $c->req->param('action') || $c->req->param('a');
396 my $file_name = $c->req->param('filename') || $c->req->param('f');
397 if(defined $project) {
398 $title .= " - " . to_utf8($project);
399 if (defined $action) {
400 $title .= "/$action";
401 if (defined $file_name) {
402 $title .= " - " . $file_name;
403 if ($action eq "tree" && $file_name !~ m|/$|) {
410 $c->stash->{version} = $c->config->{version};
411 $c->stash->{git_version} = $c->model()->run_cmd('--version');
412 $c->stash->{title} = $title;
414 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
415 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
417 $c->stash->{project} = $project;
420 my %href_params = $self->feed_info($c);
421 $href_params{'-title'} ||= 'log';
423 foreach my $format qw(RSS Atom) {
424 my $type = lc($format);
427 title => "$project - $href_params{'-title'} - $format feed",
429 # XXX A bit hacky and could do with using gitweb::href() features
430 href => "?a=$type;p=$project",
431 type => "application/$type+xml"
435 # XXX This duplication also feels a bit awkward
436 title => "$project - $href_params{'-title'} - $format feed (no merges)",
437 href => "?a=$type;p=$project;opt=--no-merges",
438 type => "application/$type+xml"
444 title => $c->config->{sitename}." projects list",
445 href => '?a=project_index',
446 type => "text/plain; charset=utf-8"
449 title => $c->config->{sitename}." projects feeds",
451 type => "text/plain; charset=utf-8"
455 $c->stash->{favicon} = $c->config->{favicon};
460 logo_url => $c->config->{logo_url},
461 logo_label => $c->config->{logo_label},
462 logo_img => $c->config->{logo},
463 home_link => $c->config->{home_link},
464 home_link_str => $c->config->{home_link_str},
467 if(defined $project) {
469 search_text => ( $c->req->param('s') || $c->req->param('searchtext') || ''),
470 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
471 || $c->req->param('h') || $c->req->param('hash')
477 # Formally git_footer_html
481 my $feed_class = 'rss_logo';
484 my $project = $c->req->param('project') || $c->req->param('p');
485 if(defined $project) {
486 (my $pstr = $project) =~ s[/?\.git$][];
487 my $descr = $c->model()->project_info($project)->{description};
488 $c->stash->{project_description} = defined $descr
492 my %href_params = $self->feed_info($c);
494 $feed_class .= ' generic';
496 $href_params{'-title'} ||= 'log';
500 class => $feed_class,
501 title => "$href_params{'-title'} $_ feed",
502 href => "/?p=$project;a=\L$_",
509 class => $feed_class,
511 href => "/?a=$_->[0]",
513 }, [opml=>'OPML'],[project_index=>'TXT'],
518 # XXX This feels wrong here, should probably be refactored.
519 # returns hash to be passed to href to generate gitweb URL
520 # in -title key it returns description of link
524 my $format = shift || 'Atom';
525 my %res = (action => lc($format));
527 # feed links are possible only for project views
528 return unless $c->req->param('project');
530 # some views should link to OPML, or to generic project feed,
531 # or don't have specific feed yet (so they should use generic)
532 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
535 my $hash = $c->req->param('h') || $c->req->param('hash');
536 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
538 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
539 # from tag links; this also makes possible to detect branch links
540 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
541 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
545 # find log type for feed description (title)
547 my $file_name = $c->req->param('f') || $c->req->param('filename');
548 if (defined $file_name) {
549 $type = "history of $file_name";
550 $type .= "/" if $c->req->param('action') eq 'tree';
551 $type .= " on '$branch'" if (defined $branch);
553 $type = "log of $branch" if (defined $branch);
556 $res{-title} = $type;
557 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
558 $res{'file_name'} = $file_name;
565 Attempt to render a view, if needed.
569 sub end : ActionClass('RenderView') {
571 # Give project views the current HEAD.
572 if ($c->stash->{project}) {
573 $c->stash->{HEAD} = $c->model()->head_hash;
581 if ($age > 60*60*24*365*2) {
582 $age_str = (int $age/60/60/24/365);
583 $age_str .= " years ago";
584 } elsif ($age > 60*60*24*(365/12)*2) {
585 $age_str = int $age/60/60/24/(365/12);
586 $age_str .= " months ago";
587 } elsif ($age > 60*60*24*7*2) {
588 $age_str = int $age/60/60/24/7;
589 $age_str .= " weeks ago";
590 } elsif ($age > 60*60*24*2) {
591 $age_str = int $age/60/60/24;
592 $age_str .= " days ago";
593 } elsif ($age > 60*60*2) {
594 $age_str = int $age/60/60;
595 $age_str .= " hours ago";
596 } elsif ($age > 60*2) {
597 $age_str = int $age/60;
598 $age_str .= " min ago";
601 $age_str .= " sec ago";
603 $age_str .= " right now";
614 This library is free software. You can redistribute it and/or modify
615 it under the same terms as Perl itself.
619 __PACKAGE__->meta->make_immutable;