Added other recent work + beginnings of commit action work.
[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
e66db0fb 32sub run_gitweb {
86382b95 33 my ( $self, $c ) = @_;
34
e66db0fb 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 }
86382b95 53}
54
55sub index :Path :Args(0) {
d5cc37a4 56 my ( $self, $c ) = @_;
86382b95 57
e66db0fb 58 # Leave actions up to gitweb at this point.
59 return $self->run_gitweb($c)
60 if $c->req->param('a');
61
d5cc37a4 62 my $list = $c->model('Git')->list_projects;
63 unless(@$list) {
86382b95 64 die "No projects found";
65 }
66
d5cc37a4 67 $c->stash(
68 searchtext => $c->req->param('searchtext') || '',
69 projects => $list,
70 action => 'index',
71 );
04d1d917 72}
73
295c9703 74sub blob : Local {
75 my ( $self, $c ) = @_;
76
295c9703 77 $c->stash(
d7c9a32f 78 blob => $c->model('GPP')->get_object($c->req->param('h'))->content,
295c9703 79 action => 'blob',
80 );
81}
82
e516cf80 83sub reflog : Local {
84 my ( $self, $c ) = @_;
85
c5065c66 86 my @log = $c->model('Git')->reflog(
c5065c66 87 '--since=yesterday'
e516cf80 88 );
c5065c66 89
e516cf80 90 $c->stash(
c5065c66 91 log => \@log,
e516cf80 92 action => 'reflog',
93 );
94}
95
1feb3d6b 96sub commit : Local {
d7c9a32f 97 my ( $self, $c ) = @_;
b7aca93a 98
99 $c->stash(
100 commit => $c->model('GPP')->get_object($c->req->param('h')),
101 action => 'commit',
102 );
d7c9a32f 103}
104
04d1d917 105sub auto : Private {
106 my($self, $c) = @_;
107
108 # Yes, this is hideous.
109 $self->header($c);
110 $self->footer($c);
111}
112
c5065c66 113# XXX This could probably be dropped altogether.
04d1d917 114use Gitalist::Util qw(to_utf8);
04d1d917 115# Formally git_header_html
116sub header {
117 my($self, $c) = @_;
118
c5065c66 119 my $title = $c->config->{sitename};
04d1d917 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');
c5065c66 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;
04d1d917 140
141 #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url);
c5065c66 142 $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css';
04d1d917 143
c5065c66 144 $c->stash->{project} = $project;
04d1d917 145 my @links;
c5065c66 146 if($project) {
147 my %href_params = $self->feed_info($c);
148 $href_params{'-title'} ||= 'log';
04d1d917 149
c5065c66 150 foreach my $format qw(RSS Atom) {
151 my $type = lc($format);
04d1d917 152 push @links, {
c5065c66 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"
04d1d917 159 }, {
c5065c66 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"
04d1d917 166 };
c5065c66 167 }
168 } else {
04d1d917 169 push @links, {
c5065c66 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 }
04d1d917 181
c5065c66 182 $c->stash->{favicon} = $c->config->{favicon};
04d1d917 183
c5065c66 184 # </head><body>
86382b95 185
c5065c66 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},
04d1d917 191 home_link_str => $c->config->{home_link_str},
c5065c66 192 );
04d1d917 193
c5065c66 194 if(defined $project) {
04d1d917 195 $c->stash(
295c9703 196 search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
197 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
c5065c66 198 || $c->req->param('h') || $c->req->param('hash')
199 || 'HEAD' ),
200 );
201 }
04d1d917 202}
203
204# Formally git_footer_html
205sub footer {
206 my($self, $c) = @_;
207
c5065c66 208 my $feed_class = 'rss_logo';
04d1d917 209
210 my @feeds;
211 my $project = $c->req->param('project') || $c->req->param('p');
c5065c66 212 if(defined $project) {
d5cc37a4 213 (my $pstr = $project) =~ s[/?\.git$][];
c5065c66 214 my $descr = $c->model('Git')->project_info($project)->{description};
215 $c->stash->{project_description} = defined $descr
216 ? $descr
217 : '';
04d1d917 218
c5065c66 219 my %href_params = $self->feed_info($c);
220 if (!%href_params) {
221 $feed_class .= ' generic';
222 }
223 $href_params{'-title'} ||= 'log';
04d1d917 224
225 @feeds = [
226 map +{
227 class => $feed_class,
228 title => "$href_params{'-title'} $_ feed",
229 href => "/?p=$project;a=\L$_",
230 name => lc $_,
c5065c66 231 }, qw(RSS Atom)
232 ];
233 } else {
04d1d917 234 @feeds = [
235 map {
236 class => $feed_class,
c5065c66 237 title => '',
238 href => "/?a=$_->[0]",
239 name => $_->[1],
240 }, [opml=>'OPML'],[project_index=>'TXT'],
241 ];
242 }
89de6a33 243}
244
04d1d917 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
248sub feed_info {
249 my($self, $c) = @_;
250
c5065c66 251 my $format = shift || 'Atom';
252 my %res = (action => lc($format));
04d1d917 253
c5065c66 254 # feed links are possible only for project views
255 return unless $c->req->param('project');
04d1d917 256
c5065c66 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;
04d1d917 262 my $hash = $c->req->param('h') || $c->req->param('hash');
263 my $hash_base = $c->req->param('hb') || $c->req->param('hashbase');
c5065c66 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';
04d1d917 274 my $file_name = $c->req->param('f') || $c->req->param('filename');
c5065c66 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;
04d1d917 288}
89de6a33 289=head2 end
290
291Attempt to render a view, if needed.
292
293=cut
294
295sub end : ActionClass('RenderView') {}
296
297=head1 AUTHOR
298
299Dan Brook,,,
300
301=head1 LICENSE
302
303This library is free software. You can redistribute it and/or modify
304it under the same terms as Perl itself.
305
306=cut
307
42fe5d11 308__PACKAGE__->meta->make_immutable;