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