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