Added very basic blob 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;
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
04d1d917 92sub auto : Private {
93 my($self, $c) = @_;
94
95 # Yes, this is hideous.
96 $self->header($c);
97 $self->footer($c);
98}
99
100use Gitalist::Util qw(to_utf8);
101use HTML::Entities qw(encode_entities);
102use URI::Escape qw(uri_escape);
103# Formally git_header_html
104sub 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>
86382b95 171
04d1d917 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(
295c9703 182 search_text => ( $c->req->param('s') || $c->req->param('searchtext') ),
183 search_hash => ( $c->req->param('hb') || $c->req->param('hashbase')
04d1d917 184 || $c->req->param('h') || $c->req->param('hash')
295c9703 185 || 'HEAD' ),
04d1d917 186 );
187 }
188}
189
190# Formally git_footer_html
191sub 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) {
d5cc37a4 199 (my $pstr = $project) =~ s[/?\.git$][];
200 my $descr = $c->model('Git')->project_info($project)->{description};
04d1d917 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 }
89de6a33 229}
230
04d1d917 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
234sub 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}
89de6a33 272=head2 end
273
274Attempt to render a view, if needed.
275
276=cut
277
278sub end : ActionClass('RenderView') {}
279
280=head1 AUTHOR
281
282Dan Brook,,,
283
284=head1 LICENSE
285
286This library is free software. You can redistribute it and/or modify
287it under the same terms as Perl itself.
288
289=cut
290
42fe5d11 291__PACKAGE__->meta->make_immutable;