Kill warnings
[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 #
8 # Sets the actions in this controller to be registered with no prefix
9 # so they function identically to actions created in MyApp.pm
10 #
11 __PACKAGE__->config->{namespace} = '';
12
13 =head1 NAME
14
15 Gitalist::Controller::Root - Root Controller for Gitalist
16
17 =head1 DESCRIPTION
18
19 [enter your description here]
20
21 =head1 METHODS
22
23 =cut
24
25 =head2 index
26
27 =cut
28
29 use IO::Capture::Stdout;
30
31 =head2 run_gitweb
32
33 The C<gitweb> shim. It should now only be explicitly accessible by
34 modifying the URL.
35
36 =cut
37
38 sub run_gitweb {
39   my ( $self, $c ) = @_;
40
41   # XXX A slippery slope to be sure.
42   if($c->req->param('a')) {
43     my $capture = IO::Capture::Stdout->new();
44     $capture->start();
45     eval {
46       my $action = gitweb::main($c);
47       $action->();
48     };
49     $capture->stop();
50
51     use Data::Dumper;
52     die Dumper($@)
53       if $@;
54
55     my $output = join '', $capture->read;
56     $c->stash->{gitweb_output} = $output;
57     $c->stash->{template} = 'gitweb.tt2';
58   }
59 }
60
61 sub _get_commit {
62   my($self, $c, $haveh) = @_;
63
64   my $h = $haveh || $c->req->param('h') || '';
65   my $f = $c->req->param('f');
66
67   # FIXME this can die when everything is migrated
68   my ($m, $pd);
69   if ($c->stash->{current_model} eq 'GitRepos') {
70       $m = $c->model()->project($c->stash->{project});
71       $pd = $m->path;
72   } else {
73       $m = $c->model();
74       ($pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
75   }
76
77   # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
78   my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
79           || ($f && $m->hash_by_path($f))
80           || $m->head_hash
81           # XXX This could definitely use more context.
82           || Carp::croak("Couldn't find a hash for the commit object!");
83
84   my $commit = $m->get_object($hash)
85     or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
86
87   return $commit;
88 }
89
90 =head2 index
91
92 Provides the project listing.
93
94 =cut
95
96 sub index :Path :Args(0) {
97   my ( $self, $c ) = @_;
98   $c->stash(current_model => 'GitRepos');
99   # Leave actions up to gitweb at this point.
100   return $self->run_gitweb($c)
101     if $c->req->param('a');
102
103   my $list = $c->model()->list_projects;
104   unless(@$list) {
105     die "No projects found in ". $c->model->repo_dir;
106   }
107
108   $c->stash(
109     searchtext => $c->req->param('searchtext') || '',
110     projects   => $list,
111     action     => 'index',
112   );
113 }
114
115 =head2 summary
116
117 A summary of what's happening in the repo.
118
119 =cut
120
121 sub summary : Local {
122   my ( $self, $c ) = @_;
123
124   my $commit = $self->_get_commit($c);
125   $c->stash(
126     commit    => $commit,
127     info      => $c->model()->project_info($c->model()->project),
128     log_lines => [$c->model()->list_revs(
129       sha1 => $commit->sha1, count => Gitalist->config->{paging}{summary}
130     )],
131     refs      => $c->model()->references,
132     heads     => [$c->model()->heads],
133     action    => 'summary',
134   );
135 }
136
137 =head2 heads
138
139 The current list of heads (aka branches) in the repo.
140
141 =cut
142
143 sub heads : Local {
144   my ( $self, $c ) = @_;
145   $c->stash( current_model => 'GitRepos' );
146   my $project = $c->model()->project( $c->stash->{project} );
147   $c->stash(
148     commit => $self->_get_commit($c),
149     heads  => [$project->heads],
150     action => 'heads',
151   );
152 }
153
154 =head2 blob
155
156 The blob action i.e the contents of a file.
157
158 =cut
159
160 sub blob : Local {
161   my ( $self, $c ) = @_;
162
163   my $h  = $c->req->param('h')
164        || $c->model()->hash_by_path($c->req->param('f'))
165        || die "No file or sha1 provided.";
166   my $hb = $c->req->param('hb')
167        || $c->model()->head_hash
168        || die "Couldn't discern the corresponding head.";
169
170   my $filename = $c->req->param('f') || '';
171
172   $c->stash(
173     blob     => $c->model()->get_object($h)->content,
174     head     => $c->model()->get_object($hb),
175     filename => $filename,
176     # XXX Hack hack hack, see View::SyntaxHighlight
177     language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
178     action   => 'blob',
179   );
180
181   $c->forward('View::SyntaxHighlight');
182 }
183
184 =head2 blobdiff
185
186 Exposes a given diff of a blob.
187
188 =cut
189
190 sub blobdiff : Local {
191   my ( $self, $c ) = @_;
192
193   my $commit = $self->_get_commit($c);
194   my $filename = $c->req->param('f')
195               || croak("No file specified!");
196   my($tree, $patch) = $c->model()->diff(
197     commit => $commit,
198     parent => $c->req->param('hp') || '',
199     file   => $filename,
200     patch  => 1,
201   );
202   $c->stash(
203     commit    => $commit,
204     diff      => $patch,
205     # XXX Hack hack hack, see View::SyntaxHighlight
206     blobs     => [$patch->[0]->{diff}],
207     language  => 'Diff',
208     action    => 'blobdiff',
209   );
210
211   $c->forward('View::SyntaxHighlight');
212 }
213
214 =head2 commit
215
216 Exposes a given commit.
217
218 =cut
219
220 sub commit : Local {
221   my ( $self, $c ) = @_;
222
223   my $commit = $self->_get_commit($c);
224   $c->stash(
225       commit      => $commit,
226       diff_tree   => ($c->model()->diff(commit => $commit))[0],
227       refs      => $c->model()->references,
228       action      => 'commit',
229   );
230 }
231
232 =head2 commitdiff
233
234 Exposes a given diff of a commit.
235
236 =cut
237
238 sub commitdiff : Local {
239   my ( $self, $c ) = @_;
240
241   my $commit = $self->_get_commit($c);
242   my($tree, $patch) = $c->model()->diff(
243       commit => $commit,
244       parent => $c->req->param('hp') || '',
245       patch  => 1,
246   );
247   $c->stash(
248     commit    => $commit,
249     diff_tree => $tree,
250     diff      => $patch,
251     # XXX Hack hack hack, see View::SyntaxHighlight
252     blobs     => [map $_->{diff}, @$patch],
253     language  => 'Diff',
254     action    => 'commitdiff',
255   );
256
257   $c->forward('View::SyntaxHighlight');
258 }
259
260 =head2 shortlog
261
262 Expose an abbreviated log of a given sha1.
263
264 =cut
265
266 sub shortlog : Local {
267   my ( $self, $c ) = @_;
268
269   my $commit  = $self->_get_commit($c);
270   my %logargs = (
271       sha1   => $commit->sha1,
272       count  => Gitalist->config->{paging}{log},
273       ($c->req->param('f') ? (file => $c->req->param('f')) : ())
274   );
275
276   my $page = $c->req->param('pg') || 0;
277   $logargs{skip} = $c->req->param('pg') * $logargs{count}
278     if $c->req->param('pg');
279
280   $c->stash(
281       commit    => $commit,
282       log_lines => [$c->model()->list_revs(%logargs)],
283       refs      => $c->model()->references,
284       action    => 'shortlog',
285       page      => $page,
286   );
287 }
288
289 =head2 log
290
291 Calls shortlog internally. Perhaps that should be reversed ...
292
293 =cut
294 sub log : Local {
295     $_[0]->shortlog($_[1]);
296     $_[1]->stash->{action} = 'log';
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
308   my $commit = $self->_get_commit($c, $c->req->param('hb'));
309   my $tree   = $c->model()->get_object($c->req->param('h') || $commit->tree_sha1);
310   $c->stash(
311       # XXX Useful defaults needed ...
312       commit    => $commit,
313       tree      => $tree,
314       tree_list => [$c->model()->list_tree($tree->sha1)],
315           path      => $c->req->param('f') || '',
316       action    => 'tree',
317   );
318 }
319
320 =head2 reflog
321
322 Expose the local reflog. This may go away.
323
324 =cut
325
326 sub reflog : Local {
327   my ( $self, $c ) = @_;
328
329   my @log = $c->model()->reflog(
330       '--since=yesterday'
331   );
332
333   $c->stash(
334       log    => \@log,
335       action => 'reflog',
336   );
337 }
338
339 sub search : Local {
340   my($self, $c) = @_;
341
342   my $commit  = $self->_get_commit($c);
343   # Lifted from /shortlog.
344   my %logargs = (
345     sha1   => $commit->sha1,
346     count  => Gitalist->config->{paging}{log},
347     ($c->req->param('f') ? (file => $c->req->param('f')) : ()),
348         search => {
349           type   => $c->req->param('type'),
350           text   => $c->req->param('text'),
351           regexp => $c->req->param('regexp') || 0,
352     }
353   );
354
355   $c->stash(
356       commit  => $commit,
357       results => [$c->model()->list_revs(%logargs)],
358       action  => 'search',
359           # This could be added - page      => $page,
360   );
361 }
362
363 sub search_help : Local {
364     Carp::croak "Not implemented.";
365 }
366
367 =head2 auto
368
369 Populate the header and footer. Perhaps not the best location.
370
371 =cut
372
373 sub auto : Private {
374   my($self, $c) = @_;
375
376   # XXX Move these to a plugin!
377   $c->stash(
378     time_since => sub {
379       return 'never' unless $_[0];
380       return age_string(time - $_[0]->epoch);
381     },
382     short_cmt => sub {
383       my $cmt = shift;
384       my($line) = split /\n/, $cmt;
385       $line =~ s/^(.{70,80}\b).*/$1 …/;
386       return $line;
387     },
388     abridged_description => sub {
389         join(' ', grep { defined } (split / /, shift)[0..10]);
390     },
391   );
392
393   # Yes, this is hideous.
394   $self->header($c);
395   $self->footer($c);
396 }
397
398 # XXX This could probably be dropped altogether.
399 use Gitalist::Util qw(to_utf8);
400 # Formally git_header_html
401 sub header {
402   my($self, $c) = @_;
403
404   my $title = $c->config->{sitename};
405
406   my $project   = $c->req->param('project')  || $c->req->param('p');
407   my $action    = $c->req->param('action')   || $c->req->param('a');
408   my $file_name = $c->req->param('filename') || $c->req->param('f');
409   if(defined $project) {
410     $title .= " - " . to_utf8($project);
411     if (defined $action) {
412       $title .= "/$action";
413       if (defined $file_name) {
414         $title .= " - " . $file_name;
415         if ($action eq "tree" && $file_name !~ m|/$|) {
416           $title .= "/";
417         }
418       }
419     }
420   }
421
422   $c->stash->{version}     = $c->config->{version};
423   $c->stash->{git_version} = $c->model()->run_cmd('--version');
424   $c->stash->{title}       = $title;
425
426   #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
427   $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
428
429   $c->stash->{project} = $project;
430   my @links;
431   if($project) {
432     my %href_params = $self->feed_info($c);
433     $href_params{'-title'} ||= 'log';
434
435     foreach my $format qw(RSS Atom) {
436       my $type = lc($format);
437       push @links, {
438         rel   => 'alternate',
439         title => "$project - $href_params{'-title'} - $format feed",
440
441         # XXX A bit hacky and could do with using gitweb::href() features
442         href  => "?a=$type;p=$project",
443         type  => "application/$type+xml"
444         }, {
445         rel   => 'alternate',
446
447         # XXX This duplication also feels a bit awkward
448         title => "$project - $href_params{'-title'} - $format feed (no merges)",
449         href  => "?a=$type;p=$project;opt=--no-merges",
450         type  => "application/$type+xml"
451         };
452     }
453   } else {
454     push @links, {
455       rel => "alternate",
456       title => $c->config->{sitename}." projects list",
457       href => '?a=project_index',
458       type => "text/plain; charset=utf-8"
459       }, {
460       rel => "alternate",
461       title => $c->config->{sitename}." projects feeds",
462       href => '?a=opml',
463       type => "text/plain; charset=utf-8"
464       };
465   }
466
467   $c->stash->{favicon} = $c->config->{favicon};
468
469   # </head><body>
470
471   $c->stash(
472     logo_url      => $c->config->{logo_url},
473     logo_label    => $c->config->{logo_label},
474     logo_img      => $c->config->{logo},
475     home_link     => $c->config->{home_link},
476     home_link_str => $c->config->{home_link_str},
477     );
478
479   if(defined $project) {
480     $c->stash(
481       search_text => ( $c->req->param('s') || $c->req->param('searchtext') || ''),
482       search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
483           || $c->req->param('h')  || $c->req->param('hash')
484           || 'HEAD' ),
485       );
486   }
487 }
488
489 # Formally git_footer_html
490 sub footer {
491   my($self, $c) = @_;
492
493   my $feed_class = 'rss_logo';
494
495   my @feeds;
496   my $project = $c->req->param('project')  || $c->req->param('p');
497   if(defined $project) {
498     (my $pstr = $project) =~ s[/?\.git$][];
499     my $descr = $c->model()->project_info($project)->{description};
500     $c->stash->{project_description} = defined $descr
501       ? $descr
502       : '';
503
504     my %href_params = $self->feed_info($c);
505     if (!%href_params) {
506       $feed_class .= ' generic';
507     }
508     $href_params{'-title'} ||= 'log';
509
510     @feeds = [
511       map +{
512         class => $feed_class,
513         title => "$href_params{'-title'} $_ feed",
514         href  => "/?p=$project;a=\L$_",
515         name  => lc $_,
516         }, qw(RSS Atom)
517       ];
518   } else {
519     @feeds = [
520       map {
521         class => $feed_class,
522           title => '',
523           href  => "/?a=$_->[0]",
524           name  => $_->[1],
525         }, [opml=>'OPML'],[project_index=>'TXT'],
526       ];
527   }
528 }
529
530 # XXX This feels wrong here, should probably be refactored.
531 # returns hash to be passed to href to generate gitweb URL
532 # in -title key it returns description of link
533 sub feed_info {
534   my($self, $c) = @_;
535
536   my $format = shift || 'Atom';
537   my %res = (action => lc($format));
538
539   # feed links are possible only for project views
540   return unless $c->req->param('project');
541
542   # some views should link to OPML, or to generic project feed,
543   # or don't have specific feed yet (so they should use generic)
544   return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
545
546   my $branch;
547   my $hash = $c->req->param('h')  || $c->req->param('hash');
548   my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
549
550   # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
551   # from tag links; this also makes possible to detect branch links
552   if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
553     (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
554     $branch = $1;
555   }
556
557   # find log type for feed description (title)
558   my $type = 'log';
559   my $file_name = $c->req->param('f') || $c->req->param('filename');
560   if (defined $file_name) {
561     $type  = "history of $file_name";
562     $type .= "/" if $c->req->param('action') eq 'tree';
563     $type .= " on '$branch'" if (defined $branch);
564   } else {
565     $type = "log of $branch" if (defined $branch);
566   }
567
568   $res{-title} = $type;
569   $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
570   $res{'file_name'} = $file_name;
571
572   return %res;
573 }
574
575 =head2 end
576
577 Attempt to render a view, if needed.
578
579 =cut
580
581 sub end : ActionClass('RenderView') {
582   my ($self, $c) = @_;
583   # Give project views the current HEAD.
584   if ($c->stash->{project}) {
585       if ($c->stash->{current_model} eq 'GitRepos') {
586           my $project = $c->model()->project($c->stash->{project});
587           $c->stash->{HEAD} = $project->head_hash;
588       } else {
589           $c->stash->{HEAD} = $c->model()->head_hash;
590       }
591   }
592 }
593
594 sub age_string {
595         my $age = shift;
596         my $age_str;
597
598         if ($age > 60*60*24*365*2) {
599                 $age_str = (int $age/60/60/24/365);
600                 $age_str .= " years ago";
601         } elsif ($age > 60*60*24*(365/12)*2) {
602                 $age_str = int $age/60/60/24/(365/12);
603                 $age_str .= " months ago";
604         } elsif ($age > 60*60*24*7*2) {
605                 $age_str = int $age/60/60/24/7;
606                 $age_str .= " weeks ago";
607         } elsif ($age > 60*60*24*2) {
608                 $age_str = int $age/60/60/24;
609                 $age_str .= " days ago";
610         } elsif ($age > 60*60*2) {
611                 $age_str = int $age/60/60;
612                 $age_str .= " hours ago";
613         } elsif ($age > 60*2) {
614                 $age_str = int $age/60;
615                 $age_str .= " min ago";
616         } elsif ($age > 2) {
617                 $age_str = int $age;
618                 $age_str .= " sec ago";
619         } else {
620                 $age_str .= " right now";
621         }
622         return $age_str;
623 }
624
625 =head1 AUTHOR
626
627 Dan Brook
628
629 =head1 LICENSE
630
631 This library is free software. You can redistribute it and/or modify
632 it under the same terms as Perl itself.
633
634 =cut
635
636 __PACKAGE__->meta->make_immutable;