1 package Gitalist::Controller::Root;
3 use namespace::autoclean;
5 BEGIN { extends 'Catalyst::Controller' }
7 __PACKAGE__->config->{namespace} = '';
13 use XML::OPML::SimpleGen;
17 Gitalist::Controller::Root - Root Controller for Gitalist
21 [enter your description here]
28 my($self, $c, $haveh) = @_;
30 my $h = $haveh || $c->req->param('h') || '';
31 my $f = $c->req->param('f');
33 my $m = $c->stash->{Project};
36 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
37 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
38 || ($f && $m->hash_by_path($f))
40 # XXX This could definitely use more context.
41 || Carp::croak("Couldn't find a hash for the commit object!");
43 my $obj = $m->get_object($hash)
44 or Carp::croak("Couldn't find a object for '$hash' in '$pd'!");
51 Provides the project listing.
55 sub index :Path :Args(0) {
56 my ( $self, $c ) = @_;
58 $c->detach($c->req->param('a'))
59 if $c->req->param('a');
61 my @list = @{ $c->model()->projects };
62 die 'No projects found in '. $c->model->repo_dir
65 my $search = $c->req->param('s') || '';
68 index($_->name, $search) > -1
69 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
74 search_text => $search,
80 sub project_index : Local {
81 my ( $self, $c ) = @_;
83 my @list = @{ $c->model()->projects };
84 die 'No projects found in '. $c->model->repo_dir
87 $c->response->content_type('text/plain');
89 join "\n", map $_->name, @list
91 $c->response->status(200);
96 A summary of what's happening in the repo.
100 sub summary : Local {
101 my ( $self, $c ) = @_;
102 my $project = $c->stash->{Project};
103 $c->detach('error_404') unless $project;
104 my $commit = $self->_get_object($c);
105 my @heads = @{$project->heads};
106 my $maxitems = Gitalist->config->{paging}{summary} || 10;
109 log_lines => [$project->list_revs(
110 sha1 => $commit->sha1,
113 refs => $project->references,
114 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
121 The current list of heads (aka branches) in the repo.
126 my ( $self, $c ) = @_;
127 my $project = $c->stash->{Project};
129 commit => $self->_get_object($c),
130 heads => $project->heads,
137 The current list of tags in the repo.
142 my ( $self, $c ) = @_;
143 my $project = $c->stash->{Project};
145 commit => $self->_get_object($c),
146 tags => $project->tags,
154 my $project = $c->stash->{Project};
155 my $h = $c->req->param('h')
156 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
157 || die "No file or sha1 provided.";
158 my $hb = $c->req->param('hb')
159 || $project->head_hash
160 || die "Couldn't discern the corresponding head.";
161 my $filename = $c->req->param('f') || '';
163 my $blame = $project->get_object($hb)->blame($filename);
166 head => $project->get_object($hb),
167 filename => $filename,
169 # XXX Hack hack hack, see View::SyntaxHighlight
170 language => ($filename =~ /\.p[lm]$/i ? 'Perl' : ''),
171 blob => join("\n", map $_->{line}, @$blame),
174 $c->forward('View::SyntaxHighlight')
175 unless $c->stash->{no_wrapper};
179 my ( $self, $c ) = @_;
180 my $project = $c->stash->{Project};
181 my $h = $c->req->param('h')
182 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
183 || die "No file or sha1 provided.";
184 my $hb = $c->req->param('hb')
185 || $project->head_hash
186 || die "Couldn't discern the corresponding head.";
188 my $filename = $c->req->param('f') || '';
190 my $blob = $project->get_object($h);
191 $blob = $project->get_object(
192 $project->hash_by_path($h || $hb, $filename)
193 ) if $blob->type ne 'blob';
195 return $blob, $project->get_object($hb), $filename;
200 The blob action i.e the contents of a file.
205 my ( $self, $c ) = @_;
207 my($blob, $head, $filename) = $self->_blob_objs($c);
209 blob => $blob->content,
211 filename => $filename,
212 # XXX Hack hack hack, see View::SyntaxHighlight
213 language => ($filename =~ /\.p[lm]$/i ? 'Perl' : ''),
217 $c->forward('View::SyntaxHighlight')
218 unless $c->stash->{no_wrapper};
223 The plain text version of blob, where file is rendered as is.
227 sub blob_plain : Local {
230 my($blob) = $self->_blob_objs($c);
231 $c->response->content_type('text/plain; charset=utf-8');
232 $c->response->body($blob->content);
233 $c->response->status(200);
236 =head2 blobdiff_plain
238 The plain text version of blobdiff.
242 sub blobdiff_plain : Local {
245 $c->stash(no_wrapper => 1);
246 $c->response->content_type('text/plain; charset=utf-8');
248 $c->forward('blobdiff');
253 Exposes a given diff of a blob.
257 sub blobdiff : Local {
258 my ( $self, $c ) = @_;
259 my $commit = $self->_get_object($c, $c->req->param('hb'));
260 my $filename = $c->req->param('f')
261 || croak("No file specified!");
262 my($tree, $patch) = $c->stash->{Project}->diff(
265 parent => $c->req->param('hpb') || undef,
271 filename => $filename,
272 # XXX Hack hack hack, see View::SyntaxHighlight
273 blobs => [$patch->[0]->{diff}],
275 action => 'blobdiff',
278 $c->forward('View::SyntaxHighlight')
279 unless $c->stash->{no_wrapper};
284 Exposes a given commit.
289 my ( $self, $c ) = @_;
290 my $project = $c->stash->{Project};
291 my $commit = $self->_get_object($c);
294 diff_tree => ($project->diff(commit => $commit))[0],
295 refs => $project->references,
302 Exposes a given diff of a commit.
306 sub commitdiff : Local {
307 my ( $self, $c ) = @_;
308 my $commit = $self->_get_object($c);
309 my($tree, $patch) = $c->stash->{Project}->diff(
311 parent => $c->req->param('hp') || undef,
318 # XXX Hack hack hack, see View::SyntaxHighlight
319 blobs => [map $_->{diff}, @$patch],
321 action => 'commitdiff',
324 $c->forward('View::SyntaxHighlight')
325 unless $c->stash->{no_wrapper};
328 sub commitdiff_plain : Local {
331 $c->stash(no_wrapper => 1);
332 $c->response->content_type('text/plain; charset=utf-8');
334 $c->forward('commitdiff');
339 Expose an abbreviated log of a given sha1.
343 sub shortlog : Local {
344 my ( $self, $c ) = @_;
346 my $project = $c->stash->{Project};
347 my $commit = $self->_get_object($c, $c->req->param('hb'));
348 my $filename = $c->req->param('f') || '';
351 sha1 => $commit->sha1,
352 count => Gitalist->config->{paging}{log} || 25,
353 ($filename ? (file => $filename) : ())
356 my $page = $c->req->param('pg') || 0;
357 $logargs{skip} = $c->req->param('pg') * $logargs{count}
358 if $c->req->param('pg');
362 log_lines => [$project->list_revs(%logargs)],
363 refs => $project->references,
365 filename => $filename,
366 action => 'shortlog',
372 Calls shortlog internally. Perhaps that should be reversed ...
376 $_[0]->shortlog($_[1]);
377 $_[1]->stash->{action} = 'log';
380 # For legacy support.
381 sub history : Local {
382 $_[1]->forward('shortlog');
387 The tree of a given commit.
392 my ( $self, $c ) = @_;
393 my $project = $c->stash->{Project};
394 my $commit = $self->_get_object($c, $c->req->param('hb'));
395 my $filename = $c->req->param('f') || '';
397 ? $project->get_object($project->hash_by_path($commit->sha1, $filename))
398 : $project->get_object($commit->tree_sha1)
403 tree_list => [$project->list_tree($tree->sha1)],
404 path => $c->req->param('f') || '',
411 Expose the local reflog. This may go away.
416 my ( $self, $c ) = @_;
417 my @log = $c->stash->{Project}->reflog(
429 The action for the search form.
435 $c->stash(current_action => 'GitRepos');
436 my $project = $c->stash->{Project};
437 my $commit = $self->_get_object($c);
438 # Lifted from /shortlog.
440 sha1 => $commit->sha1,
441 count => Gitalist->config->{paging}{log},
442 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
444 type => $c->req->param('type'),
445 text => $c->req->param('text'),
446 regexp => $c->req->param('regexp') || 0,
452 results => [$project->list_revs(%logargs)],
454 # This could be added - page => $page,
460 Provides some help for the search form.
464 sub search_help : Local {
466 $c->stash(template => 'search_help.tt2');
471 Provides an atom feed for a given project.
478 my $feed = XML::Atom::Feed->new;
480 my $host = lc Sys::Hostname::hostname();
481 $feed->title($host . ' - ' . Gitalist->config->{name});
482 $feed->updated(~~DateTime->now);
484 my $project = $c->stash->{Project};
486 sha1 => $project->head_hash,
487 count => Gitalist->config->{paging}{log} || 25,
488 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
491 my $mk_title = $c->stash->{short_cmt};
492 for my $commit ($project->list_revs(%logargs)) {
493 my $entry = XML::Atom::Entry->new;
494 $entry->title( $mk_title->($commit->comment) );
495 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
497 $entry->content($commit->comment);
498 $feed->add_entry($entry);
501 $c->response->body($feed->as_xml);
502 $c->response->content_type('application/atom+xml');
503 $c->response->status(200);
508 Provides an RSS feed for a given project.
515 my $project = $c->stash->{Project};
517 my $rss = XML::RSS->new(version => '2.0');
519 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
520 link => $c->uri_for('summary', {p=>$project->name}),
522 description => $project->description,
523 pubDate => DateTime->now,
524 lastBuildDate => DateTime->now,
528 sha1 => $project->head_hash,
529 count => Gitalist->config->{paging}{log} || 25,
530 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
532 my $mk_title = $c->stash->{short_cmt};
533 for my $commit ($project->list_revs(%logargs)) {
534 # XXX Needs work ....
536 title => $mk_title->($commit->comment),
537 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
538 description => $commit->comment,
542 $c->response->body($rss->as_string);
543 $c->response->content_type('application/rss+xml');
544 $c->response->status(200);
550 my $opml = XML::OPML::SimpleGen->new();
552 $opml->head(title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name});
554 my @list = @{ $c->model()->projects };
555 die 'No projects found in '. $c->model->repo_dir
558 for my $proj ( @list ) {
559 $opml->insert_outline(
560 text => $proj->name. ' - '. $proj->description,
561 xmlUrl => $c->uri_for(rss => {p => $proj->name}),
565 $c->response->body($opml->as_string);
566 $c->response->content_type('application/rss');
567 $c->response->status(200);
572 A raw patch for a given commit.
578 $c->detach('patches', [1]);
583 The patcheset for a given commit ???
587 sub patches : Local {
588 my ($self, $c, $count) = @_;
589 $count ||= Gitalist->config->{patches}{max};
590 my $commit = $self->_get_object($c);
591 my $parent = $c->req->param('hp') || undef;
592 my $patch = $commit->get_patch( $parent, $count );
593 $c->response->body($patch);
594 $c->response->content_type('text/plain');
595 $c->response->status(200);
600 Provides a snapshot of a given commit.
604 sub snapshot : Local {
606 my $format = $c->req->param('sf') || 'tgz';
608 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
609 my @snap = $c->stash->{Project}->snapshot(
613 $c->response->status(200);
614 $c->response->headers->header( 'Content-Disposition' =>
615 "attachment; filename=$snap[0]");
616 $c->response->body($snap[1]);
621 Populate the header and footer. Perhaps not the best location.
628 my $project = $c->req->param('p');
629 if (defined $project) {
631 $c->stash(Project => $c->model('GitRepos')->project($project));
634 $c->detach('error_404');
638 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
640 git_version => $a_project->run_cmd('--version'),
641 version => $Gitalist::VERSION,
643 # XXX Move these to a plugin!
645 return 'never' unless $_[0];
646 return age_string(time - $_[0]->epoch);
650 my($line) = split /\n/, $cmt;
651 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
654 abridged_description => sub {
655 join(' ', grep { defined } (split / /, shift)[0..10]);
662 Attempt to render a view, if needed.
666 sub end : ActionClass('RenderView') {
668 # Give project views the current HEAD.
669 if ($c->stash->{Project}) {
670 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
674 sub error_404 :Private {
676 $c->response->status(404);
678 title => 'Page not found',
679 content => 'Page not found',
687 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
688 $age_str = ( int $age / 60 / 60 / 24 / 365 );
689 $age_str .= " years ago";
691 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
692 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
693 $age_str .= " months ago";
695 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
696 $age_str = int $age / 60 / 60 / 24 / 7;
697 $age_str .= " weeks ago";
699 elsif ( $age > 60 * 60 * 24 * 2 ) {
700 $age_str = int $age / 60 / 60 / 24;
701 $age_str .= " days ago";
703 elsif ( $age > 60 * 60 * 2 ) {
704 $age_str = int $age / 60 / 60;
705 $age_str .= " hours ago";
707 elsif ( $age > 60 * 2 ) {
708 $age_str = int $age / 60;
709 $age_str .= " min ago";
713 $age_str .= " sec ago";
716 $age_str .= " right now";
721 __PACKAGE__->meta->make_immutable;
727 Gitalist::Controller::Root - Root controller for the application
731 This controller handles all of the root level paths for the application
739 =head2 commitdiff_plain
751 See L<Gitalist> for authors.
755 See L<Gitalist> for the license.