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';
63 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
64 my $hash = $c->req->param('h')
65 || ($c->req->param('f') ? $c->model('Git')->hash_by_path($c->req->param('f')) : '')
66 || $c->model('Git')->head_hash
67 # XXX This could definitely use more context.
68 || Carp::croak("Couldn't find a hash for the commit object!");
70 my $commit = $c->model('Git')->get_object($hash)
71 or Carp::croak("Couldn't find a commit object for '$hash'!");
78 Provides the project listing.
82 sub index :Path :Args(0) {
83 my ( $self, $c ) = @_;
85 # Leave actions up to gitweb at this point.
86 return $self->run_gitweb($c)
87 if $c->req->param('a');
89 my $list = $c->model('Git')->list_projects;
91 die "No projects found";
95 searchtext => $c->req->param('searchtext') || '',
103 A summary of what's happening in the repo.
107 sub summary : Local {
108 my ( $self, $c ) = @_;
110 my $commit = $self->_get_commit($c);
113 info => $c->model('Git')->project_info($c->model('Git')->project),
114 log_lines => [$c->model('Git')->list_revs(rev => $commit->sha1, count => 17)],
115 heads => [$c->model('Git')->heads],
122 The current list of heads (aka branches) in the repo.
127 my ( $self, $c ) = @_;
130 commit => $self->_get_commit($c),
131 heads => [$c->model('Git')->heads],
138 The blob action i.e the contents of a file.
143 my ( $self, $c ) = @_;
145 my $h = $c->req->param('h')
146 || $c->model('Git')->hash_by_path($c->req->param('f'))
147 || die "No file or sha1 provided.";
148 my $hb = $c->req->param('hb')
149 || $c->model('Git')->head_hash
150 || die "Couldn't discern the corresponding head.";
153 blob => $c->model('Git')->get_object($h)->content,
154 head => $c->model('Git')->get_object($hb),
155 filename => $c->req->param('f') || '',
159 $c->forward('View::SyntaxHighlight');
164 Exposes a given commit.
169 my ( $self, $c ) = @_;
171 my $commit = $self->_get_commit($c);
174 diff_tree => [$c->model('Git')->diff_tree($commit)],
175 branches_on => [$c->model('Git')->refs_for($commit->sha1)],
182 Exposes a given diff of a commit.
186 sub commitdiff : Local {
187 my ( $self, $c ) = @_;
189 my $commit = $self->_get_commit($c);
192 diff_tree => [$c->model('Git')->diff_tree($commit)],
193 diff => [$c->model('Git')->diff($commit->parent_sha1, $commit->sha1)],
194 action => 'commitdiff',
200 Expose an abbreviated log of a given sha1.
204 sub shortlog : Local {
205 my ( $self, $c ) = @_;
207 my $commit = $self->_get_commit($c);
211 log_lines => [$c->model('Git')->list_revs(rev => $commit->sha1)],
212 action => 'shortlog',
218 Calls shortlog internally. Perhaps that should be reversed ...
222 $_[0]->shortlog($_[1]);
223 $_[1]->stash->{action} = 'log';
228 The tree of a given commit.
233 my ( $self, $c ) = @_;
235 my $commit = $self->_get_commit($c);
237 # XXX Useful defaults needed ...
239 tree => $c->model('Git')->get_object($c->req->param('hb')),
240 tree_list => [$c->model('Git')->list_tree($commit->sha1)],
247 Expose the local reflog. This may go away.
252 my ( $self, $c ) = @_;
254 my @log = $c->model('Git')->reflog(
266 Populate the header and footer. Perhaps not the best location.
273 # XXX Temp hack until a decent solution is invented.
274 $c->model('Git')->project($c->req->param('p'))
275 if $c->req->param('p');
277 # Yes, this is hideous.
282 # XXX This could probably be dropped altogether.
283 use Gitalist::Util qw(to_utf8);
284 # Formally git_header_html
288 my $title = $c->config->{sitename};
290 my $project = $c->req->param('project') || $c->req->param('p');
291 my $action = $c->req->param('action') || $c->req->param('a');
292 my $file_name = $c->req->param('filename') || $c->req->param('f');
293 if(defined $project) {
294 $title .= " - " . to_utf8($project);
295 if (defined $action) {
296 $title .= "/$action";
297 if (defined $file_name) {
298 $title .= " - " . $file_name;
299 if ($action eq "tree" && $file_name !~ m|/$|) {
306 $c->stash->{version} = $c->config->{version};
307 $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
308 $c->stash->{title} = $title;
310 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
311 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
313 $c->stash->{project} = $project;
316 my %href_params = $self->feed_info($c);
317 $href_params{'-title'} ||= 'log';
319 foreach my $format qw(RSS Atom) {
320 my $type = lc($format);
323 title => "$project - $href_params{'-title'} - $format feed",
325 # XXX A bit hacky and could do with using gitweb::href() features
326 href => "?a=$type;p=$project",
327 type => "application/$type+xml"
331 # XXX This duplication also feels a bit awkward
332 title => "$project - $href_params{'-title'} - $format feed (no merges)",
333 href => "?a=$type;p=$project;opt=--no-merges",
334 type => "application/$type+xml"
340 title => $c->config->{sitename}." projects list",
341 href => '?a=project_index',
342 type => "text/plain; charset=utf-8"
345 title => $c->config->{sitename}." projects feeds",
347 type => "text/plain; charset=utf-8"
351 $c->stash->{favicon} = $c->config->{favicon};
356 logo_url => $c->config->{logo_url},
357 logo_label => $c->config->{logo_label},
358 logo_img => $c->config->{logo},
359 home_link => $c->config->{home_link},
360 home_link_str => $c->config->{home_link_str},
363 if(defined $project) {
365 search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
366 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
367 || $c->req->param('h') || $c->req->param('hash')
373 # Formally git_footer_html
377 my $feed_class = 'rss_logo';
380 my $project = $c->req->param('project') || $c->req->param('p');
381 if(defined $project) {
382 (my $pstr = $project) =~ s[/?\.git$][];
383 my $descr = $c->model('Git')->project_info($project)->{description};
384 $c->stash->{project_description} = defined $descr
388 my %href_params = $self->feed_info($c);
390 $feed_class .= ' generic';
392 $href_params{'-title'} ||= 'log';
396 class => $feed_class,
397 title => "$href_params{'-title'} $_ feed",
398 href => "/?p=$project;a=\L$_",
405 class => $feed_class,
407 href => "/?a=$_->[0]",
409 }, [opml=>'OPML'],[project_index=>'TXT'],
414 # XXX This feels wrong here, should probably be refactored.
415 # returns hash to be passed to href to generate gitweb URL
416 # in -title key it returns description of link
420 my $format = shift || 'Atom';
421 my %res = (action => lc($format));
423 # feed links are possible only for project views
424 return unless $c->req->param('project');
426 # some views should link to OPML, or to generic project feed,
427 # or don't have specific feed yet (so they should use generic)
428 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
431 my $hash = $c->req->param('h') || $c->req->param('hash');
432 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
434 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
435 # from tag links; this also makes possible to detect branch links
436 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
437 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
441 # find log type for feed description (title)
443 my $file_name = $c->req->param('f') || $c->req->param('filename');
444 if (defined $file_name) {
445 $type = "history of $file_name";
446 $type .= "/" if $c->req->param('action') eq 'tree';
447 $type .= " on '$branch'" if (defined $branch);
449 $type = "log of $branch" if (defined $branch);
452 $res{-title} = $type;
453 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
454 $res{'file_name'} = $file_name;
460 Attempt to render a view, if needed.
464 sub end : ActionClass('RenderView') {}
472 This library is free software. You can redistribute it and/or modify
473 it under the same terms as Perl itself.
477 __PACKAGE__->meta->make_immutable;