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,
81 A summary of what's happening in the repo.
86 my ( $self, $c ) = @_;
87 my $project = $c->stash->{Project};
88 $c->detach('error_404') unless $project;
89 my $commit = $self->_get_object($c);
90 my @heads = @{$project->heads};
91 my $maxitems = Gitalist->config->{paging}{summary} || 10;
94 log_lines => [$project->list_revs(
95 sha1 => $commit->sha1,
98 refs => $project->references,
99 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
106 The current list of heads (aka branches) in the repo.
111 my ( $self, $c ) = @_;
112 my $project = $c->stash->{Project};
114 commit => $self->_get_object($c),
115 heads => $project->heads,
122 The current list of tags in the repo.
127 my ( $self, $c ) = @_;
128 my $project = $c->stash->{Project};
130 commit => $self->_get_object($c),
131 tags => $project->tags,
138 The blob action i.e the contents of a file.
143 my ( $self, $c ) = @_;
144 my $project = $c->stash->{Project};
145 my $h = $c->req->param('h')
146 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
147 || die "No file or sha1 provided.";
148 my $hb = $c->req->param('hb')
149 || $project->head_hash
150 || die "Couldn't discern the corresponding head.";
152 my $filename = $c->req->param('f') || '';
155 blob => $project->get_object($h)->content,
156 head => $project->get_object($hb),
157 filename => $filename,
158 # XXX Hack hack hack, see View::SyntaxHighlight
159 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
163 $c->forward('View::SyntaxHighlight')
164 unless $c->stash->{no_wrapper};
167 sub blob_plain : Local {
170 $c->stash(no_wrapper => 1);
171 $c->response->content_type('text/plain; charset=utf-8');
176 sub blobdiff_plain : Local {
179 $c->stash(no_wrapper => 1);
180 $c->response->content_type('text/plain; charset=utf-8');
182 $c->forward('blobdiff');
188 Exposes a given diff of a blob.
192 sub blobdiff : Local {
193 my ( $self, $c ) = @_;
194 my $commit = $self->_get_object($c, $c->req->param('hb'));
195 my $filename = $c->req->param('f')
196 || croak("No file specified!");
197 my($tree, $patch) = $c->stash->{Project}->diff(
200 parent => $c->req->param('hpb') || undef,
206 # XXX Hack hack hack, see View::SyntaxHighlight
207 blobs => [$patch->[0]->{diff}],
209 action => 'blobdiff',
212 $c->forward('View::SyntaxHighlight')
213 unless $c->stash->{no_wrapper};
218 Exposes a given commit.
223 my ( $self, $c ) = @_;
224 my $project = $c->stash->{Project};
225 my $commit = $self->_get_object($c);
228 diff_tree => ($project->diff(commit => $commit))[0],
229 refs => $project->references,
236 Exposes a given diff of a commit.
240 sub commitdiff : Local {
241 my ( $self, $c ) = @_;
242 my $commit = $self->_get_object($c);
243 my($tree, $patch) = $c->stash->{Project}->diff(
245 parent => $c->req->param('hp') || undef,
252 # XXX Hack hack hack, see View::SyntaxHighlight
253 blobs => [map $_->{diff}, @$patch],
255 action => 'commitdiff',
258 $c->forward('View::SyntaxHighlight')
259 unless $c->stash->{no_wrapper};
262 sub commitdiff_plain : Local {
265 $c->stash(no_wrapper => 1);
266 $c->response->content_type('text/plain; charset=utf-8');
268 $c->forward('commitdiff');
273 Expose an abbreviated log of a given sha1.
277 sub shortlog : Local {
278 my ( $self, $c ) = @_;
279 my $project = $c->stash->{Project};
280 my $commit = $self->_get_object($c);
282 sha1 => $commit->sha1,
283 count => Gitalist->config->{paging}{log} || 25,
284 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
287 my $page = $c->req->param('pg') || 0;
288 $logargs{skip} = $c->req->param('pg') * $logargs{count}
289 if $c->req->param('pg');
293 log_lines => [$project->list_revs(%logargs)],
294 refs => $project->references,
295 action => 'shortlog',
302 Calls shortlog internally. Perhaps that should be reversed ...
306 $_[0]->shortlog($_[1]);
307 $_[1]->stash->{action} = 'log';
310 # For legacy support.
311 sub history : Local {
312 $_[0]->shortlog(@_[1 .. $#_]);
317 The tree of a given commit.
322 my ( $self, $c ) = @_;
323 my $project = $c->stash->{Project};
324 my $commit = $self->_get_object($c, $c->req->param('hb'));
325 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
329 tree_list => [$project->list_tree($tree->sha1)],
330 path => $c->req->param('f') || '',
337 Expose the local reflog. This may go away.
342 my ( $self, $c ) = @_;
343 my @log = $c->stash->{Project}->reflog(
355 The action for the search form.
361 $c->stash(current_action => 'GitRepos');
362 my $project = $c->stash->{Project};
363 my $commit = $self->_get_object($c);
364 # Lifted from /shortlog.
366 sha1 => $commit->sha1,
367 count => Gitalist->config->{paging}{log},
368 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
370 type => $c->req->param('type'),
371 text => $c->req->param('text'),
372 regexp => $c->req->param('regexp') || 0,
378 results => [$project->list_revs(%logargs)],
380 # This could be added - page => $page,
386 Provides some help for the search form.
390 sub search_help : Local {
392 $c->stash(template => 'search_help.tt2');
397 Provides an atom feed for a given project.
404 my $feed = XML::Atom::Feed->new;
406 my $host = lc Sys::Hostname::hostname();
407 $feed->title($host . ' - ' . Gitalist->config->{name});
408 $feed->updated(~~DateTime->now);
410 my $project = $c->stash->{Project};
412 sha1 => $project->head_hash,
413 count => Gitalist->config->{paging}{log} || 25,
414 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
417 my $mk_title = $c->stash->{short_cmt};
418 for my $commit ($project->list_revs(%logargs)) {
419 my $entry = XML::Atom::Entry->new;
420 $entry->title( $mk_title->($commit->comment) );
421 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
423 $entry->content($commit->comment);
424 $feed->add_entry($entry);
427 $c->response->body($feed->as_xml);
428 $c->response->content_type('application/atom+xml');
429 $c->response->status(200);
434 Provides an RSS feed for a given project.
441 my $project = $c->stash->{Project};
443 my $rss = XML::RSS->new(version => '2.0');
445 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
446 link => $c->uri_for('summary', {p=>$project->name}),
448 description => $project->description,
449 pubDate => DateTime->now,
450 lastBuildDate => DateTime->now,
454 sha1 => $project->head_hash,
455 count => Gitalist->config->{paging}{log} || 25,
456 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
458 my $mk_title = $c->stash->{short_cmt};
459 for my $commit ($project->list_revs(%logargs)) {
460 # XXX Needs work ....
462 title => $mk_title->($commit->comment),
463 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
464 description => $commit->comment,
468 $c->response->body($rss->as_string);
469 $c->response->content_type('application/rss+xml');
470 $c->response->status(200);
475 A raw patch for a given commit.
481 $c->detach('patches', [1]);
486 The patcheset for a given commit ???
490 sub patches : Local {
491 my ($self, $c, $count) = @_;
492 $count ||= Gitalist->config->{patches}{max};
493 my $commit = $self->_get_object($c);
494 my $parent = $c->req->param('hp') || undef;
495 my $patch = $commit->get_patch( $parent, $count );
496 $c->response->body($patch);
497 $c->response->content_type('text/plain');
498 $c->response->status(200);
503 Provides a snapshot of a given commit.
507 sub snapshot : Local {
509 my $format = $c->req->param('sf') || 'tgz';
511 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
512 my @snap = $c->stash->{Project}->snapshot(
516 $c->response->status(200);
517 $c->response->headers->header( 'Content-Disposition' =>
518 "attachment; filename=$snap[0]");
519 $c->response->body($snap[1]);
524 Populate the header and footer. Perhaps not the best location.
531 my $project = $c->req->param('p');
532 if (defined $project) {
534 $c->stash(Project => $c->model('GitRepos')->project($project));
537 $c->detach('error_404');
541 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
543 git_version => $a_project->run_cmd('--version'),
544 version => $Gitalist::VERSION,
546 # XXX Move these to a plugin!
548 return 'never' unless $_[0];
549 return age_string(time - $_[0]->epoch);
553 my($line) = split /\n/, $cmt;
554 $line =~ s/^(.{70,80}\b).*/$1 …/;
557 abridged_description => sub {
558 join(' ', grep { defined } (split / /, shift)[0..10]);
563 sub project_index : Local {
564 # FIXME - implement snapshot
565 Carp::croak "Not implemented.";
568 # FIXME - implement snapshot
569 Carp::croak "Not implemented.";
572 # FIXME - implement snapshot
573 Carp::croak "Not implemented.";
578 Attempt to render a view, if needed.
582 sub end : ActionClass('RenderView') {
584 # Give project views the current HEAD.
585 if ($c->stash->{Project}) {
586 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
590 sub error_404 :Private {
592 $c->response->status(404);
594 title => 'Page not found',
595 content => 'Page not found',
603 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
604 $age_str = ( int $age / 60 / 60 / 24 / 365 );
605 $age_str .= " years ago";
607 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
608 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
609 $age_str .= " months ago";
611 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
612 $age_str = int $age / 60 / 60 / 24 / 7;
613 $age_str .= " weeks ago";
615 elsif ( $age > 60 * 60 * 24 * 2 ) {
616 $age_str = int $age / 60 / 60 / 24;
617 $age_str .= " days ago";
619 elsif ( $age > 60 * 60 * 2 ) {
620 $age_str = int $age / 60 / 60;
621 $age_str .= " hours ago";
623 elsif ( $age > 60 * 2 ) {
624 $age_str = int $age / 60;
625 $age_str .= " min ago";
629 $age_str .= " sec ago";
632 $age_str .= " right now";
644 This library is free software. You can redistribute it and/or modify
645 it under the same terms as Perl itself.
649 __PACKAGE__->meta->make_immutable;