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 main shim around C<gitweb.pm>.
38 my ( $self, $c ) = @_;
40 # XXX A slippery slope to be sure.
41 if($c->req->param('a')) {
42 my $capture = IO::Capture::Stdout->new();
45 my $action = gitweb::main($c);
54 my $output = join '', $capture->read;
55 $c->stash->{gitweb_output} = $output;
56 $c->stash->{template} = 'gitweb.tt2';
61 my($self, $c, $haveh) = @_;
63 my $h = $haveh || $c->req->param('h') || '';
64 my $f = $c->req->param('f');
65 my $m = $c->model('Git');
67 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
68 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
69 || ($f && $m->hash_by_path($f))
71 # XXX This could definitely use more context.
72 || Carp::croak("Couldn't find a hash for the commit object!");
75 (my $pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
76 my $commit = $m->get_object($hash)
77 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
84 Provides the project listing.
88 sub index :Path :Args(0) {
89 my ( $self, $c ) = @_;
91 # Leave actions up to gitweb at this point.
92 return $self->run_gitweb($c)
93 if $c->req->param('a');
95 my $list = $c->model('Git')->list_projects;
97 die "No projects found in ".Gitalist->config->{repodir};
101 searchtext => $c->req->param('searchtext') || '',
109 A summary of what's happening in the repo.
113 sub summary : Local {
114 my ( $self, $c ) = @_;
116 my $commit = $self->_get_commit($c);
119 info => $c->model('Git')->project_info($c->model('Git')->project),
120 log_lines => [$c->model('Git')->list_revs(
121 sha1 => $commit->sha1, count => Gitalist->config->{paging}{summary}
123 refs => $c->model('Git')->references,
124 heads => [$c->model('Git')->heads],
131 The current list of heads (aka branches) in the repo.
136 my ( $self, $c ) = @_;
139 commit => $self->_get_commit($c),
140 heads => [$c->model('Git')->heads],
147 The blob action i.e the contents of a file.
152 my ( $self, $c ) = @_;
154 my $h = $c->req->param('h')
155 || $c->model('Git')->hash_by_path($c->req->param('f'))
156 || die "No file or sha1 provided.";
157 my $hb = $c->req->param('hb')
158 || $c->model('Git')->head_hash
159 || die "Couldn't discern the corresponding head.";
161 my $filename = $c->req->param('f') || '';
164 blob => $c->model('Git')->get_object($h)->content,
165 head => $c->model('Git')->get_object($hb),
166 filename => $filename,
167 # XXX Hack hack hack, see View::SyntaxHighlight
168 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
172 $c->forward('View::SyntaxHighlight');
177 Exposes a given diff of a blob.
181 sub blobdiff : Local {
182 my ( $self, $c ) = @_;
184 my $commit = $self->_get_commit($c);
185 my $filename = $c->req->param('f')
186 || croak("No file specified!");
187 my($tree, $patch) = $c->model('Git')->diff(
189 parent => $c->req->param('hp') || '',
196 # XXX Hack hack hack, see View::SyntaxHighlight
197 blobs => [$patch->[0]->{diff}],
199 action => 'blobdiff',
202 $c->forward('View::SyntaxHighlight');
207 Exposes a given commit.
212 my ( $self, $c ) = @_;
214 my $commit = $self->_get_commit($c);
217 diff_tree => ($c->model('Git')->diff(commit => $commit))[0],
218 branches_on => [$c->model('Git')->refs_for($commit->sha1)],
225 Exposes a given diff of a commit.
229 sub commitdiff : Local {
230 my ( $self, $c ) = @_;
232 my $commit = $self->_get_commit($c);
233 my($tree, $patch) = $c->model('Git')->diff(
235 parent => $c->req->param('hp') || '',
242 # XXX Hack hack hack, see View::SyntaxHighlight
243 blobs => [map $_->{diff}, @$patch],
245 action => 'commitdiff',
248 $c->forward('View::SyntaxHighlight');
253 Expose an abbreviated log of a given sha1.
257 sub shortlog : Local {
258 my ( $self, $c ) = @_;
260 my $commit = $self->_get_commit($c);
262 sha1 => $commit->sha1,
263 count => Gitalist->config->{paging}{log},
264 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
267 my $page = $c->req->param('pg') || 0;
268 $logargs{skip} = $c->req->param('pg') * $logargs{count}
269 if $c->req->param('pg');
273 log_lines => [$c->model('Git')->list_revs(%logargs)],
274 refs => $c->model('Git')->references,
275 action => 'shortlog',
282 Calls shortlog internally. Perhaps that should be reversed ...
286 $_[0]->shortlog($_[1]);
287 $_[1]->stash->{action} = 'log';
292 The tree of a given commit.
297 my ( $self, $c ) = @_;
299 my $commit = $self->_get_commit($c, $c->req->param('hb'));
300 my $tree = $c->model('Git')->get_object($c->req->param('h') || $commit->tree_sha1);
302 # XXX Useful defaults needed ...
305 tree_list => [$c->model('Git')->list_tree($tree->sha1)],
306 path => $c->req->param('f') || '',
313 Expose the local reflog. This may go away.
318 my ( $self, $c ) = @_;
320 my @log = $c->model('Git')->reflog(
333 my $commit = $self->_get_commit($c);
334 # Lifted from /shortlog.
336 sha1 => $commit->sha1,
337 count => Gitalist->config->{paging}{log},
338 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
340 type => $c->req->param('type'),
341 text => $c->req->param('text'),
342 regexp => $c->req->param('regexp') || 0,
348 results => [$c->model('Git')->list_revs(%logargs)],
350 # This could be added - page => $page,
354 sub search_help : Local {
355 Carp::croak "Not implemented.";
360 Populate the header and footer. Perhaps not the best location.
367 # Yes, this is hideous.
372 # XXX This could probably be dropped altogether.
373 use Gitalist::Util qw(to_utf8);
374 # Formally git_header_html
378 my $title = $c->config->{sitename};
380 my $project = $c->req->param('project') || $c->req->param('p');
381 my $action = $c->req->param('action') || $c->req->param('a');
382 my $file_name = $c->req->param('filename') || $c->req->param('f');
383 if(defined $project) {
384 $title .= " - " . to_utf8($project);
385 if (defined $action) {
386 $title .= "/$action";
387 if (defined $file_name) {
388 $title .= " - " . $file_name;
389 if ($action eq "tree" && $file_name !~ m|/$|) {
396 $c->stash->{version} = $c->config->{version};
397 $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
398 $c->stash->{title} = $title;
400 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
401 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
403 $c->stash->{project} = $project;
406 my %href_params = $self->feed_info($c);
407 $href_params{'-title'} ||= 'log';
409 foreach my $format qw(RSS Atom) {
410 my $type = lc($format);
413 title => "$project - $href_params{'-title'} - $format feed",
415 # XXX A bit hacky and could do with using gitweb::href() features
416 href => "?a=$type;p=$project",
417 type => "application/$type+xml"
421 # XXX This duplication also feels a bit awkward
422 title => "$project - $href_params{'-title'} - $format feed (no merges)",
423 href => "?a=$type;p=$project;opt=--no-merges",
424 type => "application/$type+xml"
430 title => $c->config->{sitename}." projects list",
431 href => '?a=project_index',
432 type => "text/plain; charset=utf-8"
435 title => $c->config->{sitename}." projects feeds",
437 type => "text/plain; charset=utf-8"
441 $c->stash->{favicon} = $c->config->{favicon};
446 logo_url => $c->config->{logo_url},
447 logo_label => $c->config->{logo_label},
448 logo_img => $c->config->{logo},
449 home_link => $c->config->{home_link},
450 home_link_str => $c->config->{home_link_str},
453 if(defined $project) {
455 search_text => ( $c->req->param('s') || $c->req->param('searchtext') || ''),
456 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
457 || $c->req->param('h') || $c->req->param('hash')
463 # Formally git_footer_html
467 my $feed_class = 'rss_logo';
470 my $project = $c->req->param('project') || $c->req->param('p');
471 if(defined $project) {
472 (my $pstr = $project) =~ s[/?\.git$][];
473 my $descr = $c->model('Git')->project_info($project)->{description};
474 $c->stash->{project_description} = defined $descr
478 my %href_params = $self->feed_info($c);
480 $feed_class .= ' generic';
482 $href_params{'-title'} ||= 'log';
486 class => $feed_class,
487 title => "$href_params{'-title'} $_ feed",
488 href => "/?p=$project;a=\L$_",
495 class => $feed_class,
497 href => "/?a=$_->[0]",
499 }, [opml=>'OPML'],[project_index=>'TXT'],
504 # XXX This feels wrong here, should probably be refactored.
505 # returns hash to be passed to href to generate gitweb URL
506 # in -title key it returns description of link
510 my $format = shift || 'Atom';
511 my %res = (action => lc($format));
513 # feed links are possible only for project views
514 return unless $c->req->param('project');
516 # some views should link to OPML, or to generic project feed,
517 # or don't have specific feed yet (so they should use generic)
518 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
521 my $hash = $c->req->param('h') || $c->req->param('hash');
522 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
524 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
525 # from tag links; this also makes possible to detect branch links
526 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
527 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
531 # find log type for feed description (title)
533 my $file_name = $c->req->param('f') || $c->req->param('filename');
534 if (defined $file_name) {
535 $type = "history of $file_name";
536 $type .= "/" if $c->req->param('action') eq 'tree';
537 $type .= " on '$branch'" if (defined $branch);
539 $type = "log of $branch" if (defined $branch);
542 $res{-title} = $type;
543 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
544 $res{'file_name'} = $file_name;
550 Attempt to render a view, if needed.
554 sub end : ActionClass('RenderView') {
555 # Give every view the current HEAD.
556 $_[1]->stash->{HEAD} = $_[1]->model('Git')->head_hash;
565 This library is free software. You can redistribute it and/or modify
566 it under the same terms as Perl itself.
570 __PACKAGE__->meta->make_immutable;