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