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 $obj = $m->get_object($hash)
43 or Carp::croak("Couldn't find a 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, $c->req->param('hb'));
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 $_[1]->forward('shortlog');
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 $filename = $c->req->param('f') || '';
389 ? $project->get_object($project->hash_by_path($commit->sha1, $filename))
390 : $project->get_object($commit->tree_sha1)
395 tree_list => [$project->list_tree($tree->sha1)],
396 path => $c->req->param('f') || '',
403 Expose the local reflog. This may go away.
408 my ( $self, $c ) = @_;
409 my @log = $c->stash->{Project}->reflog(
421 The action for the search form.
427 $c->stash(current_action => 'GitRepos');
428 my $project = $c->stash->{Project};
429 my $commit = $self->_get_object($c);
430 # Lifted from /shortlog.
432 sha1 => $commit->sha1,
433 count => Gitalist->config->{paging}{log},
434 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
436 type => $c->req->param('type'),
437 text => $c->req->param('text'),
438 regexp => $c->req->param('regexp') || 0,
444 results => [$project->list_revs(%logargs)],
446 # This could be added - page => $page,
452 Provides some help for the search form.
456 sub search_help : Local {
458 $c->stash(template => 'search_help.tt2');
463 Provides an atom feed for a given project.
470 my $feed = XML::Atom::Feed->new;
472 my $host = lc Sys::Hostname::hostname();
473 $feed->title($host . ' - ' . Gitalist->config->{name});
474 $feed->updated(~~DateTime->now);
476 my $project = $c->stash->{Project};
478 sha1 => $project->head_hash,
479 count => Gitalist->config->{paging}{log} || 25,
480 ($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 my $entry = XML::Atom::Entry->new;
486 $entry->title( $mk_title->($commit->comment) );
487 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
489 $entry->content($commit->comment);
490 $feed->add_entry($entry);
493 $c->response->body($feed->as_xml);
494 $c->response->content_type('application/atom+xml');
495 $c->response->status(200);
500 Provides an RSS feed for a given project.
507 my $project = $c->stash->{Project};
509 my $rss = XML::RSS->new(version => '2.0');
511 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
512 link => $c->uri_for('summary', {p=>$project->name}),
514 description => $project->description,
515 pubDate => DateTime->now,
516 lastBuildDate => DateTime->now,
520 sha1 => $project->head_hash,
521 count => Gitalist->config->{paging}{log} || 25,
522 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
524 my $mk_title = $c->stash->{short_cmt};
525 for my $commit ($project->list_revs(%logargs)) {
526 # XXX Needs work ....
528 title => $mk_title->($commit->comment),
529 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
530 description => $commit->comment,
534 $c->response->body($rss->as_string);
535 $c->response->content_type('application/rss+xml');
536 $c->response->status(200);
541 A raw patch for a given commit.
547 $c->detach('patches', [1]);
552 The patcheset for a given commit ???
556 sub patches : Local {
557 my ($self, $c, $count) = @_;
558 $count ||= Gitalist->config->{patches}{max};
559 my $commit = $self->_get_object($c);
560 my $parent = $c->req->param('hp') || undef;
561 my $patch = $commit->get_patch( $parent, $count );
562 $c->response->body($patch);
563 $c->response->content_type('text/plain');
564 $c->response->status(200);
569 Provides a snapshot of a given commit.
573 sub snapshot : Local {
575 my $format = $c->req->param('sf') || 'tgz';
577 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
578 my @snap = $c->stash->{Project}->snapshot(
582 $c->response->status(200);
583 $c->response->headers->header( 'Content-Disposition' =>
584 "attachment; filename=$snap[0]");
585 $c->response->body($snap[1]);
590 Populate the header and footer. Perhaps not the best location.
597 my $project = $c->req->param('p');
598 if (defined $project) {
600 $c->stash(Project => $c->model('GitRepos')->project($project));
603 $c->detach('error_404');
607 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
609 git_version => $a_project->run_cmd('--version'),
610 version => $Gitalist::VERSION,
612 # XXX Move these to a plugin!
614 return 'never' unless $_[0];
615 return age_string(time - $_[0]->epoch);
619 my($line) = split /\n/, $cmt;
620 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
623 abridged_description => sub {
624 join(' ', grep { defined } (split / /, shift)[0..10]);
630 # FIXME - implement snapshot
631 Carp::croak "Not implemented.";
636 Attempt to render a view, if needed.
640 sub end : ActionClass('RenderView') {
642 # Give project views the current HEAD.
643 if ($c->stash->{Project}) {
644 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
648 sub error_404 :Private {
650 $c->response->status(404);
652 title => 'Page not found',
653 content => 'Page not found',
661 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
662 $age_str = ( int $age / 60 / 60 / 24 / 365 );
663 $age_str .= " years ago";
665 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
666 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
667 $age_str .= " months ago";
669 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
670 $age_str = int $age / 60 / 60 / 24 / 7;
671 $age_str .= " weeks ago";
673 elsif ( $age > 60 * 60 * 24 * 2 ) {
674 $age_str = int $age / 60 / 60 / 24;
675 $age_str .= " days ago";
677 elsif ( $age > 60 * 60 * 2 ) {
678 $age_str = int $age / 60 / 60;
679 $age_str .= " hours ago";
681 elsif ( $age > 60 * 2 ) {
682 $age_str = int $age / 60;
683 $age_str .= " min ago";
687 $age_str .= " sec ago";
690 $age_str .= " right now";
702 This library is free software. You can redistribute it and/or modify
703 it under the same terms as Perl itself.
707 __PACKAGE__->meta->make_immutable;