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