8f90b64070f740707525757663352190a138579d
[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 main shim around C<gitweb.pm>.
34
35 =cut
36
37 sub run_gitweb {
38   my ( $self, $c ) = @_;
39
40   # XXX A slippery slope to be sure.
41   if($c->req->param('a')) {
42     my $capture = IO::Capture::Stdout->new();
43     $capture->start();
44     eval {
45       my $action = gitweb::main($c);
46       $action->();
47     };
48     $capture->stop();
49     
50     use Data::Dumper;
51     die Dumper($@)
52       if $@;
53   
54     my $output = join '', $capture->read;
55     $c->stash->{gitweb_output} = $output;
56     $c->stash->{template} = 'gitweb.tt2';
57   }
58 }
59
60 sub _get_commit {
61   my($self, $c) = @_;
62
63   my $h = $c->req->param('h');
64   my $f = $c->req->param('f');
65   my $m = $c->model('Git');
66
67   # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD.
68   my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h)
69           || ($f && $m->hash_by_path($f))
70           || $m->head_hash
71           # XXX This could definitely use more context.
72           || Carp::croak("Couldn't find a hash for the commit object!");
73
74     
75   (my $pd = $m->project_dir( $m->project )) =~ s{/\.git$}();
76   my $commit = $m->get_object($hash)
77     or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!");
78
79   return $commit;
80 }
81
82 =head2 index
83
84 Provides the project listing.
85
86 =cut
87
88 sub index :Path :Args(0) {
89   my ( $self, $c ) = @_;
90
91   # Leave actions up to gitweb at this point.
92   return $self->run_gitweb($c)
93     if $c->req->param('a');
94
95   my $list = $c->model('Git')->list_projects;
96   unless(@$list) {
97     die "No projects found";
98   }
99
100   $c->stash(
101     searchtext => $c->req->param('searchtext') || '',
102     projects   => $list,
103     action     => 'index',
104   );
105 }
106
107 =head2 summary
108
109 A summary of what's happening in the repo.
110
111 =cut
112
113 sub summary : Local {
114   my ( $self, $c ) = @_;
115
116   my $commit = $self->_get_commit($c);
117   $c->stash(
118     commit    => $commit,
119     info      => $c->model('Git')->project_info($c->model('Git')->project),
120     log_lines => [$c->model('Git')->list_revs(sha1 => $commit->sha1, count => 16)],
121     refs      => $c->model('Git')->references,
122     heads     => [$c->model('Git')->heads],
123     HEAD      => $c->model('Git')->head_hash,
124     action    => 'summary',
125   );
126 }
127
128 =head2 heads
129
130 The current list of heads (aka branches) in the repo.
131
132 =cut
133
134 sub heads : Local {
135   my ( $self, $c ) = @_;
136
137   $c->stash(
138     commit => $self->_get_commit($c),
139     heads  => [$c->model('Git')->heads],
140     HEAD   => $c->model('Git')->head_hash,
141     action => 'heads',
142   );
143 }
144
145 =head2 blob
146
147 The blob action i.e the contents of a file.
148
149 =cut
150
151 sub blob : Local {
152   my ( $self, $c ) = @_;
153
154   my $h  = $c->req->param('h')
155        || $c->model('Git')->hash_by_path($c->req->param('f'))
156        || die "No file or sha1 provided.";
157   my $hb = $c->req->param('hb')
158        || $c->model('Git')->head_hash
159        || die "Couldn't discern the corresponding head.";
160
161   my $filename = $c->req->param('f') || '';
162
163   $c->stash(
164     blob     => $c->model('Git')->get_object($h)->content,
165     head     => $c->model('Git')->get_object($hb),
166     filename => $filename,
167     # XXX Hack hack hack, see View::SyntaxHighlight
168     language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''),
169     action   => 'blob',
170   );
171
172   $c->forward('View::SyntaxHighlight');
173 }
174
175 =head2 blobdiff
176
177 Exposes a given diff of a blob.
178
179 =cut
180
181 sub blobdiff : Local {
182   my ( $self, $c ) = @_;
183
184   my $commit = $self->_get_commit($c);
185   my $filename = $c->req->param('f')
186               || croak("No file specified!");
187   my @diff = $c->model('Git')->diff(
188     $commit->parent_sha1, $commit->sha1, '--', $filename
189   );
190   $c->stash(
191     commit    => $commit,
192     diff      => \@diff,
193     # XXX Hack hack hack, see View::SyntaxHighlight
194     blobs     => [$diff[0]->{diff}],
195     language  => 'Diff',
196     action    => 'blobdiff',
197   );
198
199   $c->forward('View::SyntaxHighlight');
200 }
201
202 =head2 commit
203
204 Exposes a given commit.
205
206 =cut
207
208 sub commit : Local {
209   my ( $self, $c ) = @_;
210
211   my $commit = $self->_get_commit($c);
212   $c->stash(
213       commit      => $commit,
214       diff_tree   => [$c->model('Git')->diff_tree($commit)],
215       branches_on => [$c->model('Git')->refs_for($commit->sha1)],
216       action      => 'commit',
217   );
218 }
219
220 =head2 commitdiff
221
222 Exposes a given diff of a commit.
223
224 =cut
225
226 sub commitdiff : Local {
227   my ( $self, $c ) = @_;
228
229   my $commit = $self->_get_commit($c);
230   my @difflist = $c->model('Git')->diff($commit->parent_sha1, $commit->sha1);
231   $c->stash(
232     commit    => $commit,
233     diff_tree => [$c->model('Git')->diff_tree($commit)],
234     diff      => \@difflist,
235     # XXX Hack hack hack, see View::SyntaxHighlight
236     blobs     => [map $_->{diff}, @difflist],
237     language  => 'Diff',
238     action    => 'commitdiff',
239   );
240
241   $c->forward('View::SyntaxHighlight');
242 }
243
244 =head2 shortlog
245
246 Expose an abbreviated log of a given sha1.
247
248 =cut
249
250 sub shortlog : Local {
251   my ( $self, $c ) = @_;
252
253   my $commit  = $self->_get_commit($c);
254   my @logargs = (
255       sha1 => $commit->sha1,
256       ($c->req->param('f') ? (file => $c->req->param('f')) : ())
257   );
258   # XXX Needs paging.
259   $c->stash(
260       commit    => $commit,
261       log_lines => [$c->model('Git')->list_revs(@logargs)],
262       refs      => $c->model('Git')->references,
263       action    => 'shortlog',
264   );
265 }
266
267 =head2 log
268
269 Calls shortlog internally. Perhaps that should be reversed ...
270
271 =cut
272 sub log : Local {
273     $_[0]->shortlog($_[1]);
274     $_[1]->stash->{action} = 'log';
275 }
276
277 =head2 tree
278
279 The tree of a given commit.
280
281 =cut
282
283 sub tree : Local {
284   my ( $self, $c ) = @_;
285
286   my $commit = $self->_get_commit($c);
287   $c->stash(
288       # XXX Useful defaults needed ...
289       commit    => $commit,
290       tree      => $c->model('Git')->get_object($c->req->param('hb')),
291       tree_list => [$c->model('Git')->list_tree($commit->sha1)],
292       action    => 'tree',
293   );
294 }
295
296 =head2 reflog
297
298 Expose the local reflog. This may go away.
299
300 =cut
301
302 sub reflog : Local {
303   my ( $self, $c ) = @_;
304
305   my @log = $c->model('Git')->reflog(
306       '--since=yesterday'
307   );
308
309   $c->stash(
310       log    => \@log,
311       action => 'reflog',
312   );
313 }
314
315 =head2 auto
316
317 Populate the header and footer. Perhaps not the best location.
318
319 =cut
320
321 sub auto : Private {
322     my($self, $c) = @_;
323
324     # XXX Temp hack until a decent solution is invented.
325     $c->model('Git')->project($c->req->param('p'))
326       if $c->req->param('p');
327
328     # Yes, this is hideous.
329     $self->header($c);
330     $self->footer($c);
331 }
332
333 # XXX This could probably be dropped altogether.
334 use Gitalist::Util qw(to_utf8);
335 # Formally git_header_html
336 sub header {
337   my($self, $c) = @_;
338
339   my $title = $c->config->{sitename};
340
341   my $project   = $c->req->param('project')  || $c->req->param('p');
342   my $action    = $c->req->param('action')   || $c->req->param('a');
343   my $file_name = $c->req->param('filename') || $c->req->param('f');
344   if(defined $project) {
345     $title .= " - " . to_utf8($project);
346     if (defined $action) {
347       $title .= "/$action";
348       if (defined $file_name) {
349         $title .= " - " . $file_name;
350         if ($action eq "tree" && $file_name !~ m|/$|) {
351           $title .= "/";
352         }
353       }
354     }
355   }
356
357   $c->stash->{version}     = $c->config->{version};
358   $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
359   $c->stash->{title}       = $title;
360
361   #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
362   $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
363
364   $c->stash->{project} = $project;
365   my @links;
366   if($project) {
367     my %href_params = $self->feed_info($c);
368     $href_params{'-title'} ||= 'log';
369
370     foreach my $format qw(RSS Atom) {
371       my $type = lc($format);
372       push @links, {
373         rel   => 'alternate',
374         title => "$project - $href_params{'-title'} - $format feed",
375
376         # XXX A bit hacky and could do with using gitweb::href() features
377         href  => "?a=$type;p=$project",
378         type  => "application/$type+xml"
379         }, {
380         rel   => 'alternate',
381
382         # XXX This duplication also feels a bit awkward
383         title => "$project - $href_params{'-title'} - $format feed (no merges)",
384         href  => "?a=$type;p=$project;opt=--no-merges",
385         type  => "application/$type+xml"
386         };
387     }
388   } else {
389     push @links, {
390       rel => "alternate",
391       title => $c->config->{sitename}." projects list",
392       href => '?a=project_index',
393       type => "text/plain; charset=utf-8"
394       }, {
395       rel => "alternate",
396       title => $c->config->{sitename}." projects feeds",
397       href => '?a=opml',
398       type => "text/plain; charset=utf-8"
399       };
400   }
401
402   $c->stash->{favicon} = $c->config->{favicon};
403
404   # </head><body>
405
406   $c->stash(
407     logo_url      => $c->config->{logo_url},
408     logo_label    => $c->config->{logo_label},
409     logo_img      => $c->config->{logo},
410     home_link     => $c->config->{home_link},
411     home_link_str => $c->config->{home_link_str},
412     );
413
414   if(defined $project) {
415     $c->stash(
416       search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
417       search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
418           || $c->req->param('h')  || $c->req->param('hash')
419           || 'HEAD' ),
420       );
421   }
422 }
423
424 # Formally git_footer_html
425 sub footer {
426   my($self, $c) = @_;
427
428   my $feed_class = 'rss_logo';
429
430   my @feeds;
431   my $project = $c->req->param('project')  || $c->req->param('p');
432   if(defined $project) {
433     (my $pstr = $project) =~ s[/?\.git$][];
434     my $descr = $c->model('Git')->project_info($project)->{description};
435     $c->stash->{project_description} = defined $descr
436       ? $descr
437       : '';
438
439     my %href_params = $self->feed_info($c);
440     if (!%href_params) {
441       $feed_class .= ' generic';
442     }
443     $href_params{'-title'} ||= 'log';
444
445     @feeds = [
446       map +{
447         class => $feed_class,
448         title => "$href_params{'-title'} $_ feed",
449         href  => "/?p=$project;a=\L$_",
450         name  => lc $_,
451         }, qw(RSS Atom)
452       ];
453   } else {
454     @feeds = [
455       map {
456         class => $feed_class,
457           title => '',
458           href  => "/?a=$_->[0]",
459           name  => $_->[1],
460         }, [opml=>'OPML'],[project_index=>'TXT'],
461       ];
462   }
463 }
464
465 # XXX This feels wrong here, should probably be refactored.
466 # returns hash to be passed to href to generate gitweb URL
467 # in -title key it returns description of link
468 sub feed_info {
469   my($self, $c) = @_;
470
471   my $format = shift || 'Atom';
472   my %res = (action => lc($format));
473
474   # feed links are possible only for project views
475   return unless $c->req->param('project');
476
477   # some views should link to OPML, or to generic project feed,
478   # or don't have specific feed yet (so they should use generic)
479   return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
480
481   my $branch;
482   my $hash = $c->req->param('h')  || $c->req->param('hash');
483   my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
484
485   # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
486   # from tag links; this also makes possible to detect branch links
487   if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
488     (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
489     $branch = $1;
490   }
491
492   # find log type for feed description (title)
493   my $type = 'log';
494   my $file_name = $c->req->param('f') || $c->req->param('filename');
495   if (defined $file_name) {
496     $type  = "history of $file_name";
497     $type .= "/" if $c->req->param('action') eq 'tree';
498     $type .= " on '$branch'" if (defined $branch);
499   } else {
500     $type = "log of $branch" if (defined $branch);
501   }
502
503   $res{-title} = $type;
504   $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
505   $res{'file_name'} = $file_name;
506
507   return %res;
508 }
509 =head2 end
510
511 Attempt to render a view, if needed.
512
513 =cut
514
515 sub end : ActionClass('RenderView') {}
516
517 =head1 AUTHOR
518
519 Dan Brook,,,
520
521 =head1 LICENSE
522
523 This library is free software. You can redistribute it and/or modify
524 it under the same terms as Perl itself.
525
526 =cut
527
528 __PACKAGE__->meta->make_immutable;