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