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 ($c->stash->{current_model} eq 'GitRepos') {
70 $m = $c->model()->project($c->stash->{project});
74 ($pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
77 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
78 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
79 || ($f && $m->hash_by_path($f))
81 # XXX This could definitely use more context.
82 || Carp::croak("Couldn't find a hash for the commit object!");
84 my $commit = $m->get_object($hash)
85 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
92 Provides the project listing.
96 sub index :Path :Args(0) {
97 my ( $self, $c ) = @_;
98 $c->stash(current_model => 'GitRepos');
99 # Leave actions up to gitweb at this point.
100 return $self->run_gitweb($c)
101 if $c->req->param('a');
103 my $list = $c->model()->list_projects;
105 die "No projects found in ". $c->model->repo_dir;
109 searchtext => $c->req->param('searchtext') || '',
117 A summary of what's happening in the repo.
121 sub summary : Local {
122 my ( $self, $c ) = @_;
124 my $commit = $self->_get_commit($c);
127 info => $c->model()->project_info($c->model()->project),
128 log_lines => [$c->model()->list_revs(
129 sha1 => $commit->sha1, count => Gitalist->config->{paging}{summary}
131 refs => $c->model()->references,
132 heads => [$c->model()->heads],
139 The current list of heads (aka branches) in the repo.
144 my ( $self, $c ) = @_;
145 $c->stash( current_model => 'GitRepos' );
146 my $project = $c->model()->project( $c->stash->{project} );
148 commit => $self->_get_commit($c),
149 heads => [$project->heads],
156 The blob action i.e the contents of a file.
161 my ( $self, $c ) = @_;
163 my $h = $c->req->param('h')
164 || $c->model()->hash_by_path($c->req->param('f'))
165 || die "No file or sha1 provided.";
166 my $hb = $c->req->param('hb')
167 || $c->model()->head_hash
168 || die "Couldn't discern the corresponding head.";
170 my $filename = $c->req->param('f') || '';
173 blob => $c->model()->get_object($h)->content,
174 head => $c->model()->get_object($hb),
175 filename => $filename,
176 # XXX Hack hack hack, see View::SyntaxHighlight
177 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
181 $c->forward('View::SyntaxHighlight');
186 Exposes a given diff of a blob.
190 sub blobdiff : Local {
191 my ( $self, $c ) = @_;
193 my $commit = $self->_get_commit($c);
194 my $filename = $c->req->param('f')
195 || croak("No file specified!");
196 my($tree, $patch) = $c->model()->diff(
198 parent => $c->req->param('hp') || '',
205 # XXX Hack hack hack, see View::SyntaxHighlight
206 blobs => [$patch->[0]->{diff}],
208 action => 'blobdiff',
211 $c->forward('View::SyntaxHighlight');
216 Exposes a given commit.
221 my ( $self, $c ) = @_;
223 my $commit = $self->_get_commit($c);
226 diff_tree => ($c->model()->diff(commit => $commit))[0],
227 refs => $c->model()->references,
234 Exposes a given diff of a commit.
238 sub commitdiff : Local {
239 my ( $self, $c ) = @_;
241 my $commit = $self->_get_commit($c);
242 my($tree, $patch) = $c->model()->diff(
244 parent => $c->req->param('hp') || '',
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 ) = @_;
269 my $commit = $self->_get_commit($c);
271 sha1 => $commit->sha1,
272 count => Gitalist->config->{paging}{log},
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 => [$c->model()->list_revs(%logargs)],
283 refs => $c->model()->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 ) = @_;
308 my $commit = $self->_get_commit($c, $c->req->param('hb'));
309 my $tree = $c->model()->get_object($c->req->param('h') || $commit->tree_sha1);
311 # XXX Useful defaults needed ...
314 tree_list => [$c->model()->list_tree($tree->sha1)],
315 path => $c->req->param('f') || '',
322 Expose the local reflog. This may go away.
327 my ( $self, $c ) = @_;
329 my @log = $c->model()->reflog(
342 my $commit = $self->_get_commit($c);
343 # Lifted from /shortlog.
345 sha1 => $commit->sha1,
346 count => Gitalist->config->{paging}{log},
347 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
349 type => $c->req->param('type'),
350 text => $c->req->param('text'),
351 regexp => $c->req->param('regexp') || 0,
357 results => [$c->model()->list_revs(%logargs)],
359 # This could be added - page => $page,
363 sub search_help : Local {
364 Carp::croak "Not implemented.";
369 Populate the header and footer. Perhaps not the best location.
376 # XXX Move these to a plugin!
379 return age_string(time - $_[0]->epoch);
383 my($line) = split /\n/, $cmt;
384 $line =~ s/^(.{70,80}\b).*/$1 …/;
389 # Yes, this is hideous.
394 # XXX This could probably be dropped altogether.
395 use Gitalist::Util qw(to_utf8);
396 # Formally git_header_html
400 my $title = $c->config->{sitename};
402 my $project = $c->req->param('project') || $c->req->param('p');
403 my $action = $c->req->param('action') || $c->req->param('a');
404 my $file_name = $c->req->param('filename') || $c->req->param('f');
405 if(defined $project) {
406 $title .= " - " . to_utf8($project);
407 if (defined $action) {
408 $title .= "/$action";
409 if (defined $file_name) {
410 $title .= " - " . $file_name;
411 if ($action eq "tree" && $file_name !~ m|/$|) {
418 $c->stash->{version} = $c->config->{version};
419 $c->stash->{git_version} = $c->model()->run_cmd('--version');
420 $c->stash->{title} = $title;
422 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
423 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
425 $c->stash->{project} = $project;
428 my %href_params = $self->feed_info($c);
429 $href_params{'-title'} ||= 'log';
431 foreach my $format qw(RSS Atom) {
432 my $type = lc($format);
435 title => "$project - $href_params{'-title'} - $format feed",
437 # XXX A bit hacky and could do with using gitweb::href() features
438 href => "?a=$type;p=$project",
439 type => "application/$type+xml"
443 # XXX This duplication also feels a bit awkward
444 title => "$project - $href_params{'-title'} - $format feed (no merges)",
445 href => "?a=$type;p=$project;opt=--no-merges",
446 type => "application/$type+xml"
452 title => $c->config->{sitename}." projects list",
453 href => '?a=project_index',
454 type => "text/plain; charset=utf-8"
457 title => $c->config->{sitename}." projects feeds",
459 type => "text/plain; charset=utf-8"
463 $c->stash->{favicon} = $c->config->{favicon};
468 logo_url => $c->config->{logo_url},
469 logo_label => $c->config->{logo_label},
470 logo_img => $c->config->{logo},
471 home_link => $c->config->{home_link},
472 home_link_str => $c->config->{home_link_str},
475 if(defined $project) {
477 search_text => ( $c->req->param('s') || $c->req->param('searchtext') || ''),
478 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
479 || $c->req->param('h') || $c->req->param('hash')
485 # Formally git_footer_html
489 my $feed_class = 'rss_logo';
492 my $project = $c->req->param('project') || $c->req->param('p');
493 if(defined $project) {
494 (my $pstr = $project) =~ s[/?\.git$][];
495 my $descr = $c->model()->project_info($project)->{description};
496 $c->stash->{project_description} = defined $descr
500 my %href_params = $self->feed_info($c);
502 $feed_class .= ' generic';
504 $href_params{'-title'} ||= 'log';
508 class => $feed_class,
509 title => "$href_params{'-title'} $_ feed",
510 href => "/?p=$project;a=\L$_",
517 class => $feed_class,
519 href => "/?a=$_->[0]",
521 }, [opml=>'OPML'],[project_index=>'TXT'],
526 # XXX This feels wrong here, should probably be refactored.
527 # returns hash to be passed to href to generate gitweb URL
528 # in -title key it returns description of link
532 my $format = shift || 'Atom';
533 my %res = (action => lc($format));
535 # feed links are possible only for project views
536 return unless $c->req->param('project');
538 # some views should link to OPML, or to generic project feed,
539 # or don't have specific feed yet (so they should use generic)
540 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
543 my $hash = $c->req->param('h') || $c->req->param('hash');
544 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
546 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
547 # from tag links; this also makes possible to detect branch links
548 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
549 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
553 # find log type for feed description (title)
555 my $file_name = $c->req->param('f') || $c->req->param('filename');
556 if (defined $file_name) {
557 $type = "history of $file_name";
558 $type .= "/" if $c->req->param('action') eq 'tree';
559 $type .= " on '$branch'" if (defined $branch);
561 $type = "log of $branch" if (defined $branch);
564 $res{-title} = $type;
565 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
566 $res{'file_name'} = $file_name;
573 Attempt to render a view, if needed.
577 sub end : ActionClass('RenderView') {
579 # Give project views the current HEAD.
580 if ($c->stash->{project}) {
581 if ($c->stash->{current_model} eq 'GitRepos') {
582 my $project = $c->model()->project($c->stash->{project});
583 $c->stash->{HEAD} = $project->head_hash;
585 $c->stash->{HEAD} = $c->model()->head_hash;
594 if ($age > 60*60*24*365*2) {
595 $age_str = (int $age/60/60/24/365);
596 $age_str .= " years ago";
597 } elsif ($age > 60*60*24*(365/12)*2) {
598 $age_str = int $age/60/60/24/(365/12);
599 $age_str .= " months ago";
600 } elsif ($age > 60*60*24*7*2) {
601 $age_str = int $age/60/60/24/7;
602 $age_str .= " weeks ago";
603 } elsif ($age > 60*60*24*2) {
604 $age_str = int $age/60/60/24;
605 $age_str .= " days ago";
606 } elsif ($age > 60*60*2) {
607 $age_str = int $age/60/60;
608 $age_str .= " hours ago";
609 } elsif ($age > 60*2) {
610 $age_str = int $age/60;
611 $age_str .= " min ago";
614 $age_str .= " sec ago";
616 $age_str .= " right now";
627 This library is free software. You can redistribute it and/or modify
628 it under the same terms as Perl itself.
632 __PACKAGE__->meta->make_immutable;