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') || '';
149 blame => $project->get_object($hb)->blame($filename),
150 head => $project->get_object($hb),
151 filename => $filename,
158 The blob action i.e the contents of a file.
163 my ( $self, $c ) = @_;
164 my $project = $c->stash->{Project};
165 my $h = $c->req->param('h')
166 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
167 || die "No file or sha1 provided.";
168 my $hb = $c->req->param('hb')
169 || $project->head_hash
170 || die "Couldn't discern the corresponding head.";
172 my $filename = $c->req->param('f') || '';
175 blob => $project->get_object($h)->content,
176 head => $project->get_object($hb),
177 filename => $filename,
178 # XXX Hack hack hack, see View::SyntaxHighlight
179 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
183 $c->forward('View::SyntaxHighlight')
184 unless $c->stash->{no_wrapper};
187 sub blob_plain : Local {
190 $c->stash(no_wrapper => 1);
191 $c->response->content_type('text/plain; charset=utf-8');
196 sub blobdiff_plain : Local {
199 $c->stash(no_wrapper => 1);
200 $c->response->content_type('text/plain; charset=utf-8');
202 $c->forward('blobdiff');
208 Exposes a given diff of a blob.
212 sub blobdiff : Local {
213 my ( $self, $c ) = @_;
214 my $commit = $self->_get_object($c, $c->req->param('hb'));
215 my $filename = $c->req->param('f')
216 || croak("No file specified!");
217 my($tree, $patch) = $c->stash->{Project}->diff(
220 parent => $c->req->param('hpb') || undef,
226 # XXX Hack hack hack, see View::SyntaxHighlight
227 blobs => [$patch->[0]->{diff}],
229 action => 'blobdiff',
232 $c->forward('View::SyntaxHighlight')
233 unless $c->stash->{no_wrapper};
238 Exposes a given commit.
243 my ( $self, $c ) = @_;
244 my $project = $c->stash->{Project};
245 my $commit = $self->_get_object($c);
248 diff_tree => ($project->diff(commit => $commit))[0],
249 refs => $project->references,
256 Exposes a given diff of a commit.
260 sub commitdiff : Local {
261 my ( $self, $c ) = @_;
262 my $commit = $self->_get_object($c);
263 my($tree, $patch) = $c->stash->{Project}->diff(
265 parent => $c->req->param('hp') || undef,
272 # XXX Hack hack hack, see View::SyntaxHighlight
273 blobs => [map $_->{diff}, @$patch],
275 action => 'commitdiff',
278 $c->forward('View::SyntaxHighlight')
279 unless $c->stash->{no_wrapper};
282 sub commitdiff_plain : Local {
285 $c->stash(no_wrapper => 1);
286 $c->response->content_type('text/plain; charset=utf-8');
288 $c->forward('commitdiff');
293 Expose an abbreviated log of a given sha1.
297 sub shortlog : Local {
298 my ( $self, $c ) = @_;
299 my $project = $c->stash->{Project};
300 my $commit = $self->_get_object($c);
302 sha1 => $commit->sha1,
303 count => Gitalist->config->{paging}{log} || 25,
304 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
307 my $page = $c->req->param('pg') || 0;
308 $logargs{skip} = $c->req->param('pg') * $logargs{count}
309 if $c->req->param('pg');
313 log_lines => [$project->list_revs(%logargs)],
314 refs => $project->references,
315 action => 'shortlog',
322 Calls shortlog internally. Perhaps that should be reversed ...
326 $_[0]->shortlog($_[1]);
327 $_[1]->stash->{action} = 'log';
330 # For legacy support.
331 sub history : Local {
332 $_[0]->shortlog(@_[1 .. $#_]);
337 The tree of a given commit.
342 my ( $self, $c ) = @_;
343 my $project = $c->stash->{Project};
344 my $commit = $self->_get_object($c, $c->req->param('hb'));
345 my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1);
349 tree_list => [$project->list_tree($tree->sha1)],
350 path => $c->req->param('f') || '',
357 Expose the local reflog. This may go away.
362 my ( $self, $c ) = @_;
363 my @log = $c->stash->{Project}->reflog(
375 The action for the search form.
381 $c->stash(current_action => 'GitRepos');
382 my $project = $c->stash->{Project};
383 my $commit = $self->_get_object($c);
384 # Lifted from /shortlog.
386 sha1 => $commit->sha1,
387 count => Gitalist->config->{paging}{log},
388 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
390 type => $c->req->param('type'),
391 text => $c->req->param('text'),
392 regexp => $c->req->param('regexp') || 0,
398 results => [$project->list_revs(%logargs)],
400 # This could be added - page => $page,
406 Provides some help for the search form.
410 sub search_help : Local {
412 $c->stash(template => 'search_help.tt2');
417 Provides an atom feed for a given project.
424 my $feed = XML::Atom::Feed->new;
426 my $host = lc Sys::Hostname::hostname();
427 $feed->title($host . ' - ' . Gitalist->config->{name});
428 $feed->updated(~~DateTime->now);
430 my $project = $c->stash->{Project};
432 sha1 => $project->head_hash,
433 count => Gitalist->config->{paging}{log} || 25,
434 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
437 my $mk_title = $c->stash->{short_cmt};
438 for my $commit ($project->list_revs(%logargs)) {
439 my $entry = XML::Atom::Entry->new;
440 $entry->title( $mk_title->($commit->comment) );
441 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
443 $entry->content($commit->comment);
444 $feed->add_entry($entry);
447 $c->response->body($feed->as_xml);
448 $c->response->content_type('application/atom+xml');
449 $c->response->status(200);
454 Provides an RSS feed for a given project.
461 my $project = $c->stash->{Project};
463 my $rss = XML::RSS->new(version => '2.0');
465 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
466 link => $c->uri_for('summary', {p=>$project->name}),
468 description => $project->description,
469 pubDate => DateTime->now,
470 lastBuildDate => DateTime->now,
474 sha1 => $project->head_hash,
475 count => Gitalist->config->{paging}{log} || 25,
476 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
478 my $mk_title = $c->stash->{short_cmt};
479 for my $commit ($project->list_revs(%logargs)) {
480 # XXX Needs work ....
482 title => $mk_title->($commit->comment),
483 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
484 description => $commit->comment,
488 $c->response->body($rss->as_string);
489 $c->response->content_type('application/rss+xml');
490 $c->response->status(200);
495 A raw patch for a given commit.
501 $c->detach('patches', [1]);
506 The patcheset for a given commit ???
510 sub patches : Local {
511 my ($self, $c, $count) = @_;
512 $count ||= Gitalist->config->{patches}{max};
513 my $commit = $self->_get_object($c);
514 my $parent = $c->req->param('hp') || undef;
515 my $patch = $commit->get_patch( $parent, $count );
516 $c->response->body($patch);
517 $c->response->content_type('text/plain');
518 $c->response->status(200);
523 Provides a snapshot of a given commit.
527 sub snapshot : Local {
529 my $format = $c->req->param('sf') || 'tgz';
531 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
532 my @snap = $c->stash->{Project}->snapshot(
536 $c->response->status(200);
537 $c->response->headers->header( 'Content-Disposition' =>
538 "attachment; filename=$snap[0]");
539 $c->response->body($snap[1]);
544 Populate the header and footer. Perhaps not the best location.
551 my $project = $c->req->param('p');
552 if (defined $project) {
554 $c->stash(Project => $c->model('GitRepos')->project($project));
557 $c->detach('error_404');
561 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
563 git_version => $a_project->run_cmd('--version'),
564 version => $Gitalist::VERSION,
566 # XXX Move these to a plugin!
568 return 'never' unless $_[0];
569 return age_string(time - $_[0]->epoch);
573 my($line) = split /\n/, $cmt;
574 $line =~ s/^(.{70,80}\b).*/$1 …/;
577 abridged_description => sub {
578 join(' ', grep { defined } (split / /, shift)[0..10]);
583 sub project_index : Local {
584 # FIXME - implement snapshot
585 Carp::croak "Not implemented.";
588 # FIXME - implement snapshot
589 Carp::croak "Not implemented.";
594 Attempt to render a view, if needed.
598 sub end : ActionClass('RenderView') {
600 # Give project views the current HEAD.
601 if ($c->stash->{Project}) {
602 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
606 sub error_404 :Private {
608 $c->response->status(404);
610 title => 'Page not found',
611 content => 'Page not found',
619 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
620 $age_str = ( int $age / 60 / 60 / 24 / 365 );
621 $age_str .= " years ago";
623 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
624 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
625 $age_str .= " months ago";
627 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
628 $age_str = int $age / 60 / 60 / 24 / 7;
629 $age_str .= " weeks ago";
631 elsif ( $age > 60 * 60 * 24 * 2 ) {
632 $age_str = int $age / 60 / 60 / 24;
633 $age_str .= " days ago";
635 elsif ( $age > 60 * 60 * 2 ) {
636 $age_str = int $age / 60 / 60;
637 $age_str .= " hours ago";
639 elsif ( $age > 60 * 2 ) {
640 $age_str = int $age / 60;
641 $age_str .= " min ago";
645 $age_str .= " sec ago";
648 $age_str .= " right now";
660 This library is free software. You can redistribute it and/or modify
661 it under the same terms as Perl itself.
665 __PACKAGE__->meta->make_immutable;