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,
139 my $project = $c->stash->{Project};
140 my $h = $c->req->param('h')
141 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
142 || die "No file or sha1 provided.";
143 my $hb = $c->req->param('hb')
144 || $project->head_hash
145 || die "Couldn't discern the corresponding head.";
146 my $filename = $c->req->param('f') || '';
148 my($metadata, $filedata) = $project->get_object($hb)->blame($filename);
150 metadata => $metadata,
151 filedata => $filedata,
152 head => $project->get_object($hb),
153 filename => $filename,
160 The blob action i.e the contents of a file.
165 my ( $self, $c ) = @_;
166 my $project = $c->stash->{Project};
167 my $h = $c->req->param('h')
168 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
169 || die "No file or sha1 provided.";
170 my $hb = $c->req->param('hb')
171 || $project->head_hash
172 || die "Couldn't discern the corresponding head.";
174 my $filename = $c->req->param('f') || '';
177 blob => $project->get_object($h)->content,
178 head => $project->get_object($hb),
179 filename => $filename,
180 # XXX Hack hack hack, see View::SyntaxHighlight
181 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
185 $c->forward('View::SyntaxHighlight')
186 unless $c->stash->{no_wrapper};
189 sub blob_plain : Local {
192 $c->stash(no_wrapper => 1);
193 $c->response->content_type('text/plain; charset=utf-8');
198 sub blobdiff_plain : Local {
201 $c->stash(no_wrapper => 1);
202 $c->response->content_type('text/plain; charset=utf-8');
204 $c->forward('blobdiff');
210 Exposes a given diff of a blob.
214 sub blobdiff : Local {
215 my ( $self, $c ) = @_;
216 my $commit = $self->_get_object($c, $c->req->param('hb'));
217 my $filename = $c->req->param('f')
218 || croak("No file specified!");
219 my($tree, $patch) = $c->stash->{Project}->diff(
222 parent => $c->req->param('hpb') || undef,
228 # XXX Hack hack hack, see View::SyntaxHighlight
229 blobs => [$patch->[0]->{diff}],
231 action => 'blobdiff',
234 $c->forward('View::SyntaxHighlight')
235 unless $c->stash->{no_wrapper};
240 Exposes a given commit.
245 my ( $self, $c ) = @_;
246 my $project = $c->stash->{Project};
247 my $commit = $self->_get_object($c);
250 diff_tree => ($project->diff(commit => $commit))[0],
251 refs => $project->references,
258 Exposes a given diff of a commit.
262 sub commitdiff : Local {
263 my ( $self, $c ) = @_;
264 my $commit = $self->_get_object($c);
265 my($tree, $patch) = $c->stash->{Project}->diff(
267 parent => $c->req->param('hp') || undef,
274 # XXX Hack hack hack, see View::SyntaxHighlight
275 blobs => [map $_->{diff}, @$patch],
277 action => 'commitdiff',
280 $c->forward('View::SyntaxHighlight')
281 unless $c->stash->{no_wrapper};
284 sub commitdiff_plain : Local {
287 $c->stash(no_wrapper => 1);
288 $c->response->content_type('text/plain; charset=utf-8');
290 $c->forward('commitdiff');
295 Expose an abbreviated log of a given sha1.
299 sub shortlog : Local {
300 my ( $self, $c ) = @_;
301 my $project = $c->stash->{Project};
302 my $commit = $self->_get_object($c);
304 sha1 => $commit->sha1,
305 count => Gitalist->config->{paging}{log} || 25,
306 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
309 my $page = $c->req->param('pg') || 0;
310 $logargs{skip} = $c->req->param('pg') * $logargs{count}
311 if $c->req->param('pg');
315 log_lines => [$project->list_revs(%logargs)],
316 refs => $project->references,
317 action => 'shortlog',
324 Calls shortlog internally. Perhaps that should be reversed ...
328 $_[0]->shortlog($_[1]);
329 $_[1]->stash->{action} = 'log';
332 # For legacy support.
333 sub history : Local {
334 $_[0]->shortlog(@_[1 .. $#_]);
339 The tree of a given commit.
344 my ( $self, $c ) = @_;
345 my $project = $c->stash->{Project};
346 my $commit = $self->_get_object($c, $c->req->param('hb'));
347 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
351 tree_list => [$project->list_tree($tree->sha1)],
352 path => $c->req->param('f') || '',
359 Expose the local reflog. This may go away.
364 my ( $self, $c ) = @_;
365 my @log = $c->stash->{Project}->reflog(
377 The action for the search form.
383 $c->stash(current_action => 'GitRepos');
384 my $project = $c->stash->{Project};
385 my $commit = $self->_get_object($c);
386 # Lifted from /shortlog.
388 sha1 => $commit->sha1,
389 count => Gitalist->config->{paging}{log},
390 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
392 type => $c->req->param('type'),
393 text => $c->req->param('text'),
394 regexp => $c->req->param('regexp') || 0,
400 results => [$project->list_revs(%logargs)],
402 # This could be added - page => $page,
408 Provides some help for the search form.
412 sub search_help : Local {
414 $c->stash(template => 'search_help.tt2');
419 Provides an atom feed for a given project.
426 my $feed = XML::Atom::Feed->new;
428 my $host = lc Sys::Hostname::hostname();
429 $feed->title($host . ' - ' . Gitalist->config->{name});
430 $feed->updated(~~DateTime->now);
432 my $project = $c->stash->{Project};
434 sha1 => $project->head_hash,
435 count => Gitalist->config->{paging}{log} || 25,
436 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
439 my $mk_title = $c->stash->{short_cmt};
440 for my $commit ($project->list_revs(%logargs)) {
441 my $entry = XML::Atom::Entry->new;
442 $entry->title( $mk_title->($commit->comment) );
443 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
445 $entry->content($commit->comment);
446 $feed->add_entry($entry);
449 $c->response->body($feed->as_xml);
450 $c->response->content_type('application/atom+xml');
451 $c->response->status(200);
456 Provides an RSS feed for a given project.
463 my $project = $c->stash->{Project};
465 my $rss = XML::RSS->new(version => '2.0');
467 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
468 link => $c->uri_for('summary', {p=>$project->name}),
470 description => $project->description,
471 pubDate => DateTime->now,
472 lastBuildDate => DateTime->now,
476 sha1 => $project->head_hash,
477 count => Gitalist->config->{paging}{log} || 25,
478 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
480 my $mk_title = $c->stash->{short_cmt};
481 for my $commit ($project->list_revs(%logargs)) {
482 # XXX Needs work ....
484 title => $mk_title->($commit->comment),
485 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
486 description => $commit->comment,
490 $c->response->body($rss->as_string);
491 $c->response->content_type('application/rss+xml');
492 $c->response->status(200);
497 A raw patch for a given commit.
503 $c->detach('patches', [1]);
508 The patcheset for a given commit ???
512 sub patches : Local {
513 my ($self, $c, $count) = @_;
514 $count ||= Gitalist->config->{patches}{max};
515 my $commit = $self->_get_object($c);
516 my $parent = $c->req->param('hp') || undef;
517 my $patch = $commit->get_patch( $parent, $count );
518 $c->response->body($patch);
519 $c->response->content_type('text/plain');
520 $c->response->status(200);
525 Provides a snapshot of a given commit.
529 sub snapshot : Local {
531 my $format = $c->req->param('sf') || 'tgz';
533 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
534 my @snap = $c->stash->{Project}->snapshot(
538 $c->response->status(200);
539 $c->response->headers->header( 'Content-Disposition' =>
540 "attachment; filename=$snap[0]");
541 $c->response->body($snap[1]);
546 Populate the header and footer. Perhaps not the best location.
553 my $project = $c->req->param('p');
554 if (defined $project) {
556 $c->stash(Project => $c->model('GitRepos')->project($project));
559 $c->detach('error_404');
563 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
565 git_version => $a_project->run_cmd('--version'),
566 version => $Gitalist::VERSION,
568 # XXX Move these to a plugin!
570 return 'never' unless $_[0];
571 return age_string(time - $_[0]->epoch);
575 my($line) = split /\n/, $cmt;
576 $line =~ s/^(.{70,80}\b).*/$1 …/;
579 abridged_description => sub {
580 join(' ', grep { defined } (split / /, shift)[0..10]);
585 sub project_index : Local {
586 # FIXME - implement snapshot
587 Carp::croak "Not implemented.";
590 # FIXME - implement snapshot
591 Carp::croak "Not implemented.";
596 Attempt to render a view, if needed.
600 sub end : ActionClass('RenderView') {
602 # Give project views the current HEAD.
603 if ($c->stash->{Project}) {
604 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
608 sub error_404 :Private {
610 $c->response->status(404);
612 title => 'Page not found',
613 content => 'Page not found',
621 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
622 $age_str = ( int $age / 60 / 60 / 24 / 365 );
623 $age_str .= " years ago";
625 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
626 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
627 $age_str .= " months ago";
629 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
630 $age_str = int $age / 60 / 60 / 24 / 7;
631 $age_str .= " weeks ago";
633 elsif ( $age > 60 * 60 * 24 * 2 ) {
634 $age_str = int $age / 60 / 60 / 24;
635 $age_str .= " days ago";
637 elsif ( $age > 60 * 60 * 2 ) {
638 $age_str = int $age / 60 / 60;
639 $age_str .= " hours ago";
641 elsif ( $age > 60 * 2 ) {
642 $age_str = int $age / 60;
643 $age_str .= " min ago";
647 $age_str .= " sec ago";
650 $age_str .= " right now";
662 This library is free software. You can redistribute it and/or modify
663 it under the same terms as Perl itself.
667 __PACKAGE__->meta->make_immutable;