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,
172 The blob action i.e the contents of a file.
177 my ( $self, $c ) = @_;
178 my $project = $c->stash->{Project};
179 my $h = $c->req->param('h')
180 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
181 || die "No file or sha1 provided.";
182 my $hb = $c->req->param('hb')
183 || $project->head_hash
184 || die "Couldn't discern the corresponding head.";
186 my $filename = $c->req->param('f') || '';
189 blob => $project->get_object($h)->content,
190 head => $project->get_object($hb),
191 filename => $filename,
192 # XXX Hack hack hack, see View::SyntaxHighlight
193 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
197 $c->forward('View::SyntaxHighlight')
198 unless $c->stash->{no_wrapper};
201 sub blob_plain : Local {
204 $c->stash(no_wrapper => 1);
205 $c->response->content_type('text/plain; charset=utf-8');
210 sub blobdiff_plain : Local {
213 $c->stash(no_wrapper => 1);
214 $c->response->content_type('text/plain; charset=utf-8');
216 $c->forward('blobdiff');
222 Exposes a given diff of a blob.
226 sub blobdiff : Local {
227 my ( $self, $c ) = @_;
228 my $commit = $self->_get_object($c, $c->req->param('hb'));
229 my $filename = $c->req->param('f')
230 || croak("No file specified!");
231 my($tree, $patch) = $c->stash->{Project}->diff(
234 parent => $c->req->param('hpb') || undef,
240 filename => $filename,
241 # XXX Hack hack hack, see View::SyntaxHighlight
242 blobs => [$patch->[0]->{diff}],
244 action => 'blobdiff',
247 $c->forward('View::SyntaxHighlight')
248 unless $c->stash->{no_wrapper};
253 Exposes a given commit.
258 my ( $self, $c ) = @_;
259 my $project = $c->stash->{Project};
260 my $commit = $self->_get_object($c);
263 diff_tree => ($project->diff(commit => $commit))[0],
264 refs => $project->references,
271 Exposes a given diff of a commit.
275 sub commitdiff : Local {
276 my ( $self, $c ) = @_;
277 my $commit = $self->_get_object($c);
278 my($tree, $patch) = $c->stash->{Project}->diff(
280 parent => $c->req->param('hp') || undef,
287 # XXX Hack hack hack, see View::SyntaxHighlight
288 blobs => [map $_->{diff}, @$patch],
290 action => 'commitdiff',
293 $c->forward('View::SyntaxHighlight')
294 unless $c->stash->{no_wrapper};
297 sub commitdiff_plain : Local {
300 $c->stash(no_wrapper => 1);
301 $c->response->content_type('text/plain; charset=utf-8');
303 $c->forward('commitdiff');
308 Expose an abbreviated log of a given sha1.
312 sub shortlog : Local {
313 my ( $self, $c ) = @_;
315 my $project = $c->stash->{Project};
316 my $commit = $self->_get_object($c);
317 my $filename = $c->req->param('f') || '';
320 sha1 => $commit->sha1,
321 count => Gitalist->config->{paging}{log} || 25,
322 ($filename ? (file => $filename) : ())
325 my $page = $c->req->param('pg') || 0;
326 $logargs{skip} = $c->req->param('pg') * $logargs{count}
327 if $c->req->param('pg');
331 log_lines => [$project->list_revs(%logargs)],
332 refs => $project->references,
334 filename => $filename,
335 action => 'shortlog',
341 Calls shortlog internally. Perhaps that should be reversed ...
345 $_[0]->shortlog($_[1]);
346 $_[1]->stash->{action} = 'log';
349 # For legacy support.
350 sub history : Local {
351 $_[0]->shortlog(@_[1 .. $#_]);
356 The tree of a given commit.
361 my ( $self, $c ) = @_;
362 my $project = $c->stash->{Project};
363 my $commit = $self->_get_object($c, $c->req->param('hb'));
364 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
368 tree_list => [$project->list_tree($tree->sha1)],
369 path => $c->req->param('f') || '',
376 Expose the local reflog. This may go away.
381 my ( $self, $c ) = @_;
382 my @log = $c->stash->{Project}->reflog(
394 The action for the search form.
400 $c->stash(current_action => 'GitRepos');
401 my $project = $c->stash->{Project};
402 my $commit = $self->_get_object($c);
403 # Lifted from /shortlog.
405 sha1 => $commit->sha1,
406 count => Gitalist->config->{paging}{log},
407 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
409 type => $c->req->param('type'),
410 text => $c->req->param('text'),
411 regexp => $c->req->param('regexp') || 0,
417 results => [$project->list_revs(%logargs)],
419 # This could be added - page => $page,
425 Provides some help for the search form.
429 sub search_help : Local {
431 $c->stash(template => 'search_help.tt2');
436 Provides an atom feed for a given project.
443 my $feed = XML::Atom::Feed->new;
445 my $host = lc Sys::Hostname::hostname();
446 $feed->title($host . ' - ' . Gitalist->config->{name});
447 $feed->updated(~~DateTime->now);
449 my $project = $c->stash->{Project};
451 sha1 => $project->head_hash,
452 count => Gitalist->config->{paging}{log} || 25,
453 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
456 my $mk_title = $c->stash->{short_cmt};
457 for my $commit ($project->list_revs(%logargs)) {
458 my $entry = XML::Atom::Entry->new;
459 $entry->title( $mk_title->($commit->comment) );
460 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
462 $entry->content($commit->comment);
463 $feed->add_entry($entry);
466 $c->response->body($feed->as_xml);
467 $c->response->content_type('application/atom+xml');
468 $c->response->status(200);
473 Provides an RSS feed for a given project.
480 my $project = $c->stash->{Project};
482 my $rss = XML::RSS->new(version => '2.0');
484 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
485 link => $c->uri_for('summary', {p=>$project->name}),
487 description => $project->description,
488 pubDate => DateTime->now,
489 lastBuildDate => DateTime->now,
493 sha1 => $project->head_hash,
494 count => Gitalist->config->{paging}{log} || 25,
495 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
497 my $mk_title = $c->stash->{short_cmt};
498 for my $commit ($project->list_revs(%logargs)) {
499 # XXX Needs work ....
501 title => $mk_title->($commit->comment),
502 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
503 description => $commit->comment,
507 $c->response->body($rss->as_string);
508 $c->response->content_type('application/rss+xml');
509 $c->response->status(200);
514 A raw patch for a given commit.
520 $c->detach('patches', [1]);
525 The patcheset for a given commit ???
529 sub patches : Local {
530 my ($self, $c, $count) = @_;
531 $count ||= Gitalist->config->{patches}{max};
532 my $commit = $self->_get_object($c);
533 my $parent = $c->req->param('hp') || undef;
534 my $patch = $commit->get_patch( $parent, $count );
535 $c->response->body($patch);
536 $c->response->content_type('text/plain');
537 $c->response->status(200);
542 Provides a snapshot of a given commit.
546 sub snapshot : Local {
548 my $format = $c->req->param('sf') || 'tgz';
550 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
551 my @snap = $c->stash->{Project}->snapshot(
555 $c->response->status(200);
556 $c->response->headers->header( 'Content-Disposition' =>
557 "attachment; filename=$snap[0]");
558 $c->response->body($snap[1]);
563 Populate the header and footer. Perhaps not the best location.
570 my $project = $c->req->param('p');
571 if (defined $project) {
573 $c->stash(Project => $c->model('GitRepos')->project($project));
576 $c->detach('error_404');
580 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
582 git_version => $a_project->run_cmd('--version'),
583 version => $Gitalist::VERSION,
585 # XXX Move these to a plugin!
587 return 'never' unless $_[0];
588 return age_string(time - $_[0]->epoch);
592 my($line) = split /\n/, $cmt;
593 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
596 abridged_description => sub {
597 join(' ', grep { defined } (split / /, shift)[0..10]);
603 # FIXME - implement snapshot
604 Carp::croak "Not implemented.";
609 Attempt to render a view, if needed.
613 sub end : ActionClass('RenderView') {
615 # Give project views the current HEAD.
616 if ($c->stash->{Project}) {
617 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
621 sub error_404 :Private {
623 $c->response->status(404);
625 title => 'Page not found',
626 content => 'Page not found',
634 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
635 $age_str = ( int $age / 60 / 60 / 24 / 365 );
636 $age_str .= " years ago";
638 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
639 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
640 $age_str .= " months ago";
642 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
643 $age_str = int $age / 60 / 60 / 24 / 7;
644 $age_str .= " weeks ago";
646 elsif ( $age > 60 * 60 * 24 * 2 ) {
647 $age_str = int $age / 60 / 60 / 24;
648 $age_str .= " days ago";
650 elsif ( $age > 60 * 60 * 2 ) {
651 $age_str = int $age / 60 / 60;
652 $age_str .= " hours ago";
654 elsif ( $age > 60 * 2 ) {
655 $age_str = int $age / 60;
656 $age_str .= " min ago";
660 $age_str .= " sec ago";
663 $age_str .= " right now";
675 This library is free software. You can redistribute it and/or modify
676 it under the same terms as Perl itself.
680 __PACKAGE__->meta->make_immutable;