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