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