Added /opml action.
[catagits/Gitalist.git] / lib / Gitalist / Controller / Root.pm
1 package Gitalist::Controller::Root;
2 use Moose;
3 use namespace::autoclean;
4
5 BEGIN { extends 'Catalyst::Controller' }
6
7 __PACKAGE__->config->{namespace} = '';
8
9 use Sys::Hostname ();
10 use XML::Atom::Feed;
11 use XML::Atom::Entry;
12 use XML::RSS;
13 use XML::OPML::SimpleGen;
14
15 =head1 NAME
16
17 Gitalist::Controller::Root - Root Controller for Gitalist
18
19 =head1 DESCRIPTION
20
21 [enter your description here]
22
23 =head1 METHODS
24
25 =cut
26
27 sub _get_object {
28   my($self, $c, $haveh) = @_;
29
30   my $h = $haveh || $c->req->param('h') || '';
31   my $f = $c->req->param('f');
32
33   my $m = $c->stash->{Project};
34   my $pd = $m->path;
35
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))
39           || $m->head_hash
40           # XXX This could definitely use more context.
41           || Carp::croak("Couldn't find a hash for the commit object!");
42
43   my $obj = $m->get_object($hash)
44     or Carp::croak("Couldn't find a object for '$hash' in '$pd'!");
45
46   return $obj;
47 }
48
49 =head2 index
50
51 Provides the project listing.
52
53 =cut
54
55 sub index :Path :Args(0) {
56   my ( $self, $c ) = @_;
57
58   $c->detach($c->req->param('a'))
59     if $c->req->param('a');
60
61   my @list = @{ $c->model()->projects };
62   die 'No projects found in '. $c->model->repo_dir
63     unless @list;
64
65   my $search = $c->req->param('s') || '';
66   if($search) {
67     @list = grep {
68          index($_->name, $search) > -1
69       or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 )
70     } @list
71   }
72
73   $c->stash(
74     search_text => $search,
75     projects    => \@list,
76     action      => 'index',
77   );
78 }
79
80 sub project_index : Local {
81   my ( $self, $c ) = @_;
82
83   my @list = @{ $c->model()->projects };
84   die 'No projects found in '. $c->model->repo_dir
85     unless @list;
86
87   $c->response->content_type('text/plain');
88   $c->response->body(
89     join "\n", map $_->name, @list
90   );
91   $c->response->status(200);
92 }
93
94 =head2 summary
95
96 A summary of what's happening in the repo.
97
98 =cut
99
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;
107   $c->stash(
108     commit    => $commit,
109     log_lines => [$project->list_revs(
110         sha1 => $commit->sha1,
111         count => $maxitems,
112     )],
113     refs      => $project->references,
114     heads     => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ],
115     action    => 'summary',
116   );
117 }
118
119 =head2 heads
120
121 The current list of heads (aka branches) in the repo.
122
123 =cut
124
125 sub heads : Local {
126   my ( $self, $c ) = @_;
127   my $project = $c->stash->{Project};
128   $c->stash(
129     commit => $self->_get_object($c),
130     heads  => $project->heads,
131     action => 'heads',
132   );
133 }
134
135 =head2 tags
136
137 The current list of tags in the repo.
138
139 =cut
140
141 sub tags : Local {
142   my ( $self, $c ) = @_;
143   my $project = $c->stash->{Project};
144   $c->stash(
145     commit => $self->_get_object($c),
146     tags   => $project->tags,
147     action => 'tags',
148   );
149 }
150
151 sub blame : Local {
152   my($self, $c) = @_;
153
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') || '';
162
163   $c->stash(
164     blame    => $project->get_object($hb)->blame($filename),
165     head     => $project->get_object($hb),
166     filename => $filename,
167   );
168   
169 }
170
171 sub _blob_objs {
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.";
180
181   my $filename = $c->req->param('f') || '';
182
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';
187
188   return $blob, $project->get_object($hb), $filename;
189 }
190
191 =head2 blob
192
193 The blob action i.e the contents of a file.
194
195 =cut
196
197 sub blob : Local {
198   my ( $self, $c ) = @_;
199
200   my($blob, $head, $filename) = $self->_blob_objs($c);
201   $c->stash(
202     blob     => $blob->content,
203     head     => $head,
204     filename => $filename,
205     # XXX Hack hack hack, see View::SyntaxHighlight
206     language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
207     action   => 'blob',
208   );
209
210   $c->forward('View::SyntaxHighlight')
211     unless $c->stash->{no_wrapper};
212 }
213
214 =head2 blob_plain
215
216 The plain text version of blob, where file is rendered as is.
217
218 =cut
219
220 sub blob_plain : Local {
221   my($self, $c) = @_;
222
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);
227 }
228
229 =head2 blobdiff_plain
230
231 The plain text version of blobdiff.
232
233 =cut
234
235 sub blobdiff_plain : Local {
236   my($self, $c) = @_;
237
238   $c->stash(no_wrapper => 1);
239   $c->response->content_type('text/plain; charset=utf-8');
240
241   $c->forward('blobdiff');
242 }
243
244 =head2 blobdiff
245
246 Exposes a given diff of a blob.
247
248 =cut
249
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(
256     commit => $commit,
257     patch  => 1,
258     parent => $c->req->param('hpb') || undef,
259     file   => $filename,
260   );
261   $c->stash(
262     commit    => $commit,
263     diff      => $patch,
264     filename  => $filename,
265     # XXX Hack hack hack, see View::SyntaxHighlight
266     blobs     => [$patch->[0]->{diff}],
267     language  => 'Diff',
268     action    => 'blobdiff',
269   );
270
271   $c->forward('View::SyntaxHighlight')
272     unless $c->stash->{no_wrapper};
273 }
274
275 =head2 commit
276
277 Exposes a given commit.
278
279 =cut
280
281 sub commit : Local {
282   my ( $self, $c ) = @_;
283   my $project = $c->stash->{Project};
284   my $commit = $self->_get_object($c);
285   $c->stash(
286       commit      => $commit,
287       diff_tree   => ($project->diff(commit => $commit))[0],
288       refs      => $project->references,
289       action      => 'commit',
290   );
291 }
292
293 =head2 commitdiff
294
295 Exposes a given diff of a commit.
296
297 =cut
298
299 sub commitdiff : Local {
300   my ( $self, $c ) = @_;
301   my $commit = $self->_get_object($c);
302   my($tree, $patch) = $c->stash->{Project}->diff(
303       commit => $commit,
304       parent => $c->req->param('hp') || undef,
305       patch  => 1,
306   );
307   $c->stash(
308     commit    => $commit,
309     diff_tree => $tree,
310     diff      => $patch,
311     # XXX Hack hack hack, see View::SyntaxHighlight
312     blobs     => [map $_->{diff}, @$patch],
313     language  => 'Diff',
314     action    => 'commitdiff',
315   );
316
317   $c->forward('View::SyntaxHighlight')
318     unless $c->stash->{no_wrapper};
319 }
320
321 sub commitdiff_plain : Local {
322   my($self, $c) = @_;
323
324   $c->stash(no_wrapper => 1);
325   $c->response->content_type('text/plain; charset=utf-8');
326
327   $c->forward('commitdiff');
328 }
329
330 =head2 shortlog
331
332 Expose an abbreviated log of a given sha1.
333
334 =cut
335
336 sub shortlog : Local {
337   my ( $self, $c ) = @_;
338
339   my $project  = $c->stash->{Project};
340   my $commit   = $self->_get_object($c, $c->req->param('hb'));
341   my $filename = $c->req->param('f') || '';
342
343   my %logargs = (
344       sha1   => $commit->sha1,
345       count  => Gitalist->config->{paging}{log} || 25,
346       ($filename ? (file => $filename) : ())
347   );
348
349   my $page = $c->req->param('pg') || 0;
350   $logargs{skip} = $c->req->param('pg') * $logargs{count}
351     if $c->req->param('pg');
352
353   $c->stash(
354       commit    => $commit,
355       log_lines => [$project->list_revs(%logargs)],
356       refs      => $project->references,
357       page      => $page,
358       filename  => $filename,
359       action    => 'shortlog',
360   );
361 }
362
363 =head2 log
364
365 Calls shortlog internally. Perhaps that should be reversed ...
366
367 =cut
368 sub log : Local {
369     $_[0]->shortlog($_[1]);
370     $_[1]->stash->{action} = 'log';
371 }
372
373 # For legacy support.
374 sub history : Local {
375   $_[1]->forward('shortlog');
376 }
377
378 =head2 tree
379
380 The tree of a given commit.
381
382 =cut
383
384 sub tree : Local {
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') || '';
389   my $tree    = $filename
390     ? $project->get_object($project->hash_by_path($commit->sha1, $filename))
391     : $project->get_object($commit->tree_sha1)
392   ;
393   $c->stash(
394       commit    => $commit,
395       tree      => $tree,
396       tree_list => [$project->list_tree($tree->sha1)],
397       path      => $c->req->param('f') || '',
398       action    => 'tree',
399   );
400 }
401
402 =head2 reflog
403
404 Expose the local reflog. This may go away.
405
406 =cut
407
408 sub reflog : Local {
409   my ( $self, $c ) = @_;
410   my @log = $c->stash->{Project}->reflog(
411       '--since=yesterday'
412   );
413
414   $c->stash(
415       log    => \@log,
416       action => 'reflog',
417   );
418 }
419
420 =head2 search
421
422 The action for the search form.
423
424 =cut
425
426 sub search : Local {
427   my($self, $c) = @_;
428   $c->stash(current_action => 'GitRepos');
429   my $project = $c->stash->{Project};
430   my $commit  = $self->_get_object($c);
431   # Lifted from /shortlog.
432   my %logargs = (
433     sha1   => $commit->sha1,
434     count  => Gitalist->config->{paging}{log},
435     ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
436     search => {
437       type   => $c->req->param('type'),
438       text   => $c->req->param('text'),
439       regexp => $c->req->param('regexp') || 0,
440     },
441   );
442
443   $c->stash(
444       commit  => $commit,
445       results => [$project->list_revs(%logargs)],
446       action  => 'search',
447           # This could be added - page      => $page,
448   );
449 }
450
451 =head2 search_help
452
453 Provides some help for the search form.
454
455 =cut
456
457 sub search_help : Local {
458     my ($self, $c) = @_;
459     $c->stash(template => 'search_help.tt2');
460 }
461
462 =head2 atom
463
464 Provides an atom feed for a given project.
465
466 =cut
467
468 sub atom : Local {
469   my($self, $c) = @_;
470
471   my $feed = XML::Atom::Feed->new;
472
473   my $host = lc Sys::Hostname::hostname();
474   $feed->title($host . ' - ' . Gitalist->config->{name});
475   $feed->updated(~~DateTime->now);
476
477   my $project = $c->stash->{Project};
478   my %logargs = (
479       sha1   => $project->head_hash,
480       count  => Gitalist->config->{paging}{log} || 25,
481       ($c->req->param('f') ? (file => $c->req->param('f')) : ())
482   );
483
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}));
489     # XXX Needs work ...
490     $entry->content($commit->comment);
491     $feed->add_entry($entry);
492   }
493
494   $c->response->body($feed->as_xml);
495   $c->response->content_type('application/atom+xml');
496   $c->response->status(200);
497 }
498
499 =head2 rss
500
501 Provides an RSS feed for a given project.
502
503 =cut
504
505 sub rss : Local {
506   my ($self, $c) = @_;
507
508   my $project = $c->stash->{Project};
509
510   my $rss = XML::RSS->new(version => '2.0');
511   $rss->channel(
512     title          => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name},
513     link           => $c->uri_for('summary', {p=>$project->name}),
514     language       => 'en',
515     description    => $project->description,
516     pubDate        => DateTime->now,
517     lastBuildDate  => DateTime->now,
518   );
519
520   my %logargs = (
521       sha1   => $project->head_hash,
522       count  => Gitalist->config->{paging}{log} || 25,
523       ($c->req->param('f') ? (file => $c->req->param('f')) : ())
524   );
525   my $mk_title = $c->stash->{short_cmt};
526   for my $commit ($project->list_revs(%logargs)) {
527     # XXX Needs work ....
528     $rss->add_item(
529         title       => $mk_title->($commit->comment),
530         permaLink   => $c->uri_for(commit => {h=>$commit->sha1}),
531         description => $commit->comment,
532     );
533   }
534
535   $c->response->body($rss->as_string);
536   $c->response->content_type('application/rss+xml');
537   $c->response->status(200);
538 }
539
540 sub opml : Local {
541   my($self, $c) = @_;
542
543   my $opml = XML::OPML::SimpleGen->new();
544
545   $opml->head(title => lc(Sys::Hostname::hostname()) . ' - ' . Gitalist->config->{name});
546
547   my @list = @{ $c->model()->projects };
548   die 'No projects found in '. $c->model->repo_dir
549     unless @list;
550
551   for my $proj ( @list ) {
552     $opml->insert_outline(
553       text   => $proj->name. ' - '. $proj->description,
554       xmlUrl => $c->uri_for(rss => {p => $proj->name}),
555     );
556   }
557
558   $c->response->body($opml->as_string);
559   $c->response->content_type('application/rss');
560   $c->response->status(200);
561 }
562
563 =head2 patch
564
565 A raw patch for a given commit.
566
567 =cut
568
569 sub patch : Local {
570     my ($self, $c) = @_;
571     $c->detach('patches', [1]);
572 }
573
574 =head2 patches
575
576 The patcheset for a given commit ???
577
578 =cut
579
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);
589 }
590
591 =head2 snapshot
592
593 Provides a snapshot of a given commit.
594
595 =cut
596
597 sub snapshot : Local {
598     my ($self, $c) = @_;
599     my $format = $c->req->param('sf') || 'tgz';
600     die unless $format;
601     my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1;
602     my @snap = $c->stash->{Project}->snapshot(
603         sha1 => $sha1,
604         format => $format
605     );
606     $c->response->status(200);
607     $c->response->headers->header( 'Content-Disposition' =>
608                                        "attachment; filename=$snap[0]");
609     $c->response->body($snap[1]);
610 }
611
612 =head2 auto
613
614 Populate the header and footer. Perhaps not the best location.
615
616 =cut
617
618 sub auto : Private {
619   my($self, $c) = @_;
620
621   my $project = $c->req->param('p');
622   if (defined $project) {
623     eval {
624       $c->stash(Project => $c->model('GitRepos')->project($project));
625     };
626     if ($@) {
627       $c->detach('error_404');
628     }
629   }
630
631   my $a_project = $c->stash->{Project} || $c->model()->projects->[0];
632   $c->stash(
633     git_version => $a_project->run_cmd('--version'),
634     version     => $Gitalist::VERSION,
635
636     # XXX Move these to a plugin!
637     time_since => sub {
638       return 'never' unless $_[0];
639       return age_string(time - $_[0]->epoch);
640     },
641     short_cmt => sub {
642       my $cmt = shift;
643       my($line) = split /\n/, $cmt;
644       $line =~ s/^(.{70,80}\b).*/$1 \x{2026}/;
645       return $line;
646     },
647     abridged_description => sub {
648         join(' ', grep { defined } (split / /, shift)[0..10]);
649     },
650   );
651 }
652
653 =head2 end
654
655 Attempt to render a view, if needed.
656
657 =cut
658
659 sub end : ActionClass('RenderView') {
660     my ($self, $c) = @_;
661     # Give project views the current HEAD.
662     if ($c->stash->{Project}) {
663         $c->stash->{HEAD} = $c->stash->{Project}->head_hash;
664     }
665 }
666
667 sub error_404 :Private {
668     my ($self, $c) = @_;
669     $c->response->status(404);
670     $c->stash(
671         title => 'Page not found',
672         content => 'Page not found',
673     );
674 }
675
676 sub age_string {
677   my $age = shift;
678   my $age_str;
679
680   if ( $age > 60 * 60 * 24 * 365 * 2 ) {
681     $age_str  = ( int $age / 60 / 60 / 24 / 365 );
682     $age_str .= " years ago";
683   }
684   elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
685     $age_str  = int $age / 60 / 60 / 24 / ( 365 / 12 );
686     $age_str .= " months ago";
687   }
688   elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
689     $age_str  = int $age / 60 / 60 / 24 / 7;
690     $age_str .= " weeks ago";
691   }
692   elsif ( $age > 60 * 60 * 24 * 2 ) {
693     $age_str  = int $age / 60 / 60 / 24;
694     $age_str .= " days ago";
695   }
696   elsif ( $age > 60 * 60 * 2 ) {
697     $age_str  = int $age / 60 / 60;
698     $age_str .= " hours ago";
699   }
700   elsif ( $age > 60 * 2 ) {
701     $age_str  = int $age / 60;
702     $age_str .= " min ago";
703   }
704   elsif ( $age > 2 ) {
705     $age_str  = int $age;
706     $age_str .= " sec ago";
707   }
708   else {
709     $age_str .= " right now";
710   }
711   return $age_str;
712 }
713
714
715 =head1 AUTHOR
716
717 Dan Brook
718
719 =head1 LICENSE
720
721 This library is free software. You can redistribute it and/or modify
722 it under the same terms as Perl itself.
723
724 =cut
725
726 __PACKAGE__->meta->make_immutable;