Brought back recent Moosey goodness so all should be working again.
[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 use File::Slurp qw(slurp);
31
32 sub run_gitweb {
33   my ( $self, $c ) = @_;
34
35   # XXX A slippery slope to be sure.
36   if($c->req->param('a')) {
37     my $capture = IO::Capture::Stdout->new();
38     $capture->start();
39     eval {
40       my $action = gitweb::main($c);
41       $action->();
42     };
43     $capture->stop();
44     
45     use Data::Dumper;
46     die Dumper($@)
47       if $@;
48   
49     my $output = join '', $capture->read;
50     $c->stash->{gitweb_output} = $output;
51     $c->stash->{template} = 'gitweb.tt2';
52   }
53 }
54
55 sub index :Path :Args(0) {
56   my ( $self, $c ) = @_;
57
58   # Leave actions up to gitweb at this point.
59   return $self->run_gitweb($c)
60     if $c->req->param('a');
61
62   my $list = $c->model('Git')->list_projects;
63   unless(@$list) {
64     die "No projects found";
65   }
66
67   $c->stash(
68     searchtext => $c->req->param('searchtext') || '',
69     projects   => $list,
70     action     => 'index',
71   );
72 }
73
74 sub blob : Local {
75   my ( $self, $c ) = @_;
76
77   $c->stash(
78       blob   => $c->model('GPP')->get_object($c->req->param('h'))->content,
79       action => 'blob',
80   );
81 }
82
83 sub reflog : Local {
84   my ( $self, $c ) = @_;
85
86   my @log = $c->model('Git')->reflog(
87       '--since=yesterday'
88   );
89
90   $c->stash(
91       log    => \@log,
92       action => 'reflog',
93   );
94 }
95
96 sub commit : Local {
97   my ( $self, $c ) = @_;
98
99   $c->stash(
100       commit => $c->model('GPP')->get_object($c->req->param('h')),
101       action => 'commit',
102   );
103 }
104
105 sub auto : Private {
106     my($self, $c) = @_;
107
108     # Yes, this is hideous.
109     $self->header($c);
110     $self->footer($c);
111 }
112
113 # XXX This could probably be dropped altogether.
114 use Gitalist::Util qw(to_utf8);
115 # Formally git_header_html
116 sub header {
117   my($self, $c) = @_;
118
119   my $title = $c->config->{sitename};
120
121   my $project   = $c->req->param('project')  || $c->req->param('p');
122   my $action    = $c->req->param('action')   || $c->req->param('a');
123   my $file_name = $c->req->param('filename') || $c->req->param('f');
124   if(defined $project) {
125     $title .= " - " . to_utf8($project);
126     if (defined $action) {
127       $title .= "/$action";
128       if (defined $file_name) {
129         $title .= " - " . $file_name;
130         if ($action eq "tree" && $file_name !~ m|/$|) {
131           $title .= "/";
132         }
133       }
134     }
135   }
136
137   $c->stash->{version}     = $c->config->{version};
138   $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
139   $c->stash->{title}       = $title;
140
141   #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
142   $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
143
144   $c->stash->{project} = $project;
145   my @links;
146   if($project) {
147     my %href_params = $self->feed_info($c);
148     $href_params{'-title'} ||= 'log';
149
150     foreach my $format qw(RSS Atom) {
151       my $type = lc($format);
152       push @links, {
153         rel   => 'alternate',
154         title => "$project - $href_params{'-title'} - $format feed",
155
156         # XXX A bit hacky and could do with using gitweb::href() features
157         href  => "?a=$type;p=$project",
158         type  => "application/$type+xml"
159         }, {
160         rel   => 'alternate',
161
162         # XXX This duplication also feels a bit awkward
163         title => "$project - $href_params{'-title'} - $format feed (no merges)",
164         href  => "?a=$type;p=$project;opt=--no-merges",
165         type  => "application/$type+xml"
166         };
167     }
168   } else {
169     push @links, {
170       rel => "alternate",
171       title => $c->config->{sitename}." projects list",
172       href => '?a=project_index',
173       type => "text/plain; charset=utf-8"
174       }, {
175       rel => "alternate",
176       title => $c->config->{sitename}." projects feeds",
177       href => '?a=opml',
178       type => "text/plain; charset=utf-8"
179       };
180   }
181
182   $c->stash->{favicon} = $c->config->{favicon};
183
184   # </head><body>
185
186   $c->stash(
187     logo_url      => $c->config->{logo_url},
188     logo_label    => $c->config->{logo_label},
189     logo_img      => $c->config->{logo},
190     home_link     => $c->config->{home_link},
191     home_link_str => $c->config->{home_link_str},
192     );
193
194   if(defined $project) {
195     $c->stash(
196       search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
197       search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
198           || $c->req->param('h')  || $c->req->param('hash')
199           || 'HEAD' ),
200       );
201   }
202 }
203
204 # Formally git_footer_html
205 sub footer {
206   my($self, $c) = @_;
207
208   my $feed_class = 'rss_logo';
209
210   my @feeds;
211   my $project = $c->req->param('project')  || $c->req->param('p');
212   if(defined $project) {
213     (my $pstr = $project) =~ s[/?\.git$][];
214     my $descr = $c->model('Git')->project_info($project)->{description};
215     $c->stash->{project_description} = defined $descr
216       ? $descr
217       : '';
218
219     my %href_params = $self->feed_info($c);
220     if (!%href_params) {
221       $feed_class .= ' generic';
222     }
223     $href_params{'-title'} ||= 'log';
224
225     @feeds = [
226       map +{
227         class => $feed_class,
228         title => "$href_params{'-title'} $_ feed",
229         href  => "/?p=$project;a=\L$_",
230         name  => lc $_,
231         }, qw(RSS Atom)
232       ];
233   } else {
234     @feeds = [
235       map {
236         class => $feed_class,
237           title => '',
238           href  => "/?a=$_->[0]",
239           name  => $_->[1],
240         }, [opml=>'OPML'],[project_index=>'TXT'],
241       ];
242   }
243 }
244
245 # XXX This feels wrong here, should probably be refactored.
246 # returns hash to be passed to href to generate gitweb URL
247 # in -title key it returns description of link
248 sub feed_info {
249   my($self, $c) = @_;
250
251   my $format = shift || 'Atom';
252   my %res = (action => lc($format));
253
254   # feed links are possible only for project views
255   return unless $c->req->param('project');
256
257   # some views should link to OPML, or to generic project feed,
258   # or don't have specific feed yet (so they should use generic)
259   return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
260
261   my $branch;
262   my $hash = $c->req->param('h')  || $c->req->param('hash');
263   my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
264
265   # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
266   # from tag links; this also makes possible to detect branch links
267   if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
268     (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
269     $branch = $1;
270   }
271
272   # find log type for feed description (title)
273   my $type = 'log';
274   my $file_name = $c->req->param('f') || $c->req->param('filename');
275   if (defined $file_name) {
276     $type  = "history of $file_name";
277     $type .= "/" if $c->req->param('action') eq 'tree';
278     $type .= " on '$branch'" if (defined $branch);
279   } else {
280     $type = "log of $branch" if (defined $branch);
281   }
282
283   $res{-title} = $type;
284   $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
285   $res{'file_name'} = $file_name;
286
287   return %res;
288 }
289 =head2 end
290
291 Attempt to render a view, if needed.
292
293 =cut
294
295 sub end : ActionClass('RenderView') {}
296
297 =head1 AUTHOR
298
299 Dan Brook,,,
300
301 =head1 LICENSE
302
303 This library is free software. You can redistribute it and/or modify
304 it under the same terms as Perl itself.
305
306 =cut
307
308 __PACKAGE__->meta->make_immutable;