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 my $h = $c->req->param('h');
64 my $f = $c->req->param('f');
66 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
67 my $hash = ($h =~ /[^a-f0-9]/ ? $c->model('Git')->head_hash($h) : $h)
68 || ($f && $c->model('Git')->hash_by_path($f))
69 || $c->model('Git')->head_hash
70 # XXX This could definitely use more context.
71 || Carp::croak("Couldn't find a hash for the commit object!");
73 my $commit = $c->model('Git')->get_object($hash)
74 or Carp::croak("Couldn't find a commit object for '$hash'!");
81 Provides the project listing.
85 sub index :Path :Args(0) {
86 my ( $self, $c ) = @_;
88 # Leave actions up to gitweb at this point.
89 return $self->run_gitweb($c)
90 if $c->req->param('a');
92 my $list = $c->model('Git')->list_projects;
94 die "No projects found";
98 searchtext => $c->req->param('searchtext') || '',
106 A summary of what's happening in the repo.
110 sub summary : Local {
111 my ( $self, $c ) = @_;
113 my $commit = $self->_get_commit($c);
116 info => $c->model('Git')->project_info($c->model('Git')->project),
117 log_lines => [$c->model('Git')->list_revs(sha1 => $commit->sha1, count => 16)],
118 refs => $c->model('Git')->references,
119 heads => [$c->model('Git')->heads],
120 HEAD => $c->model('Git')->head_hash,
127 The current list of heads (aka branches) in the repo.
132 my ( $self, $c ) = @_;
135 commit => $self->_get_commit($c),
136 heads => [$c->model('Git')->heads],
137 HEAD => $c->model('Git')->head_hash,
144 The blob action i.e the contents of a file.
149 my ( $self, $c ) = @_;
151 my $h = $c->req->param('h')
152 || $c->model('Git')->hash_by_path($c->req->param('f'))
153 || die "No file or sha1 provided.";
154 my $hb = $c->req->param('hb')
155 || $c->model('Git')->head_hash
156 || die "Couldn't discern the corresponding head.";
159 blob => $c->model('Git')->get_object($h)->content,
160 head => $c->model('Git')->get_object($hb),
161 filename => $c->req->param('f') || '',
165 $c->forward('View::SyntaxHighlight');
170 Exposes a given commit.
175 my ( $self, $c ) = @_;
177 my $commit = $self->_get_commit($c);
180 diff_tree => [$c->model('Git')->diff_tree($commit)],
181 branches_on => [$c->model('Git')->refs_for($commit->sha1)],
188 Exposes a given diff of a commit.
192 sub commitdiff : Local {
193 my ( $self, $c ) = @_;
195 my $commit = $self->_get_commit($c);
198 diff_tree => [$c->model('Git')->diff_tree($commit)],
199 diff => [$c->model('Git')->diff($commit->parent_sha1, $commit->sha1)],
200 action => 'commitdiff',
206 Expose an abbreviated log of a given sha1.
210 sub shortlog : Local {
211 my ( $self, $c ) = @_;
213 my $commit = $self->_get_commit($c);
217 log_lines => [$c->model('Git')->list_revs(sha1 => $commit->sha1)],
218 refs => $c->model('Git')->references,
219 action => 'shortlog',
225 Calls shortlog internally. Perhaps that should be reversed ...
229 $_[0]->shortlog($_[1]);
230 $_[1]->stash->{action} = 'log';
235 The tree of a given commit.
240 my ( $self, $c ) = @_;
242 my $commit = $self->_get_commit($c);
244 # XXX Useful defaults needed ...
246 tree => $c->model('Git')->get_object($c->req->param('hb')),
247 tree_list => [$c->model('Git')->list_tree($commit->sha1)],
254 Expose the local reflog. This may go away.
259 my ( $self, $c ) = @_;
261 my @log = $c->model('Git')->reflog(
273 Populate the header and footer. Perhaps not the best location.
280 # XXX Temp hack until a decent solution is invented.
281 $c->model('Git')->project($c->req->param('p'))
282 if $c->req->param('p');
284 # Yes, this is hideous.
289 # XXX This could probably be dropped altogether.
290 use Gitalist::Util qw(to_utf8);
291 # Formally git_header_html
295 my $title = $c->config->{sitename};
297 my $project = $c->req->param('project') || $c->req->param('p');
298 my $action = $c->req->param('action') || $c->req->param('a');
299 my $file_name = $c->req->param('filename') || $c->req->param('f');
300 if(defined $project) {
301 $title .= " - " . to_utf8($project);
302 if (defined $action) {
303 $title .= "/$action";
304 if (defined $file_name) {
305 $title .= " - " . $file_name;
306 if ($action eq "tree" && $file_name !~ m|/$|) {
313 $c->stash->{version} = $c->config->{version};
314 $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
315 $c->stash->{title} = $title;
317 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
318 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
320 $c->stash->{project} = $project;
323 my %href_params = $self->feed_info($c);
324 $href_params{'-title'} ||= 'log';
326 foreach my $format qw(RSS Atom) {
327 my $type = lc($format);
330 title => "$project - $href_params{'-title'} - $format feed",
332 # XXX A bit hacky and could do with using gitweb::href() features
333 href => "?a=$type;p=$project",
334 type => "application/$type+xml"
338 # XXX This duplication also feels a bit awkward
339 title => "$project - $href_params{'-title'} - $format feed (no merges)",
340 href => "?a=$type;p=$project;opt=--no-merges",
341 type => "application/$type+xml"
347 title => $c->config->{sitename}." projects list",
348 href => '?a=project_index',
349 type => "text/plain; charset=utf-8"
352 title => $c->config->{sitename}." projects feeds",
354 type => "text/plain; charset=utf-8"
358 $c->stash->{favicon} = $c->config->{favicon};
363 logo_url => $c->config->{logo_url},
364 logo_label => $c->config->{logo_label},
365 logo_img => $c->config->{logo},
366 home_link => $c->config->{home_link},
367 home_link_str => $c->config->{home_link_str},
370 if(defined $project) {
372 search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
373 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
374 || $c->req->param('h') || $c->req->param('hash')
380 # Formally git_footer_html
384 my $feed_class = 'rss_logo';
387 my $project = $c->req->param('project') || $c->req->param('p');
388 if(defined $project) {
389 (my $pstr = $project) =~ s[/?\.git$][];
390 my $descr = $c->model('Git')->project_info($project)->{description};
391 $c->stash->{project_description} = defined $descr
395 my %href_params = $self->feed_info($c);
397 $feed_class .= ' generic';
399 $href_params{'-title'} ||= 'log';
403 class => $feed_class,
404 title => "$href_params{'-title'} $_ feed",
405 href => "/?p=$project;a=\L$_",
412 class => $feed_class,
414 href => "/?a=$_->[0]",
416 }, [opml=>'OPML'],[project_index=>'TXT'],
421 # XXX This feels wrong here, should probably be refactored.
422 # returns hash to be passed to href to generate gitweb URL
423 # in -title key it returns description of link
427 my $format = shift || 'Atom';
428 my %res = (action => lc($format));
430 # feed links are possible only for project views
431 return unless $c->req->param('project');
433 # some views should link to OPML, or to generic project feed,
434 # or don't have specific feed yet (so they should use generic)
435 return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
438 my $hash = $c->req->param('h') || $c->req->param('hash');
439 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
441 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
442 # from tag links; this also makes possible to detect branch links
443 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
444 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
448 # find log type for feed description (title)
450 my $file_name = $c->req->param('f') || $c->req->param('filename');
451 if (defined $file_name) {
452 $type = "history of $file_name";
453 $type .= "/" if $c->req->param('action') eq 'tree';
454 $type .= " on '$branch'" if (defined $branch);
456 $type = "log of $branch" if (defined $branch);
459 $res{-title} = $type;
460 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
461 $res{'file_name'} = $file_name;
467 Attempt to render a view, if needed.
471 sub end : ActionClass('RenderView') {}
479 This library is free software. You can redistribute it and/or modify
480 it under the same terms as Perl itself.
484 __PACKAGE__->meta->make_immutable;