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 blob action i.e the contents of a file.
127 my ( $self, $c ) = @_;
128 my $project = $c->stash->{Project};
129 my $h = $c->req->param('h')
130 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
131 || die "No file or sha1 provided.";
132 my $hb = $c->req->param('hb')
133 || $project->head_hash
134 || die "Couldn't discern the corresponding head.";
136 my $filename = $c->req->param('f') || '';
139 blob => $project->get_object($h)->content,
140 head => $project->get_object($hb),
141 filename => $filename,
142 # XXX Hack hack hack, see View::SyntaxHighlight
143 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
147 $c->forward('View::SyntaxHighlight')
148 unless $c->stash->{no_wrapper};
151 sub blob_plain : Local {
154 $c->stash(no_wrapper => 1);
155 $c->response->content_type('text/plain; charset=utf-8');
160 sub blobdiff_plain : Local {
163 $c->stash(no_wrapper => 1);
164 $c->response->content_type('text/plain; charset=utf-8');
166 $c->forward('blobdiff');
172 Exposes a given diff of a blob.
176 sub blobdiff : Local {
177 my ( $self, $c ) = @_;
178 my $commit = $self->_get_object($c, $c->req->param('hb'));
179 my $filename = $c->req->param('f')
180 || croak("No file specified!");
181 my($tree, $patch) = $c->stash->{Project}->diff(
184 parent => $c->req->param('hpb') || undef,
190 # XXX Hack hack hack, see View::SyntaxHighlight
191 blobs => [$patch->[0]->{diff}],
193 action => 'blobdiff',
196 $c->forward('View::SyntaxHighlight')
197 unless $c->stash->{no_wrapper};
202 Exposes a given commit.
207 my ( $self, $c ) = @_;
208 my $project = $c->stash->{Project};
209 my $commit = $self->_get_object($c);
212 diff_tree => ($project->diff(commit => $commit))[0],
213 refs => $project->references,
220 Exposes a given diff of a commit.
224 sub commitdiff : Local {
225 my ( $self, $c ) = @_;
226 my $commit = $self->_get_object($c);
227 my($tree, $patch) = $c->stash->{Project}->diff(
229 parent => $c->req->param('hp') || undef,
236 # XXX Hack hack hack, see View::SyntaxHighlight
237 blobs => [map $_->{diff}, @$patch],
239 action => 'commitdiff',
242 $c->forward('View::SyntaxHighlight')
243 unless $c->stash->{no_wrapper};
246 sub commitdiff_plain : Local {
249 $c->stash(no_wrapper => 1);
250 $c->response->content_type('text/plain; charset=utf-8');
252 $c->forward('commitdiff');
257 Expose an abbreviated log of a given sha1.
261 sub shortlog : Local {
262 my ( $self, $c ) = @_;
263 my $project = $c->stash->{Project};
264 my $commit = $self->_get_object($c);
266 sha1 => $commit->sha1,
267 count => Gitalist->config->{paging}{log} || 25,
268 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
271 my $page = $c->req->param('pg') || 0;
272 $logargs{skip} = $c->req->param('pg') * $logargs{count}
273 if $c->req->param('pg');
277 log_lines => [$project->list_revs(%logargs)],
278 refs => $project->references,
279 action => 'shortlog',
286 Calls shortlog internally. Perhaps that should be reversed ...
290 $_[0]->shortlog($_[1]);
291 $_[1]->stash->{action} = 'log';
294 # For legacy support.
295 sub history : Local {
296 $_[0]->shortlog(@_[1 .. $#_]);
301 The tree of a given commit.
306 my ( $self, $c ) = @_;
307 my $project = $c->stash->{Project};
308 my $commit = $self->_get_object($c, $c->req->param('hb'));
309 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
313 tree_list => [$project->list_tree($tree->sha1)],
314 path => $c->req->param('f') || '',
321 Expose the local reflog. This may go away.
326 my ( $self, $c ) = @_;
327 my @log = $c->stash->{Project}->reflog(
339 $c->stash(current_action => 'GitRepos');
340 my $project = $c->stash->{Project};
341 my $commit = $self->_get_object($c);
342 # Lifted from /shortlog.
344 sha1 => $commit->sha1,
345 count => Gitalist->config->{paging}{log},
346 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
348 type => $c->req->param('type'),
349 text => $c->req->param('text'),
350 regexp => $c->req->param('regexp') || 0,
356 results => [$project->list_revs(%logargs)],
358 # This could be added - page => $page,
362 sub search_help : Local {
364 $c->stash(template => 'search_help.tt2');
370 my $feed = XML::Atom::Feed->new;
372 my $host = lc Sys::Hostname::hostname();
373 $feed->title($host . ' - ' . Gitalist->config->{name});
374 $feed->updated(~~DateTime->now);
376 my $project = $c->stash->{Project};
378 sha1 => $project->head_hash,
379 count => Gitalist->config->{paging}{log} || 25,
380 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
383 my $mk_title = $c->stash->{short_cmt};
384 for my $commit ($project->list_revs(%logargs)) {
385 my $entry = XML::Atom::Entry->new;
386 $entry->title( $mk_title->($commit->comment) );
387 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
389 $entry->content($commit->comment);
390 $feed->add_entry($entry);
393 $c->response->body($feed->as_xml);
394 $c->response->content_type('application/atom+xml');
395 $c->response->status(200);
401 my $project = $c->stash->{Project};
403 my $rss = XML::RSS->new(version => '2.0');
405 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
406 link => $c->uri_for('summary', {p=>$project->name}),
408 description => $project->description,
409 pubDate => DateTime->now,
410 lastBuildDate => DateTime->now,
414 sha1 => $project->head_hash,
415 count => Gitalist->config->{paging}{log} || 25,
416 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
418 my $mk_title = $c->stash->{short_cmt};
419 for my $commit ($project->list_revs(%logargs)) {
420 # XXX Needs work ....
422 title => $mk_title->($commit->comment),
423 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
424 description => $commit->comment,
428 $c->response->body($rss->as_string);
429 $c->response->content_type('application/rss+xml');
430 $c->response->status(200);
435 $c->detach('patches', [1]);
438 sub patches : Local {
439 my ($self, $c, $count) = @_;
440 $count ||= Gitalist->config->{patches}{max};
441 my $commit = $self->_get_object($c);
442 my $parent = $c->req->param('hp') || undef;
443 my $patch = $commit->get_patch( $parent, $count );
444 $c->response->body($patch);
445 $c->response->content_type('text/plain');
446 $c->response->status(200);
449 sub snapshot : Local {
451 my $format = $c->req->param('sf') || 'tgz';
453 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
454 my @snap = $c->stash->{Project}->snapshot(
458 $c->response->status(200);
459 $c->response->headers->header( 'Content-Disposition' =>
460 "attachment; filename=$snap[0]");
461 $c->response->body($snap[1]);
466 Populate the header and footer. Perhaps not the best location.
473 my $project = $c->req->param('p');
474 if (defined $project) {
476 $c->stash(Project => $c->model('GitRepos')->project($project));
479 $c->detach('error_404');
483 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
485 git_version => $a_project->run_cmd('--version'),
486 version => $Gitalist::VERSION,
488 # XXX Move these to a plugin!
490 return 'never' unless $_[0];
491 return age_string(time - $_[0]->epoch);
495 my($line) = split /\n/, $cmt;
496 $line =~ s/^(.{70,80}\b).*/$1 …/;
499 abridged_description => sub {
500 join(' ', grep { defined } (split / /, shift)[0..10]);
506 # FIXME - implement snapshot
507 Carp::croak "Not implemented.";
509 sub project_index : Local {
510 # FIXME - implement snapshot
511 Carp::croak "Not implemented.";
514 # FIXME - implement snapshot
515 Carp::croak "Not implemented.";
518 # FIXME - implement snapshot
519 Carp::croak "Not implemented.";
524 Attempt to render a view, if needed.
528 sub end : ActionClass('RenderView') {
530 # Give project views the current HEAD.
531 if ($c->stash->{Project}) {
532 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
536 sub error_404 :Private {
538 $c->response->status(404);
540 title => 'Page not found',
541 content => 'Page not found',
549 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
550 $age_str = ( int $age / 60 / 60 / 24 / 365 );
551 $age_str .= " years ago";
553 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
554 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
555 $age_str .= " months ago";
557 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
558 $age_str = int $age / 60 / 60 / 24 / 7;
559 $age_str .= " weeks ago";
561 elsif ( $age > 60 * 60 * 24 * 2 ) {
562 $age_str = int $age / 60 / 60 / 24;
563 $age_str .= " days ago";
565 elsif ( $age > 60 * 60 * 2 ) {
566 $age_str = int $age / 60 / 60;
567 $age_str .= " hours ago";
569 elsif ( $age > 60 * 2 ) {
570 $age_str = int $age / 60;
571 $age_str .= " min ago";
575 $age_str .= " sec ago";
578 $age_str .= " right now";
590 This library is free software. You can redistribute it and/or modify
591 it under the same terms as Perl itself.
595 __PACKAGE__->meta->make_immutable;