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