1 package Gitalist::Controller::Root;
3 use namespace::autoclean;
5 BEGIN { extends 'Catalyst::Controller' }
7 __PACKAGE__->config->{namespace} = '';
13 use XML::OPML::SimpleGen;
17 Gitalist::Controller::Root - Root Controller for Gitalist
21 [enter your description here]
28 my($self, $c, $haveh) = @_;
30 my $h = $haveh || $c->req->param('h') || '';
31 my $f = $c->req->param('f');
33 my $m = $c->stash->{Project};
36 # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
37 my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
38 || ($f && $m->hash_by_path($f))
40 # XXX This could definitely use more context.
41 || Carp::croak("Couldn't find a hash for the commit object!");
43 my $obj = $m->get_object($hash)
44 or Carp::croak("Couldn't find a object for '$hash' in '$pd'!");
51 Provides the project listing.
55 sub index :Path :Args(0) {
56 my ( $self, $c ) = @_;
58 $c->detach($c->req->param('a'))
59 if $c->req->param('a');
61 my @list = @{ $c->model()->projects };
62 die 'No projects found in '. $c->model->repo_dir
65 my $search = $c->req->param('s') || '';
68 index($_->name, $search) > -1
69 or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
74 search_text => $search,
80 sub project_index : Local {
81 my ( $self, $c ) = @_;
83 my @list = @{ $c->model()->projects };
84 die 'No projects found in '. $c->model->repo_dir
87 $c->response->content_type('text/plain');
89 join "\n", map $_->name, @list
91 $c->response->status(200);
96 A summary of what's happening in the repo.
100 sub summary : Local {
101 my ( $self, $c ) = @_;
102 my $project = $c->stash->{Project};
103 $c->detach('error_404') unless $project;
104 my $commit = $self->_get_object($c);
105 my @heads = @{$project->heads};
106 my $maxitems = Gitalist->config->{paging}{summary} || 10;
109 log_lines => [$project->list_revs(
110 sha1 => $commit->sha1,
113 refs => $project->references,
114 heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
121 The current list of heads (aka branches) in the repo.
126 my ( $self, $c ) = @_;
127 my $project = $c->stash->{Project};
129 commit => $self->_get_object($c),
130 heads => $project->heads,
137 The current list of tags in the repo.
142 my ( $self, $c ) = @_;
143 my $project = $c->stash->{Project};
145 commit => $self->_get_object($c),
146 tags => $project->tags,
154 my $project = $c->stash->{Project};
155 my $h = $c->req->param('h')
156 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
157 || die "No file or sha1 provided.";
158 my $hb = $c->req->param('hb')
159 || $project->head_hash
160 || die "Couldn't discern the corresponding head.";
161 my $filename = $c->req->param('f') || '';
164 blame => $project->get_object($hb)->blame($filename),
165 head => $project->get_object($hb),
166 filename => $filename,
172 my ( $self, $c ) = @_;
173 my $project = $c->stash->{Project};
174 my $h = $c->req->param('h')
175 || $project->hash_by_path($c->req->param('hb'), $c->req->param('f'))
176 || die "No file or sha1 provided.";
177 my $hb = $c->req->param('hb')
178 || $project->head_hash
179 || die "Couldn't discern the corresponding head.";
181 my $filename = $c->req->param('f') || '';
183 my $blob = $project->get_object($h);
184 $blob = $project->get_object(
185 $project->hash_by_path($h || $hb, $filename)
186 ) if $blob->type ne 'blob';
188 return $blob, $project->get_object($hb), $filename;
193 The blob action i.e the contents of a file.
198 my ( $self, $c ) = @_;
200 my($blob, $head, $filename) = $self->_blob_objs($c);
202 blob => $blob->content,
204 filename => $filename,
205 # XXX Hack hack hack, see View::SyntaxHighlight
206 language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
210 $c->forward('View::SyntaxHighlight')
211 unless $c->stash->{no_wrapper};
216 The plain text version of blob, where file is rendered as is.
220 sub blob_plain : Local {
223 my($blob) = $self->_blob_objs($c);
224 $c->response->content_type('text/plain; charset=utf-8');
225 $c->response->body($blob->content);
226 $c->response->status(200);
229 =head2 blobdiff_plain
231 The plain text version of blobdiff.
235 sub blobdiff_plain : Local {
238 $c->stash(no_wrapper => 1);
239 $c->response->content_type('text/plain; charset=utf-8');
241 $c->forward('blobdiff');
246 Exposes a given diff of a blob.
250 sub blobdiff : Local {
251 my ( $self, $c ) = @_;
252 my $commit = $self->_get_object($c, $c->req->param('hb'));
253 my $filename = $c->req->param('f')
254 || croak("No file specified!");
255 my($tree, $patch) = $c->stash->{Project}->diff(
258 parent => $c->req->param('hpb') || undef,
264 filename => $filename,
265 # XXX Hack hack hack, see View::SyntaxHighlight
266 blobs => [$patch->[0]->{diff}],
268 action => 'blobdiff',
271 $c->forward('View::SyntaxHighlight')
272 unless $c->stash->{no_wrapper};
277 Exposes a given commit.
282 my ( $self, $c ) = @_;
283 my $project = $c->stash->{Project};
284 my $commit = $self->_get_object($c);
287 diff_tree => ($project->diff(commit => $commit))[0],
288 refs => $project->references,
295 Exposes a given diff of a commit.
299 sub commitdiff : Local {
300 my ( $self, $c ) = @_;
301 my $commit = $self->_get_object($c);
302 my($tree, $patch) = $c->stash->{Project}->diff(
304 parent => $c->req->param('hp') || undef,
311 # XXX Hack hack hack, see View::SyntaxHighlight
312 blobs => [map $_->{diff}, @$patch],
314 action => 'commitdiff',
317 $c->forward('View::SyntaxHighlight')
318 unless $c->stash->{no_wrapper};
321 sub commitdiff_plain : Local {
324 $c->stash(no_wrapper => 1);
325 $c->response->content_type('text/plain; charset=utf-8');
327 $c->forward('commitdiff');
332 Expose an abbreviated log of a given sha1.
336 sub shortlog : Local {
337 my ( $self, $c ) = @_;
339 my $project = $c->stash->{Project};
340 my $commit = $self->_get_object($c, $c->req->param('hb'));
341 my $filename = $c->req->param('f') || '';
344 sha1 => $commit->sha1,
345 count => Gitalist->config->{paging}{log} || 25,
346 ($filename ? (file => $filename) : ())
349 my $page = $c->req->param('pg') || 0;
350 $logargs{skip} = $c->req->param('pg') * $logargs{count}
351 if $c->req->param('pg');
355 log_lines => [$project->list_revs(%logargs)],
356 refs => $project->references,
358 filename => $filename,
359 action => 'shortlog',
365 Calls shortlog internally. Perhaps that should be reversed ...
369 $_[0]->shortlog($_[1]);
370 $_[1]->stash->{action} = 'log';
373 # For legacy support.
374 sub history : Local {
375 $_[1]->forward('shortlog');
380 The tree of a given commit.
385 my ( $self, $c ) = @_;
386 my $project = $c->stash->{Project};
387 my $commit = $self->_get_object($c, $c->req->param('hb'));
388 my $filename = $c->req->param('f') || '';
390 ? $project->get_object($project->hash_by_path($commit->sha1, $filename))
391 : $project->get_object($commit->tree_sha1)
396 tree_list => [$project->list_tree($tree->sha1)],
397 path => $c->req->param('f') || '',
404 Expose the local reflog. This may go away.
409 my ( $self, $c ) = @_;
410 my @log = $c->stash->{Project}->reflog(
422 The action for the search form.
428 $c->stash(current_action => 'GitRepos');
429 my $project = $c->stash->{Project};
430 my $commit = $self->_get_object($c);
431 # Lifted from /shortlog.
433 sha1 => $commit->sha1,
434 count => Gitalist->config->{paging}{log},
435 ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
437 type => $c->req->param('type'),
438 text => $c->req->param('text'),
439 regexp => $c->req->param('regexp') || 0,
445 results => [$project->list_revs(%logargs)],
447 # This could be added - page => $page,
453 Provides some help for the search form.
457 sub search_help : Local {
459 $c->stash(template => 'search_help.tt2');
464 Provides an atom feed for a given project.
471 my $feed = XML::Atom::Feed->new;
473 my $host = lc Sys::Hostname::hostname();
474 $feed->title($host . ' - ' . Gitalist->config->{name});
475 $feed->updated(~~DateTime->now);
477 my $project = $c->stash->{Project};
479 sha1 => $project->head_hash,
480 count => Gitalist->config->{paging}{log} || 25,
481 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
484 my $mk_title = $c->stash->{short_cmt};
485 for my $commit ($project->list_revs(%logargs)) {
486 my $entry = XML::Atom::Entry->new;
487 $entry->title( $mk_title->($commit->comment) );
488 $entry->id($c->uri_for('commit', {h=>$commit->sha1}));
490 $entry->content($commit->comment);
491 $feed->add_entry($entry);
494 $c->response->body($feed->as_xml);
495 $c->response->content_type('application/atom+xml');
496 $c->response->status(200);
501 Provides an RSS feed for a given project.
508 my $project = $c->stash->{Project};
510 my $rss = XML::RSS->new(version => '2.0');
512 title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
513 link => $c->uri_for('summary', {p=>$project->name}),
515 description => $project->description,
516 pubDate => DateTime->now,
517 lastBuildDate => DateTime->now,
521 sha1 => $project->head_hash,
522 count => Gitalist->config->{paging}{log} || 25,
523 ($c->req->param('f') ? (file => $c->req->param('f')) : ())
525 my $mk_title = $c->stash->{short_cmt};
526 for my $commit ($project->list_revs(%logargs)) {
527 # XXX Needs work ....
529 title => $mk_title->($commit->comment),
530 permaLink => $c->uri_for(commit => {h=>$commit->sha1}),
531 description => $commit->comment,
535 $c->response->body($rss->as_string);
536 $c->response->content_type('application/rss+xml');
537 $c->response->status(200);
543 my $opml = XML::OPML::SimpleGen->new();
545 $opml->head(title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name});
547 my @list = @{ $c->model()->projects };
548 die 'No projects found in '. $c->model->repo_dir
551 for my $proj ( @list ) {
552 $opml->insert_outline(
553 text => $proj->name. ' - '. $proj->description,
554 xmlUrl => $c->uri_for(rss => {p => $proj->name}),
558 $c->response->body($opml->as_string);
559 $c->response->content_type('application/rss');
560 $c->response->status(200);
565 A raw patch for a given commit.
571 $c->detach('patches', [1]);
576 The patcheset for a given commit ???
580 sub patches : Local {
581 my ($self, $c, $count) = @_;
582 $count ||= Gitalist->config->{patches}{max};
583 my $commit = $self->_get_object($c);
584 my $parent = $c->req->param('hp') || undef;
585 my $patch = $commit->get_patch( $parent, $count );
586 $c->response->body($patch);
587 $c->response->content_type('text/plain');
588 $c->response->status(200);
593 Provides a snapshot of a given commit.
597 sub snapshot : Local {
599 my $format = $c->req->param('sf') || 'tgz';
601 my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
602 my @snap = $c->stash->{Project}->snapshot(
606 $c->response->status(200);
607 $c->response->headers->header( 'Content-Disposition' =>
608 "attachment; filename=$snap[0]");
609 $c->response->body($snap[1]);
614 Populate the header and footer. Perhaps not the best location.
621 my $project = $c->req->param('p');
622 if (defined $project) {
624 $c->stash(Project => $c->model('GitRepos')->project($project));
627 $c->detach('error_404');
631 my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
633 git_version => $a_project->run_cmd('--version'),
634 version => $Gitalist::VERSION,
636 # XXX Move these to a plugin!
638 return 'never' unless $_[0];
639 return age_string(time - $_[0]->epoch);
643 my($line) = split /\n/, $cmt;
644 $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
647 abridged_description => sub {
648 join(' ', grep { defined } (split / /, shift)[0..10]);
655 Attempt to render a view, if needed.
659 sub end : ActionClass('RenderView') {
661 # Give project views the current HEAD.
662 if ($c->stash->{Project}) {
663 $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
667 sub error_404 :Private {
669 $c->response->status(404);
671 title => 'Page not found',
672 content => 'Page not found',
680 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
681 $age_str = ( int $age / 60 / 60 / 24 / 365 );
682 $age_str .= " years ago";
684 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
685 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
686 $age_str .= " months ago";
688 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
689 $age_str = int $age / 60 / 60 / 24 / 7;
690 $age_str .= " weeks ago";
692 elsif ( $age > 60 * 60 * 24 * 2 ) {
693 $age_str = int $age / 60 / 60 / 24;
694 $age_str .= " days ago";
696 elsif ( $age > 60 * 60 * 2 ) {
697 $age_str = int $age / 60 / 60;
698 $age_str .= " hours ago";
700 elsif ( $age > 60 * 2 ) {
701 $age_str = int $age / 60;
702 $age_str .= " min ago";
706 $age_str .= " sec ago";
709 $age_str .= " right now";
721 This library is free software. You can redistribute it and/or modify
722 it under the same terms as Perl itself.
726 __PACKAGE__->meta->make_immutable;