1 package Gitalist::Controller::Root;
3 use namespace::autoclean;
5 BEGIN { extends 'Catalyst::Controller' }
7 __PACKAGE__->config->{namespace} = '';
16 Gitalist::Controller::Root - Root Controller for Gitalist
20 [enter your description here]
27 my($self, $c, $haveh) = @_;
29 my $h = $haveh || $c->req->param('h') || '';
30 my $f = $c->req->param('f');
32 my $m = $c->stash->{Project};
35 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
36 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
37 || ($f && $m->hash_by_path($f))
39 # XXX This could definitely use more context.
40 || Carp::croak("Couldn't find a hash for the commit object!");
42 my $commit = $m->get_object($hash)
43 or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
50 Provides the project listing.
54 sub index :Path :Args(0) {
55 my ( $self, $c ) = @_;
57 $c->detach($c->req->param('a'))
58 if $c->req->param('a');
60 my @list = @{ $c->model()->projects };
61 die 'No projects found in '. $c->model->repo_dir
64 my $search = $c->req->param('s') || '';
67 index($_->name, $search) > -1
68 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
73 search_text => $search,
81 A summary of what's happening in the repo.
86 my ( $self, $c ) = @_;
87 my $project = $c->stash->{Project};
88 $c->detach('error_404') unless $project;
89 my $commit = $self->_get_object($c);
90 my @heads = @{$project->heads};
91 my $maxitems = Gitalist->config->{paging}{summary} || 10;
94 log_lines => [$project->list_revs(
95 sha1 => $commit->sha1,
98 refs => $project->references,
99 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
106 The current list of heads (aka branches) in the repo.
111 my ( $self, $c ) = @_;
112 my $project = $c->stash->{Project};
114 commit => $self->_get_object($c),
115 heads => $project->heads,
122 The current list of tags in the repo.
127 my ( $self, $c ) = @_;
128 my $project = $c->stash->{Project};
130 commit => $self->_get_object($c),
131 tags => $project->tags,
139 my $project = $c->stash->{Project};
140 my $h = $c->req->param('h')
141 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
142 || die "No file or sha1 provided.";
143 my $hb = $c->req->param('hb')
144 || $project->head_hash
145 || die "Couldn't discern the corresponding head.";
146 my $filename = $c->req->param('f') || '';
149 blame => $project->get_object($hb)->blame($filename),
150 head => $project->get_object($hb),
151 filename => $filename,
158 The blob action i.e the contents of a file.
163 my ( $self, $c ) = @_;
164 my $project = $c->stash->{Project};
165 my $h = $c->req->param('h')
166 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
167 || die "No file or sha1 provided.";
168 my $hb = $c->req->param('hb')
169 || $project->head_hash
170 || die "Couldn't discern the corresponding head.";
172 my $filename = $c->req->param('f') || '';
175 blob => $project->get_object($h)->content,
176 head => $project->get_object($hb),
177 filename => $filename,
178 # XXX Hack hack hack, see View::SyntaxHighlight
179 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
183 $c->forward('View::SyntaxHighlight')
184 unless $c->stash->{no_wrapper};
187 sub blob_plain : Local {
190 $c->stash(no_wrapper => 1);
191 $c->response->content_type('text/plain; charset=utf-8');
196 sub blobdiff_plain : Local {
199 $c->stash(no_wrapper => 1);
200 $c->response->content_type('text/plain; charset=utf-8');
202 $c->forward('blobdiff');
208 Exposes a given diff of a blob.
212 sub blobdiff : Local {
213 my ( $self, $c ) = @_;
214 my $commit = $self->_get_object($c, $c->req->param('hb'));
215 my $filename = $c->req->param('f')
216 || croak("No file specified!");
217 my($tree, $patch) = $c->stash->{Project}->diff(
220 parent => $c->req->param('hpb') || undef,
226 filename => $filename,
227 # XXX Hack hack hack, see View::SyntaxHighlight
228 blobs => [$patch->[0]->{diff}],
230 action => 'blobdiff',
233 $c->forward('View::SyntaxHighlight')
234 unless $c->stash->{no_wrapper};
239 Exposes a given commit.
244 my ( $self, $c ) = @_;
245 my $project = $c->stash->{Project};
246 my $commit = $self->_get_object($c);
249 diff_tree => ($project->diff(commit => $commit))[0],
250 refs => $project->references,
257 Exposes a given diff of a commit.
261 sub commitdiff : Local {
262 my ( $self, $c ) = @_;
263 my $commit = $self->_get_object($c);
264 my($tree, $patch) = $c->stash->{Project}->diff(
266 parent => $c->req->param('hp') || undef,
273 # XXX Hack hack hack, see View::SyntaxHighlight
274 blobs => [map $_->{diff}, @$patch],
276 action => 'commitdiff',
279 $c->forward('View::SyntaxHighlight')
280 unless $c->stash->{no_wrapper};
283 sub commitdiff_plain : Local {
286 $c->stash(no_wrapper => 1);
287 $c->response->content_type('text/plain; charset=utf-8');
289 $c->forward('commitdiff');
294 Expose an abbreviated log of a given sha1.
298 sub shortlog : Local {
299 my ( $self, $c ) = @_;
301 my $project = $c->stash->{Project};
302 my $commit = $self->_get_object($c);
303 my $filename = $c->req->param('f') || '';
306 sha1 => $commit->sha1,
307 count => Gitalist->config->{paging}{log} || 25,
308 ($filename ? (file => $filename) : ())
311 my $page = $c->req->param('pg') || 0;
312 $logargs{skip} = $c->req->param('pg') * $logargs{count}
313 if $c->req->param('pg');
317 log_lines => [$project->list_revs(%logargs)],
318 refs => $project->references,
320 filename => $filename,
321 action => 'shortlog',
327 Calls shortlog internally. Perhaps that should be reversed ...
331 $_[0]->shortlog($_[1]);
332 $_[1]->stash->{action} = 'log';
335 # For legacy support.
336 sub history : Local {
337 $_[0]->shortlog(@_[1 .. $#_]);
342 The tree of a given commit.
347 my ( $self, $c ) = @_;
348 my $project = $c->stash->{Project};
349 my $commit = $self->_get_object($c, $c->req->param('hb'));
350 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
354 tree_list => [$project->list_tree($tree->sha1)],
355 path => $c->req->param('f') || '',
362 Expose the local reflog. This may go away.
367 my ( $self, $c ) = @_;
368 my @log = $c->stash->{Project}->reflog(
380 The action for the search form.
386 $c->stash(current_action => 'GitRepos');
387 my $project = $c->stash->{Project};
388 my $commit = $self->_get_object($c);
389 # Lifted from /shortlog.
391 sha1 => $commit->sha1,
392 count => Gitalist->config->{paging}{log},
393 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
395 type => $c->req->param('type'),
396 text => $c->req->param('text'),
397 regexp => $c->req->param('regexp') || 0,
403 results => [$project->list_revs(%logargs)],
405 # This could be added - page => $page,
411 Provides some help for the search form.
415 sub search_help : Local {
417 $c->stash(template => 'search_help.tt2');
422 Provides an atom feed for a given project.
429 my $feed = XML::Atom::Feed->new;
431 my $host = lc Sys::Hostname::hostname();
432 $feed->title($host . ' - ' . Gitalist->config->{name});
433 $feed->updated(~~DateTime->now);
435 my $project = $c->stash->{Project};
437 sha1 => $project->head_hash,
438 count => Gitalist->config->{paging}{log} || 25,
439 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
442 my $mk_title = $c->stash->{short_cmt};
443 for my $commit ($project->list_revs(%logargs)) {
444 my $entry = XML::Atom::Entry->new;
445 $entry->title( $mk_title->($commit->comment) );
446 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
448 $entry->content($commit->comment);
449 $feed->add_entry($entry);
452 $c->response->body($feed->as_xml);
453 $c->response->content_type('application/atom+xml');
454 $c->response->status(200);
459 Provides an RSS feed for a given project.
466 my $project = $c->stash->{Project};
468 my $rss = XML::RSS->new(version => '2.0');
470 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
471 link => $c->uri_for('summary', {p=>$project->name}),
473 description => $project->description,
474 pubDate => DateTime->now,
475 lastBuildDate => DateTime->now,
479 sha1 => $project->head_hash,
480 count => Gitalist->config->{paging}{log} || 25,
481 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
483 my $mk_title = $c->stash->{short_cmt};
484 for my $commit ($project->list_revs(%logargs)) {
485 # XXX Needs work ....
487 title => $mk_title->($commit->comment),
488 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
489 description => $commit->comment,
493 $c->response->body($rss->as_string);
494 $c->response->content_type('application/rss+xml');
495 $c->response->status(200);
500 A raw patch for a given commit.
506 $c->detach('patches', [1]);
511 The patcheset for a given commit ???
515 sub patches : Local {
516 my ($self, $c, $count) = @_;
517 $count ||= Gitalist->config->{patches}{max};
518 my $commit = $self->_get_object($c);
519 my $parent = $c->req->param('hp') || undef;
520 my $patch = $commit->get_patch( $parent, $count );
521 $c->response->body($patch);
522 $c->response->content_type('text/plain');
523 $c->response->status(200);
528 Provides a snapshot of a given commit.
532 sub snapshot : Local {
534 my $format = $c->req->param('sf') || 'tgz';
536 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
537 my @snap = $c->stash->{Project}->snapshot(
541 $c->response->status(200);
542 $c->response->headers->header( 'Content-Disposition' =>
543 "attachment; filename=$snap[0]");
544 $c->response->body($snap[1]);
549 Populate the header and footer. Perhaps not the best location.
556 my $project = $c->req->param('p');
557 if (defined $project) {
559 $c->stash(Project => $c->model('GitRepos')->project($project));
562 $c->detach('error_404');
566 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
568 git_version => $a_project->run_cmd('--version'),
569 version => $Gitalist::VERSION,
571 # XXX Move these to a plugin!
573 return 'never' unless $_[0];
574 return age_string(time - $_[0]->epoch);
578 my($line) = split /\n/, $cmt;
579 $line =~ s/^(.{70,80}\b).*/$1 …/;
582 abridged_description => sub {
583 join(' ', grep { defined } (split / /, shift)[0..10]);
588 sub project_index : Local {
589 # FIXME - implement snapshot
590 Carp::croak "Not implemented.";
593 # FIXME - implement snapshot
594 Carp::croak "Not implemented.";
599 Attempt to render a view, if needed.
603 sub end : ActionClass('RenderView') {
605 # Give project views the current HEAD.
606 if ($c->stash->{Project}) {
607 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
611 sub error_404 :Private {
613 $c->response->status(404);
615 title => 'Page not found',
616 content => 'Page not found',
624 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
625 $age_str = ( int $age / 60 / 60 / 24 / 365 );
626 $age_str .= " years ago";
628 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
629 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
630 $age_str .= " months ago";
632 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
633 $age_str = int $age / 60 / 60 / 24 / 7;
634 $age_str .= " weeks ago";
636 elsif ( $age > 60 * 60 * 24 * 2 ) {
637 $age_str = int $age / 60 / 60 / 24;
638 $age_str .= " days ago";
640 elsif ( $age > 60 * 60 * 2 ) {
641 $age_str = int $age / 60 / 60;
642 $age_str .= " hours ago";
644 elsif ( $age > 60 * 2 ) {
645 $age_str = int $age / 60;
646 $age_str .= " min ago";
650 $age_str .= " sec ago";
653 $age_str .= " right now";
665 This library is free software. You can redistribute it and/or modify
666 it under the same terms as Perl itself.
670 __PACKAGE__->meta->make_immutable;