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