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,
79 sub project_index : Local {
80 my ( $self, $c ) = @_;
82 my @list = @{ $c->model()->projects };
83 die 'No projects found in '. $c->model->repo_dir
86 $c->response->content_type('text/plain');
88 join "\n", map $_->name, @list
90 $c->response->status(200);
95 A summary of what's happening in the repo.
100 my ( $self, $c ) = @_;
101 my $project = $c->stash->{Project};
102 $c->detach('error_404') unless $project;
103 my $commit = $self->_get_object($c);
104 my @heads = @{$project->heads};
105 my $maxitems = Gitalist->config->{paging}{summary} || 10;
108 log_lines => [$project->list_revs(
109 sha1 => $commit->sha1,
112 refs => $project->references,
113 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
120 The current list of heads (aka branches) in the repo.
125 my ( $self, $c ) = @_;
126 my $project = $c->stash->{Project};
128 commit => $self->_get_object($c),
129 heads => $project->heads,
136 The current list of tags in the repo.
141 my ( $self, $c ) = @_;
142 my $project = $c->stash->{Project};
144 commit => $self->_get_object($c),
145 tags => $project->tags,
153 my $project = $c->stash->{Project};
154 my $h = $c->req->param('h')
155 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
156 || die "No file or sha1 provided.";
157 my $hb = $c->req->param('hb')
158 || $project->head_hash
159 || die "Couldn't discern the corresponding head.";
160 my $filename = $c->req->param('f') || '';
163 blame => $project->get_object($hb)->blame($filename),
164 head => $project->get_object($hb),
165 filename => $filename,
171 my ( $self, $c ) = @_;
172 my $project = $c->stash->{Project};
173 my $h = $c->req->param('h')
174 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
175 || die "No file or sha1 provided.";
176 my $hb = $c->req->param('hb')
177 || $project->head_hash
178 || die "Couldn't discern the corresponding head.";
180 my $filename = $c->req->param('f') || '';
182 my $blob = $project->get_object($h);
183 $blob = $project->get_object(
184 $project->hash_by_path($h || $hb, $filename)
185 ) if $blob->type ne 'blob';
187 return $blob, $project->get_object($hb), $filename;
192 The blob action i.e the contents of a file.
197 my ( $self, $c ) = @_;
199 my($blob, $head, $filename) = $self->_blob_objs($c);
201 blob => $blob->content,
203 filename => $filename,
204 # XXX Hack hack hack, see View::SyntaxHighlight
205 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
209 $c->forward('View::SyntaxHighlight')
210 unless $c->stash->{no_wrapper};
215 The plain text version of blob, where file is rendered as is.
219 sub blob_plain : Local {
222 my($blob) = $self->_blob_objs($c);
223 $c->response->content_type('text/plain; charset=utf-8');
224 $c->response->body($blob->content);
225 $c->response->status(200);
228 =head2 blobdiff_plain
230 The plain text version of blobdiff.
234 sub blobdiff_plain : Local {
237 $c->stash(no_wrapper => 1);
238 $c->response->content_type('text/plain; charset=utf-8');
240 $c->forward('blobdiff');
245 Exposes a given diff of a blob.
249 sub blobdiff : Local {
250 my ( $self, $c ) = @_;
251 my $commit = $self->_get_object($c, $c->req->param('hb'));
252 my $filename = $c->req->param('f')
253 || croak("No file specified!");
254 my($tree, $patch) = $c->stash->{Project}->diff(
257 parent => $c->req->param('hpb') || undef,
263 filename => $filename,
264 # XXX Hack hack hack, see View::SyntaxHighlight
265 blobs => [$patch->[0]->{diff}],
267 action => 'blobdiff',
270 $c->forward('View::SyntaxHighlight')
271 unless $c->stash->{no_wrapper};
276 Exposes a given commit.
281 my ( $self, $c ) = @_;
282 my $project = $c->stash->{Project};
283 my $commit = $self->_get_object($c);
286 diff_tree => ($project->diff(commit => $commit))[0],
287 refs => $project->references,
294 Exposes a given diff of a commit.
298 sub commitdiff : Local {
299 my ( $self, $c ) = @_;
300 my $commit = $self->_get_object($c);
301 my($tree, $patch) = $c->stash->{Project}->diff(
303 parent => $c->req->param('hp') || undef,
310 # XXX Hack hack hack, see View::SyntaxHighlight
311 blobs => [map $_->{diff}, @$patch],
313 action => 'commitdiff',
316 $c->forward('View::SyntaxHighlight')
317 unless $c->stash->{no_wrapper};
320 sub commitdiff_plain : Local {
323 $c->stash(no_wrapper => 1);
324 $c->response->content_type('text/plain; charset=utf-8');
326 $c->forward('commitdiff');
331 Expose an abbreviated log of a given sha1.
335 sub shortlog : Local {
336 my ( $self, $c ) = @_;
338 my $project = $c->stash->{Project};
339 my $commit = $self->_get_object($c);
340 my $filename = $c->req->param('f') || '';
343 sha1 => $commit->sha1,
344 count => Gitalist->config->{paging}{log} || 25,
345 ($filename ? (file => $filename) : ())
348 my $page = $c->req->param('pg') || 0;
349 $logargs{skip} = $c->req->param('pg') * $logargs{count}
350 if $c->req->param('pg');
354 log_lines => [$project->list_revs(%logargs)],
355 refs => $project->references,
357 filename => $filename,
358 action => 'shortlog',
364 Calls shortlog internally. Perhaps that should be reversed ...
368 $_[0]->shortlog($_[1]);
369 $_[1]->stash->{action} = 'log';
372 # For legacy support.
373 sub history : Local {
374 $_[0]->shortlog(@_[1 .. $#_]);
379 The tree of a given commit.
384 my ( $self, $c ) = @_;
385 my $project = $c->stash->{Project};
386 my $commit = $self->_get_object($c, $c->req->param('hb'));
387 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
391 tree_list => [$project->list_tree($tree->sha1)],
392 path => $c->req->param('f') || '',
399 Expose the local reflog. This may go away.
404 my ( $self, $c ) = @_;
405 my @log = $c->stash->{Project}->reflog(
417 The action for the search form.
423 $c->stash(current_action => 'GitRepos');
424 my $project = $c->stash->{Project};
425 my $commit = $self->_get_object($c);
426 # Lifted from /shortlog.
428 sha1 => $commit->sha1,
429 count => Gitalist->config->{paging}{log},
430 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
432 type => $c->req->param('type'),
433 text => $c->req->param('text'),
434 regexp => $c->req->param('regexp') || 0,
440 results => [$project->list_revs(%logargs)],
442 # This could be added - page => $page,
448 Provides some help for the search form.
452 sub search_help : Local {
454 $c->stash(template => 'search_help.tt2');
459 Provides an atom feed for a given project.
466 my $feed = XML::Atom::Feed->new;
468 my $host = lc Sys::Hostname::hostname();
469 $feed->title($host . ' - ' . Gitalist->config->{name});
470 $feed->updated(~~DateTime->now);
472 my $project = $c->stash->{Project};
474 sha1 => $project->head_hash,
475 count => Gitalist->config->{paging}{log} || 25,
476 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
479 my $mk_title = $c->stash->{short_cmt};
480 for my $commit ($project->list_revs(%logargs)) {
481 my $entry = XML::Atom::Entry->new;
482 $entry->title( $mk_title->($commit->comment) );
483 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
485 $entry->content($commit->comment);
486 $feed->add_entry($entry);
489 $c->response->body($feed->as_xml);
490 $c->response->content_type('application/atom+xml');
491 $c->response->status(200);
496 Provides an RSS feed for a given project.
503 my $project = $c->stash->{Project};
505 my $rss = XML::RSS->new(version => '2.0');
507 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
508 link => $c->uri_for('summary', {p=>$project->name}),
510 description => $project->description,
511 pubDate => DateTime->now,
512 lastBuildDate => DateTime->now,
516 sha1 => $project->head_hash,
517 count => Gitalist->config->{paging}{log} || 25,
518 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
520 my $mk_title = $c->stash->{short_cmt};
521 for my $commit ($project->list_revs(%logargs)) {
522 # XXX Needs work ....
524 title => $mk_title->($commit->comment),
525 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
526 description => $commit->comment,
530 $c->response->body($rss->as_string);
531 $c->response->content_type('application/rss+xml');
532 $c->response->status(200);
537 A raw patch for a given commit.
543 $c->detach('patches', [1]);
548 The patcheset for a given commit ???
552 sub patches : Local {
553 my ($self, $c, $count) = @_;
554 $count ||= Gitalist->config->{patches}{max};
555 my $commit = $self->_get_object($c);
556 my $parent = $c->req->param('hp') || undef;
557 my $patch = $commit->get_patch( $parent, $count );
558 $c->response->body($patch);
559 $c->response->content_type('text/plain');
560 $c->response->status(200);
565 Provides a snapshot of a given commit.
569 sub snapshot : Local {
571 my $format = $c->req->param('sf') || 'tgz';
573 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
574 my @snap = $c->stash->{Project}->snapshot(
578 $c->response->status(200);
579 $c->response->headers->header( 'Content-Disposition' =>
580 "attachment; filename=$snap[0]");
581 $c->response->body($snap[1]);
586 Populate the header and footer. Perhaps not the best location.
593 my $project = $c->req->param('p');
594 if (defined $project) {
596 $c->stash(Project => $c->model('GitRepos')->project($project));
599 $c->detach('error_404');
603 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
605 git_version => $a_project->run_cmd('--version'),
606 version => $Gitalist::VERSION,
608 # XXX Move these to a plugin!
610 return 'never' unless $_[0];
611 return age_string(time - $_[0]->epoch);
615 my($line) = split /\n/, $cmt;
616 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
619 abridged_description => sub {
620 join(' ', grep { defined } (split / /, shift)[0..10]);
626 # FIXME - implement snapshot
627 Carp::croak "Not implemented.";
632 Attempt to render a view, if needed.
636 sub end : ActionClass('RenderView') {
638 # Give project views the current HEAD.
639 if ($c->stash->{Project}) {
640 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
644 sub error_404 :Private {
646 $c->response->status(404);
648 title => 'Page not found',
649 content => 'Page not found',
657 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
658 $age_str = ( int $age / 60 / 60 / 24 / 365 );
659 $age_str .= " years ago";
661 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
662 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
663 $age_str .= " months ago";
665 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
666 $age_str = int $age / 60 / 60 / 24 / 7;
667 $age_str .= " weeks ago";
669 elsif ( $age > 60 * 60 * 24 * 2 ) {
670 $age_str = int $age / 60 / 60 / 24;
671 $age_str .= " days ago";
673 elsif ( $age > 60 * 60 * 2 ) {
674 $age_str = int $age / 60 / 60;
675 $age_str .= " hours ago";
677 elsif ( $age > 60 * 2 ) {
678 $age_str = int $age / 60;
679 $age_str .= " min ago";
683 $age_str .= " sec ago";
686 $age_str .= " right now";
698 This library is free software. You can redistribute it and/or modify
699 it under the same terms as Perl itself.
703 __PACKAGE__->meta->make_immutable;