Added very basic blob action.
[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   my $git      = $c->model('Git');
78   my $req      = $c->req;
79   my $filename = $req->param('f')  || $req->param('filename');
80   my $hash     = $req->param('hb') || $req->param('hashbase')
81               || $git->get_head_hash($req->param('p') || $req->param('project'));
82   my $filehash = $git->get_hash_by_path($hash, $filename, 'blob');
83   
84   my $blob = $git->run_cmd('cat-file' => blob => $filehash);
85
86   $c->stash(
87       blob   => encode_entities($blob),
88       action => 'blob',
89   );
90 }
91
92 sub auto : Private {
93     my($self, $c) = @_;
94
95     # Yes, this is hideous.
96     $self->header($c);
97     $self->footer($c);
98 }
99
100 use Gitalist::Util qw(to_utf8);
101 use HTML::Entities qw(encode_entities);
102 use URI::Escape    qw(uri_escape);
103 # Formally git_header_html
104 sub header {
105   my($self, $c) = @_;
106
107         my $title = $c->config->{sitename};
108
109   my $project   = $c->req->param('project')  || $c->req->param('p');
110   my $action    = $c->req->param('action')   || $c->req->param('a');
111   my $file_name = $c->req->param('filename') || $c->req->param('f');
112         if(defined $project) {
113                 $title .= " - " . to_utf8($project);
114                 if (defined $action) {
115                         $title .= "/$action";
116                         if (defined $file_name) {
117                                 $title .= " - " . encode_entities($file_name);
118                                 if ($action eq "tree" && $file_name !~ m|/$|) {
119                                         $title .= "/";
120                                 }
121                         }
122                 }
123         }
124
125         $c->stash->{version}     = $c->config->{version};
126         $c->stash->{git_version} = $c->model('Git')->run_cmd('--version');
127         $c->stash->{title}       = $title;
128
129   #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
130         $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
131
132         $c->stash->{project} = $project;
133   my @links;
134         if($project) {
135                 my %href_params = $self->feed_info($c);
136                 $href_params{'-title'} ||= 'log';
137
138                 foreach my $format qw(RSS Atom) {
139                         my $type = lc($format);
140       push @links, {
141                               rel   => 'alternate',
142                               title => "$project - $href_params{'-title'} - $format feed",
143             # XXX A bit hacky and could do with using gitweb::href() features
144                               href  => "?a=$type;p=$project",
145                               type  => "application/$type+xml"
146         }, {
147                               rel   => 'alternate',
148             # XXX This duplication also feels a bit awkward
149                               title => "$project - $href_params{'-title'} - $format feed (no merges)",
150                               href  => "?a=$type;p=$project;opt=--no-merges",
151                               type  => "application/$type+xml"
152         };
153                 }
154         } else {
155     push @links, {
156         rel => "alternate",
157         title => $c->config->{sitename}." projects list",
158         href => '?a=project_index',
159         type => "text/plain; charset=utf-8"
160     }, {
161         rel => "alternate",
162         title => $c->config->{sitename}." projects feeds",
163         href => '?a=opml',
164         type => "text/plain; charset=utf-8"
165     };
166         }
167
168         $c->stash->{favicon} = $c->config->{favicon};
169
170         # </head><body>
171
172         $c->stash(
173     logo_url      => uri_escape($c->config->{logo_url}),
174           logo_label    => encode_entities($c->config->{logo_label}),
175           logo_img      => $c->config->{logo},
176           home_link     => uri_escape($c->config->{home_link}),
177     home_link_str => $c->config->{home_link_str},
178   );
179
180         if(defined $project) {
181     $c->stash(
182       search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
183       search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
184                    || $c->req->param('h')  || $c->req->param('hash')
185                    || 'HEAD' ),
186     );
187         }
188 }
189
190 # Formally git_footer_html
191 sub footer {
192   my($self, $c) = @_;
193
194         my $feed_class = 'rss_logo';
195
196   my @feeds;
197   my $project = $c->req->param('project')  || $c->req->param('p');
198         if(defined $project) {
199     (my $pstr = $project) =~ s[/?\.git$][];
200                 my $descr = $c->model('Git')->project_info($project)->{description};
201                 $c->stash->{project_description} = defined $descr
202                         ? encode_entities($descr)
203                         : '';
204
205                 my %href_params = $self->feed_info($c);
206                 if (!%href_params) {
207                         $feed_class .= ' generic';
208                 }
209                 $href_params{'-title'} ||= 'log';
210
211     @feeds = [
212       map +{
213         class => $feed_class,
214         title => "$href_params{'-title'} $_ feed",
215         href  => "/?p=$project;a=\L$_",
216         name  => lc $_,
217       }, qw(RSS Atom)
218     ];
219         } else {
220     @feeds = [
221       map {
222         class => $feed_class,
223         title => '',
224         href  => "/?a=$_->[0]",
225         name  => $_->[1],
226       }, [opml=>'OPML'],[project_index=>'TXT'],
227     ];
228         }
229 }
230
231 # XXX This feels wrong here, should probably be refactored.
232 # returns hash to be passed to href to generate gitweb URL
233 # in -title key it returns description of link
234 sub feed_info {
235   my($self, $c) = @_;
236
237         my $format = shift || 'Atom';
238         my %res = (action => lc($format));
239
240         # feed links are possible only for project views
241         return unless $c->req->param('project');
242         # some views should link to OPML, or to generic project feed,
243         # or don't have specific feed yet (so they should use generic)
244         return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x;
245
246         my $branch;
247   my $hash = $c->req->param('h')  || $c->req->param('hash');
248   my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
249         # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
250         # from tag links; this also makes possible to detect branch links
251         if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
252             (defined $hash      && $hash      =~ m!^refs/heads/(.*)$!)) {
253                 $branch = $1;
254         }
255         # find log type for feed description (title)
256         my $type = 'log';
257   my $file_name = $c->req->param('f') || $c->req->param('filename');
258         if (defined $file_name) {
259                 $type  = "history of $file_name";
260                 $type .= "/" if $c->req->param('action') eq 'tree';
261                 $type .= " on '$branch'" if (defined $branch);
262         } else {
263                 $type = "log of $branch" if (defined $branch);
264         }
265
266         $res{-title} = $type;
267         $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
268         $res{'file_name'} = $file_name;
269
270         return %res;
271 }
272 =head2 end
273
274 Attempt to render a view, if needed.
275
276 =cut
277
278 sub end : ActionClass('RenderView') {}
279
280 =head1 AUTHOR
281
282 Dan Brook,,,
283
284 =head1 LICENSE
285
286 This library is free software. You can redistribute it and/or modify
287 it under the same terms as Perl itself.
288
289 =cut
290
291 __PACKAGE__->meta->make_immutable;