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->model()->project($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 ) = @_;
123 my $commit = $self->_get_commit($c);
126 info => $c->model()->project_info($c->model()->project),
127 log_lines => [$c->model()->list_revs(
128 sha1 => $commit->sha1, count => Gitalist->config->{paging}{summary}
130 refs => $c->model()->references,
131 heads => [$c->model()->heads],
138 The current list of heads (aka branches) in the repo.
143 my ( $self, $c ) = @_;
144 $c->stash( current_model => 'GitRepos' );
145 my $project = $c->model()->project( $c->stash->{project} );
147 commit => $self->_get_commit($c),
148 heads => [$project->heads],
155 The blob action i.e the contents of a file.
160 my ( $self, $c ) = @_;
162 my $h = $c->req->param('h')
163 || $c->model()->hash_by_path($c->req->param('f'))
164 || die "No file or sha1 provided.";
165 my $hb = $c->req->param('hb')
166 || $c->model()->head_hash
167 || die "Couldn't discern the corresponding head.";
169 my $filename = $c->req->param('f') || '';
172 blob => $c->model()->get_object($h)->content,
173 head => $c->model()->get_object($hb),
174 filename => $filename,
175 # XXX Hack hack hack, see View::SyntaxHighlight
176 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
180 $c->forward('View::SyntaxHighlight');
185 Exposes a given diff of a blob.
189 sub blobdiff : Local {
190 my ( $self, $c ) = @_;
192 my $commit = $self->_get_commit($c);
193 my $filename = $c->req->param('f')
194 || croak("No file specified!");
195 my($tree, $patch) = $c->model()->diff(
197 parent => $c->req->param('hp') || '',
204 # XXX Hack hack hack, see View::SyntaxHighlight
205 blobs => [$patch->[0]->{diff}],
207 action => 'blobdiff',
210 $c->forward('View::SyntaxHighlight');
215 Exposes a given commit.
220 my ( $self, $c ) = @_;
222 my $commit = $self->_get_commit($c);
225 diff_tree => ($c->model()->diff(commit => $commit))[0],
226 refs => $c->model()->references,
233 Exposes a given diff of a commit.
237 sub commitdiff : Local {
238 my ( $self, $c ) = @_;
240 my $commit = $self->_get_commit($c);
241 my($tree, $patch) = $c->model()->diff(
243 parent => $c->req->param('hp') || '',
250 # XXX Hack hack hack, see View::SyntaxHighlight
251 blobs => [map $_->{diff}, @$patch],
253 action => 'commitdiff',
256 $c->forward('View::SyntaxHighlight');
261 Expose an abbreviated log of a given sha1.
265 sub shortlog : Local {
266 my ( $self, $c ) = @_;
268 my $commit = $self->_get_commit($c);
270 sha1 => $commit->sha1,
271 count => Gitalist->config->{paging}{log},
272 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
275 my $page = $c->req->param('pg') || 0;
276 $logargs{skip} = $c->req->param('pg') * $logargs{count}
277 if $c->req->param('pg');
281 log_lines => [$c->model()->list_revs(%logargs)],
282 refs => $c->model()->references,
283 action => 'shortlog',
290 Calls shortlog internally. Perhaps that should be reversed ...
294 $_[0]->shortlog($_[1]);
295 $_[1]->stash->{action} = 'log';
300 The tree of a given commit.
305 my ( $self, $c ) = @_;
307 my $commit = $self->_get_commit($c, $c->req->param('hb'));
308 my $tree = $c->model()->get_object($c->req->param('h') || $commit->tree_sha1);
310 # XXX Useful defaults needed ...
313 tree_list => [$c->model()->list_tree($tree->sha1)],
314 path => $c->req->param('f') || '',
321 Expose the local reflog. This may go away.
326 my ( $self, $c ) = @_;
328 my @log = $c->model()->reflog(
341 my $commit = $self->_get_commit($c);
342 # Lifted from /shortlog.
344 sha1 => $commit->sha1,
345 count => Gitalist->config->{paging}{log},
346 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
348 type => $c->req->param('type'),
349 text => $c->req->param('text'),
350 regexp => $c->req->param('regexp') || 0,
356 results => [$c->model()->list_revs(%logargs)],
358 # This could be added - page => $page,
362 sub search_help : Local {
363 Carp::croak "Not implemented.";
368 Populate the header and footer. Perhaps not the best location.
375 # XXX Move these to a plugin!
378 return 'never' unless $_[0];
379 return age_string(time - $_[0]->epoch);
383 my($line) = split /\n/, $cmt;
384 $line =~ s/^(.{70,80}\b).*/$1 …/;
387 abridged_description => sub {
388 join(' ', grep { defined } (split / /, shift)[0..10]);
392 # Yes, this is hideous.
397 # XXX This could probably be dropped altogether.
398 use Gitalist::Util qw(to_utf8);
399 # Formally git_header_html
403 my $title = $c->config->{sitename};
405 my $project = $c->req->param('project') || $c->req->param('p');
406 my $action = $c->req->param('action') || $c->req->param('a');
407 my $file_name = $c->req->param('filename') || $c->req->param('f');
408 if(defined $project) {
409 $title .= " - " . to_utf8($project);
410 if (defined $action) {
411 $title .= "/$action";
412 if (defined $file_name) {
413 $title .= " - " . $file_name;
414 if ($action eq "tree" && $file_name !~ m|/$|) {
421 $c->stash->{version} = $c->config->{version};
422 $c->stash->{git_version} = $c->model()->run_cmd('--version');
423 $c->stash->{title} = $title;
425 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
426 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
428 $c->stash->{project} = $project;
431 my %href_params = $self->feed_info($c);
432 $href_params{'-title'} ||= 'log';
434 foreach my $format qw(RSS Atom) {
435 my $type = lc($format);
438 title => "$project - $href_params{'-title'} - $format feed",
440 # XXX A bit hacky and could do with using gitweb::href() features
441 href => "?a=$type;p=$project",
442 type => "application/$type+xml"
446 # XXX This duplication also feels a bit awkward
447 title => "$project - $href_params{'-title'} - $format feed (no merges)",
448 href => "?a=$type;p=$project;opt=--no-merges",
449 type => "application/$type+xml"
455 title => $c->config->{sitename}." projects list",
456 href => '?a=project_index',
457 type => "text/plain; charset=utf-8"
460 title => $c->config->{sitename}." projects feeds",
462 type => "text/plain; charset=utf-8"
466 $c->stash->{favicon} = $c->config->{favicon};
471 logo_url => $c->config->{logo_url},
472 logo_label => $c->config->{logo_label},
473 logo_img => $c->config->{logo},
474 home_link => $c->config->{home_link},
475 home_link_str => $c->config->{home_link_str},
478 if(defined $project) {
480 search_text => ( $c->req->param('s') || $c->req->param('searchtext') || ''),
481 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
482 || $c->req->param('h') || $c->req->param('hash')
488 # Formally git_footer_html
492 my $feed_class = 'rss_logo';
495 my $project = $c->req->param('project') || $c->req->param('p');
496 if(defined $project) {
497 (my $pstr = $project) =~ s[/?\.git$][];
498 my $descr = $c->model()->project_info($project)->{description};
499 $c->stash->{project_description} = defined $descr
503 my %href_params = $self->feed_info($c);
505 $feed_class .= ' generic';
507 $href_params{'-title'} ||= 'log';
511 class => $feed_class,
512 title => "$href_params{'-title'} $_ feed",
513 href => "/?p=$project;a=\L$_",
520 class => $feed_class,
522 href => "/?a=$_->[0]",
524 }, [opml=>'OPML'],[project_index=>'TXT'],
529 # XXX This feels wrong here, should probably be refactored.
530 # returns hash to be passed to href to generate gitweb URL
531 # in -title key it returns description of link
535 my $format = shift || 'Atom';
536 my %res = (action => lc($format));
538 # feed links are possible only for project views
539 return unless $c->req->param('project');
541 # some views should link to OPML, or to generic project feed,
542 # or don't have specific feed yet (so they should use generic)
543 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
546 my $hash = $c->req->param('h') || $c->req->param('hash');
547 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
549 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
550 # from tag links; this also makes possible to detect branch links
551 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
552 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
556 # find log type for feed description (title)
558 my $file_name = $c->req->param('f') || $c->req->param('filename');
559 if (defined $file_name) {
560 $type = "history of $file_name";
561 $type .= "/" if $c->req->param('action') eq 'tree';
562 $type .= " on '$branch'" if (defined $branch);
564 $type = "log of $branch" if (defined $branch);
567 $res{-title} = $type;
568 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
569 $res{'file_name'} = $file_name;
576 Attempt to render a view, if needed.
580 sub end : ActionClass('RenderView') {
582 # Give project views the current HEAD.
583 if ($c->stash->{project}) {
584 if ($c->stash->{current_model} &&
585 $c->stash->{current_model} eq 'GitRepos') {
586 my $project = $c->model()->project($c->stash->{project});
587 $c->stash->{HEAD} = $project->head_hash;
589 $c->stash->{HEAD} = $c->model()->head_hash;
598 if ($age > 60*60*24*365*2) {
599 $age_str = (int $age/60/60/24/365);
600 $age_str .= " years ago";
601 } elsif ($age > 60*60*24*(365/12)*2) {
602 $age_str = int $age/60/60/24/(365/12);
603 $age_str .= " months ago";
604 } elsif ($age > 60*60*24*7*2) {
605 $age_str = int $age/60/60/24/7;
606 $age_str .= " weeks ago";
607 } elsif ($age > 60*60*24*2) {
608 $age_str = int $age/60/60/24;
609 $age_str .= " days ago";
610 } elsif ($age > 60*60*2) {
611 $age_str = int $age/60/60;
612 $age_str .= " hours ago";
613 } elsif ($age > 60*2) {
614 $age_str = int $age/60;
615 $age_str .= " min ago";
618 $age_str .= " sec ago";
620 $age_str .= " right now";
631 This library is free software. You can redistribute it and/or modify
632 it under the same terms as Perl itself.
636 __PACKAGE__->meta->make_immutable;