1 package Gitalist::Controller::Root;
3 use namespace::autoclean;
5 BEGIN { extends 'Catalyst::Controller' }
7 __PACKAGE__->config->{namespace} = '';
14 use XML::OPML::SimpleGen;
18 Gitalist::Controller::Root - Root Controller for Gitalist
22 [enter your description here]
29 my($self, $c, $haveh) = @_;
31 my $h = $haveh || $c->req->param('h') || '';
32 my $f = $c->req->param('f');
34 my $m = $c->stash->{Project};
37 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
38 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
39 || ($f && $m->hash_by_path($f))
41 # XXX This could definitely use more context.
42 || Carp::croak("Couldn't find a hash for the commit object!");
44 my $obj = $m->get_object($hash)
45 or Carp::croak("Couldn't find a object for '$hash' in '$pd'!");
52 Provides the project listing.
56 sub index :Path :Args(0) {
57 my ( $self, $c ) = @_;
59 $c->detach($c->req->param('a'))
60 if $c->req->param('a');
62 my @list = @{ $c->model()->projects };
63 die 'No projects found in '. $c->model->repo_dir
66 my $search = $c->req->param('s') || '';
69 index($_->name, $search) > -1
70 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
75 search_text => $search,
81 sub project_index : Local {
82 my ( $self, $c ) = @_;
84 my @list = @{ $c->model()->projects };
85 die 'No projects found in '. $c->model->repo_dir
88 $c->response->content_type('text/plain');
90 join "\n", map $_->name, @list
92 $c->response->status(200);
97 A summary of what's happening in the repo.
101 sub summary : Local {
102 my ( $self, $c ) = @_;
103 my $project = $c->stash->{Project};
104 $c->detach('error_404') unless $project;
105 my $commit = $self->_get_object($c);
106 my @heads = @{$project->heads};
107 my $maxitems = Gitalist->config->{paging}{summary} || 10;
110 log_lines => [$project->list_revs(
111 sha1 => $commit->sha1,
114 refs => $project->references,
115 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
122 The current list of heads (aka branches) in the repo.
127 my ( $self, $c ) = @_;
128 my $project = $c->stash->{Project};
130 commit => $self->_get_object($c),
131 heads => $project->heads,
138 The current list of tags in the repo.
143 my ( $self, $c ) = @_;
144 my $project = $c->stash->{Project};
146 commit => $self->_get_object($c),
147 tags => $project->tags,
155 my $project = $c->stash->{Project};
156 my $h = $c->req->param('h')
157 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
158 || die "No file or sha1 provided.";
159 my $hb = $c->req->param('hb')
160 || $project->head_hash
161 || die "Couldn't discern the corresponding head.";
162 my $filename = $c->req->param('f') || '';
165 blame => $project->get_object($hb)->blame($filename),
166 head => $project->get_object($hb),
167 filename => $filename,
173 my ( $self, $c ) = @_;
174 my $project = $c->stash->{Project};
175 my $h = $c->req->param('h')
176 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
177 || die "No file or sha1 provided.";
178 my $hb = $c->req->param('hb')
179 || $project->head_hash
180 || die "Couldn't discern the corresponding head.";
182 my $filename = $c->req->param('f') || '';
184 my $blob = $project->get_object($h);
185 $blob = $project->get_object(
186 $project->hash_by_path($h || $hb, $filename)
187 ) if $blob->type ne 'blob';
189 return $blob, $project->get_object($hb), $filename;
194 The blob action i.e the contents of a file.
199 my ( $self, $c ) = @_;
201 my($blob, $head, $filename) = $self->_blob_objs($c);
203 blob => $blob->content,
205 filename => $filename,
206 # XXX Hack hack hack, see View::SyntaxHighlight
207 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
211 $c->forward('View::SyntaxHighlight')
212 unless $c->stash->{no_wrapper};
217 The plain text version of blob, where file is rendered as is.
221 sub blob_plain : Local {
224 my($blob) = $self->_blob_objs($c);
225 $c->response->content_type('text/plain; charset=utf-8');
226 $c->response->body($blob->content);
227 $c->response->status(200);
230 =head2 blobdiff_plain
232 The plain text version of blobdiff.
236 sub blobdiff_plain : Local {
239 $c->stash(no_wrapper => 1);
240 $c->response->content_type('text/plain; charset=utf-8');
242 $c->forward('blobdiff');
247 Exposes a given diff of a blob.
251 sub blobdiff : Local {
252 my ( $self, $c ) = @_;
253 my $commit = $self->_get_object($c, $c->req->param('hb'));
254 my $filename = $c->req->param('f')
255 || croak("No file specified!");
256 my($tree, $patch) = $c->stash->{Project}->diff(
259 parent => $c->req->param('hpb') || undef,
265 filename => $filename,
266 # XXX Hack hack hack, see View::SyntaxHighlight
267 blobs => [$patch->[0]->{diff}],
269 action => 'blobdiff',
272 $c->forward('View::SyntaxHighlight')
273 unless $c->stash->{no_wrapper};
278 Exposes a given commit.
283 my ( $self, $c ) = @_;
284 my $project = $c->stash->{Project};
285 my $commit = $self->_get_object($c);
288 diff_tree => ($project->diff(commit => $commit))[0],
289 refs => $project->references,
296 Exposes a given diff of a commit.
300 sub commitdiff : Local {
301 my ( $self, $c ) = @_;
302 my $commit = $self->_get_object($c);
303 my($tree, $patch) = $c->stash->{Project}->diff(
305 parent => $c->req->param('hp') || undef,
312 # XXX Hack hack hack, see View::SyntaxHighlight
313 blobs => [map $_->{diff}, @$patch],
315 action => 'commitdiff',
318 $c->forward('View::SyntaxHighlight')
319 unless $c->stash->{no_wrapper};
322 sub commitdiff_plain : Local {
325 $c->stash(no_wrapper => 1);
326 $c->response->content_type('text/plain; charset=utf-8');
328 $c->forward('commitdiff');
333 Expose an abbreviated log of a given sha1.
337 sub shortlog : Local {
338 my ( $self, $c ) = @_;
340 my $project = $c->stash->{Project};
341 my $commit = $self->_get_object($c, $c->req->param('hb'));
342 my $filename = $c->req->param('f') || '';
345 sha1 => $commit->sha1,
346 count => Gitalist->config->{paging}{log} || 25,
347 ($filename ? (file => $filename) : ())
350 my $page = $c->req->param('pg') || 0;
351 $logargs{skip} = $c->req->param('pg') * $logargs{count}
352 if $c->req->param('pg');
356 log_lines => [$project->list_revs(%logargs)],
357 refs => $project->references,
359 filename => $filename,
360 action => 'shortlog',
366 Calls shortlog internally. Perhaps that should be reversed ...
370 $_[0]->shortlog($_[1]);
371 $_[1]->stash->{action} = 'log';
374 # For legacy support.
375 sub history : Local {
376 my ( $self, $c ) = @_;
378 my $project = $c->stash->{Project};
379 my $file = $project->get_object(
380 $project->hash_by_path(
382 $c->stash->{filename}
385 $c->stash( action => 'history',
386 filetype => $file->type,
392 The tree of a given commit.
397 my ( $self, $c ) = @_;
398 my $project = $c->stash->{Project};
399 my $commit = $self->_get_object($c, $c->req->param('hb'));
400 my $filename = $c->req->param('f') || '';
402 ? $project->get_object($project->hash_by_path($commit->sha1, $filename))
403 : $project->get_object($commit->tree_sha1)
408 tree_list => [$project->list_tree($tree->sha1)],
409 path => $c->req->param('f') || '',
416 Expose the local reflog. This may go away.
421 my ( $self, $c ) = @_;
422 my @log = $c->stash->{Project}->reflog(
434 The action for the search form.
440 $c->stash(current_action => 'GitRepos');
441 my $project = $c->stash->{Project};
442 my $commit = $self->_get_object($c);
443 # Lifted from /shortlog.
445 sha1 => $commit->sha1,
446 count => Gitalist->config->{paging}{log},
447 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
449 type => $c->req->param('type'),
450 text => $c->req->param('text'),
451 regexp => $c->req->param('regexp') || 0,
457 results => [$project->list_revs(%logargs)],
459 # This could be added - page => $page,
465 Provides some help for the search form.
469 sub search_help : Local {
471 $c->stash(template => 'search_help.tt2');
476 Provides an atom feed for a given project.
483 my $feed = XML::Atom::Feed->new;
485 my $host = lc Sys::Hostname::hostname();
486 $feed->title($host . ' - ' . Gitalist->config->{name});
487 $feed->updated(~~DateTime->now);
489 my $project = $c->stash->{Project};
491 sha1 => $project->head_hash,
492 count => Gitalist->config->{paging}{log} || 25,
493 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
496 my $mk_title = $c->stash->{short_cmt};
497 for my $commit ($project->list_revs(%logargs)) {
498 my $entry = XML::Atom::Entry->new;
499 $entry->title( $mk_title->($commit->comment) );
500 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
502 $entry->content($commit->comment);
503 $feed->add_entry($entry);
506 $c->response->body($feed->as_xml);
507 $c->response->content_type('application/atom+xml');
508 $c->response->status(200);
513 Provides an RSS feed for a given project.
520 my $project = $c->stash->{Project};
522 my $rss = XML::RSS->new(version => '2.0');
524 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
525 link => $c->uri_for('summary', {p=>$project->name}),
527 description => $project->description,
528 pubDate => DateTime->now,
529 lastBuildDate => DateTime->now,
533 sha1 => $project->head_hash,
534 count => Gitalist->config->{paging}{log} || 25,
535 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
537 my $mk_title = $c->stash->{short_cmt};
538 for my $commit ($project->list_revs(%logargs)) {
539 # XXX Needs work ....
541 title => $mk_title->($commit->comment),
542 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
543 description => $commit->comment,
547 $c->response->body($rss->as_string);
548 $c->response->content_type('application/rss+xml');
549 $c->response->status(200);
555 my $opml = XML::OPML::SimpleGen->new();
557 $opml->head(title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name});
559 my @list = @{ $c->model()->projects };
560 die 'No projects found in '. $c->model->repo_dir
563 for my $proj ( @list ) {
564 $opml->insert_outline(
565 text => $proj->name. ' - '. $proj->description,
566 xmlUrl => $c->uri_for(rss => {p => $proj->name}),
570 $c->response->body($opml->as_string);
571 $c->response->content_type('application/rss');
572 $c->response->status(200);
577 A raw patch for a given commit.
583 $c->detach('patches', [1]);
588 The patcheset for a given commit ???
592 sub patches : Local {
593 my ($self, $c, $count) = @_;
594 $count ||= Gitalist->config->{patches}{max};
595 my $commit = $self->_get_object($c);
596 my $parent = $c->req->param('hp') || undef;
597 my $patch = $commit->get_patch( $parent, $count );
598 $c->response->body($patch);
599 $c->response->content_type('text/plain');
600 $c->response->status(200);
605 Provides a snapshot of a given commit.
609 sub snapshot : Local {
611 my $format = $c->req->param('sf') || 'tgz';
613 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
614 my @snap = $c->stash->{Project}->snapshot(
618 $c->response->status(200);
619 $c->response->headers->header( 'Content-Disposition' =>
620 "attachment; filename=$snap[0]");
621 $c->response->body($snap[1]);
626 Populate the header and footer. Perhaps not the best location.
633 my $project = $c->req->param('p');
634 if (defined $project) {
636 $c->stash(Project => $c->model('GitRepos')->project($project));
639 $c->detach('error_404');
643 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
645 git_version => $a_project->run_cmd('--version'),
646 version => $Gitalist::VERSION,
648 # XXX Move these to a plugin!
650 return 'never' unless $_[0];
651 return age_string(time - $_[0]->epoch);
655 my($line) = split /\n/, $cmt;
656 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
659 abridged_description => sub {
660 join(' ', grep { defined } (split / /, shift)[0..10]);
667 Attempt to render a view, if needed.
671 sub end : ActionClass('RenderView') {
673 # Give project views the current HEAD.
674 if ($c->stash->{Project}) {
675 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
679 sub error_404 :Private {
681 $c->response->status(404);
683 title => 'Page not found',
684 content => 'Page not found',
692 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
693 $age_str = ( int $age / 60 / 60 / 24 / 365 );
694 $age_str .= " years ago";
696 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
697 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
698 $age_str .= " months ago";
700 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
701 $age_str = int $age / 60 / 60 / 24 / 7;
702 $age_str .= " weeks ago";
704 elsif ( $age > 60 * 60 * 24 * 2 ) {
705 $age_str = int $age / 60 / 60 / 24;
706 $age_str .= " days ago";
708 elsif ( $age > 60 * 60 * 2 ) {
709 $age_str = int $age / 60 / 60;
710 $age_str .= " hours ago";
712 elsif ( $age > 60 * 2 ) {
713 $age_str = int $age / 60;
714 $age_str .= " min ago";
718 $age_str .= " sec ago";
721 $age_str .= " right now";
726 __PACKAGE__->meta->make_immutable;
732 Gitalist::Controller::Root - Root controller for the application
736 This controller handles all of the root level paths for the application
744 =head2 commitdiff_plain
756 See L<Gitalist> for authors.
760 See L<Gitalist> for the license.