Fixed properties of the Model so one can navigate between repos.
[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   $c->stash(
162     blob     => $c->model('Git')->get_object($h)->content,
163     head     => $c->model('Git')->get_object($hb),
164     filename => $c->req->param('f') || '',
165     action   => 'blob',
166   );
167
168   $c->forward('View::SyntaxHighlight');
169 }
170
171 =head2 commit
172
173 Exposes a given commit.
174
175 =cut
176
177 sub commit : Local {
178   my ( $self, $c ) = @_;
179
180   my $commit = $self->_get_commit($c);
181   $c->stash(
182       commit      => $commit,
183       diff_tree   => [$c->model('Git')->diff_tree($commit)],
184       branches_on => [$c->model('Git')->refs_for($commit->sha1)],
185       action      => 'commit',
186   );
187 }
188
189 =head2 commitdiff
190
191 Exposes a given diff of a commit.
192
193 =cut
194
195 sub commitdiff : Local {
196   my ( $self, $c ) = @_;
197
198   my $commit = $self->_get_commit($c);
199   $c->stash(
200       commit      => $commit,
201       diff_tree   => [$c->model('Git')->diff_tree($commit)],
202       diff        => [$c->model('Git')->diff($commit->parent_sha1, $commit->sha1)],
203       action      => 'commitdiff',
204   );
205 }
206
207 =head2 shortlog
208
209 Expose an abbreviated log of a given sha1.
210
211 =cut
212
213 sub shortlog : Local {
214   my ( $self, $c ) = @_;
215
216   my $commit = $self->_get_commit($c);
217   # XXX Needs paging.
218   $c->stash(
219       commit    => $commit,
220       log_lines => [$c->model('Git')->list_revs(sha1 => $commit->sha1)],
221       refs      => $c->model('Git')->references,
222       action    => 'shortlog',
223   );
224 }
225
226 =head2 log
227
228 Calls shortlog internally. Perhaps that should be reversed ...
229
230 =cut
231 sub log : Local {
232     $_[0]->shortlog($_[1]);
233     $_[1]->stash->{action} = 'log';
234 }
235
236 =head2 tree
237
238 The tree of a given commit.
239
240 =cut
241
242 sub tree : Local {
243   my ( $self, $c ) = @_;
244
245   my $commit = $self->_get_commit($c);
246   $c->stash(
247       # XXX Useful defaults needed ...
248       commit    => $commit,
249       tree      => $c->model('Git')->get_object($c->req->param('hb')),
250       tree_list => [$c->model('Git')->list_tree($commit->sha1)],
251       action    => 'tree',
252   );
253 }
254
255 =head2 reflog
256
257 Expose the local reflog. This may go away.
258
259 =cut
260
261 sub reflog : Local {
262   my ( $self, $c ) = @_;
263
264   my @log = $c->model('Git')->reflog(
265       '--since=yesterday'
266   );
267
268   $c->stash(
269       log    => \@log,
270       action => 'reflog',
271   );
272 }
273
274 =head2 auto
275
276 Populate the header and footer. Perhaps not the best location.
277
278 =cut
279
280 sub auto : Private {
281     my($self, $c) = @_;
282
283     # XXX Temp hack until a decent solution is invented.
284     $c->model('Git')->project($c->req->param('p'))
285       if $c->req->param('p');
286
287     # Yes, this is hideous.
288     $self->header($c);
289     $self->footer($c);
290 }
291
292 # XXX This could probably be dropped altogether.
293 use Gitalist::Util qw(to_utf8);
294 # Formally git_header_html
295 sub header {
296   my($self, $c) = @_;
297
298   my $title = $c->config->{sitename};
299
300   my $project   = $c->req->param('project')  || $c->req->param('p');
301   my $action    = $c->req->param('action')   || $c->req->param('a');
302   my $file_name = $c->req->param('filename') || $c->req->param('f');
303   if(defined $project) {
304     $title .= " - " . to_utf8($project);
305     if (defined $action) {
306       $title .= "/$action";
307       if (defined $file_name) {
308         $title .= " - " . $file_name;
309         if ($action eq "tree" && $file_name !~ m|/$|) {
310           $title .= "/";
311         }
312       }
313     }
314   }
315
316   $c->stash->{version}     = $c->config->{version};
317   $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
318   $c->stash->{title}       = $title;
319
320   #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
321   $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
322
323   $c->stash->{project} = $project;
324   my @links;
325   if($project) {
326     my %href_params = $self->feed_info($c);
327     $href_params{'-title'} ||= 'log';
328
329     foreach my $format qw(RSS Atom) {
330       my $type = lc($format);
331       push @links, {
332         rel   => 'alternate',
333         title => "$project - $href_params{'-title'} - $format feed",
334
335         # XXX A bit hacky and could do with using gitweb::href() features
336         href  => "?a=$type;p=$project",
337         type  => "application/$type+xml"
338         }, {
339         rel   => 'alternate',
340
341         # XXX This duplication also feels a bit awkward
342         title => "$project - $href_params{'-title'} - $format feed (no merges)",
343         href  => "?a=$type;p=$project;opt=--no-merges",
344         type  => "application/$type+xml"
345         };
346     }
347   } else {
348     push @links, {
349       rel => "alternate",
350       title => $c->config->{sitename}." projects list",
351       href => '?a=project_index',
352       type => "text/plain; charset=utf-8"
353       }, {
354       rel => "alternate",
355       title => $c->config->{sitename}." projects feeds",
356       href => '?a=opml',
357       type => "text/plain; charset=utf-8"
358       };
359   }
360
361   $c->stash->{favicon} = $c->config->{favicon};
362
363   # </head><body>
364
365   $c->stash(
366     logo_url      => $c->config->{logo_url},
367     logo_label    => $c->config->{logo_label},
368     logo_img      => $c->config->{logo},
369     home_link     => $c->config->{home_link},
370     home_link_str => $c->config->{home_link_str},
371     );
372
373   if(defined $project) {
374     $c->stash(
375       search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
376       search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
377           || $c->req->param('h')  || $c->req->param('hash')
378           || 'HEAD' ),
379       );
380   }
381 }
382
383 # Formally git_footer_html
384 sub footer {
385   my($self, $c) = @_;
386
387   my $feed_class = 'rss_logo';
388
389   my @feeds;
390   my $project = $c->req->param('project')  || $c->req->param('p');
391   if(defined $project) {
392     (my $pstr = $project) =~ s[/?\.git$][];
393     my $descr = $c->model('Git')->project_info($project)->{description};
394     $c->stash->{project_description} = defined $descr
395       ? $descr
396       : '';
397
398     my %href_params = $self->feed_info($c);
399     if (!%href_params) {
400       $feed_class .= ' generic';
401     }
402     $href_params{'-title'} ||= 'log';
403
404     @feeds = [
405       map +{
406         class => $feed_class,
407         title => "$href_params{'-title'} $_ feed",
408         href  => "/?p=$project;a=\L$_",
409         name  => lc $_,
410         }, qw(RSS Atom)
411       ];
412   } else {
413     @feeds = [
414       map {
415         class => $feed_class,
416           title => '',
417           href  => "/?a=$_->[0]",
418           name  => $_->[1],
419         }, [opml=>'OPML'],[project_index=>'TXT'],
420       ];
421   }
422 }
423
424 # XXX This feels wrong here, should probably be refactored.
425 # returns hash to be passed to href to generate gitweb URL
426 # in -title key it returns description of link
427 sub feed_info {
428   my($self, $c) = @_;
429
430   my $format = shift || 'Atom';
431   my %res = (action => lc($format));
432
433   # feed links are possible only for project views
434   return unless $c->req->param('project');
435
436   # some views should link to OPML, or to generic project feed,
437   # or don't have specific feed yet (so they should use generic)
438   return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
439
440   my $branch;
441   my $hash = $c->req->param('h')  || $c->req->param('hash');
442   my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
443
444   # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
445   # from tag links; this also makes possible to detect branch links
446   if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
447     (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
448     $branch = $1;
449   }
450
451   # find log type for feed description (title)
452   my $type = 'log';
453   my $file_name = $c->req->param('f') || $c->req->param('filename');
454   if (defined $file_name) {
455     $type  = "history of $file_name";
456     $type .= "/" if $c->req->param('action') eq 'tree';
457     $type .= " on '$branch'" if (defined $branch);
458   } else {
459     $type = "log of $branch" if (defined $branch);
460   }
461
462   $res{-title} = $type;
463   $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
464   $res{'file_name'} = $file_name;
465
466   return %res;
467 }
468 =head2 end
469
470 Attempt to render a view, if needed.
471
472 =cut
473
474 sub end : ActionClass('RenderView') {}
475
476 =head1 AUTHOR
477
478 Dan Brook,,,
479
480 =head1 LICENSE
481
482 This library is free software. You can redistribute it and/or modify
483 it under the same terms as Perl itself.
484
485 =cut
486
487 __PACKAGE__->meta->make_immutable;