Commit | Line | Data |
89de6a33 |
1 | package Gitalist::Controller::Root; |
42fe5d11 |
2 | use Moose; |
3 | use namespace::autoclean; |
89de6a33 |
4 | |
42fe5d11 |
5 | BEGIN { 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 | |
15 | Gitalist::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 |
29 | use IO::Capture::Stdout; |
d3feefcf |
30 | |
1625cd5f |
31 | =head2 run_gitweb |
32 | |
33 | The main shim around C<gitweb.pm>. |
34 | |
35 | =cut |
36 | |
e66db0fb |
37 | sub run_gitweb { |
86382b95 |
38 | my ( $self, $c ) = @_; |
39 | |
e66db0fb |
40 | # XXX A slippery slope to be sure. |
41 | if($c->req->param('a')) { |
42 | my $capture = IO::Capture::Stdout->new(); |
43 | $capture->start(); |
44 | eval { |
45 | my $action = gitweb::main($c); |
46 | $action->(); |
47 | }; |
48 | $capture->stop(); |
49 | |
50 | use Data::Dumper; |
51 | die Dumper($@) |
52 | if $@; |
53 | |
54 | my $output = join '', $capture->read; |
55 | $c->stash->{gitweb_output} = $output; |
56 | $c->stash->{template} = 'gitweb.tt2'; |
57 | } |
86382b95 |
58 | } |
59 | |
9dc3b9a5 |
60 | sub _get_commit { |
61 | my($self, $c) = @_; |
62 | |
0ee97fec |
63 | my $h = $c->req->param('h'); |
64 | my $f = $c->req->param('f'); |
a7cc1ede |
65 | my $m = $c->model('Git'); |
0ee97fec |
66 | |
9dc3b9a5 |
67 | # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD. |
a7cc1ede |
68 | my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h) |
69 | || ($f && $m->hash_by_path($f)) |
70 | || $m->head_hash |
9dc3b9a5 |
71 | # XXX This could definitely use more context. |
72 | || Carp::croak("Couldn't find a hash for the commit object!"); |
73 | |
a7cc1ede |
74 | |
75 | (my $pd = $m->project_dir( $m->project )) =~ s{/\.git$}(); |
76 | my $commit = $m->get_object($hash) |
77 | or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!"); |
9dc3b9a5 |
78 | |
79 | return $commit; |
80 | } |
81 | |
1625cd5f |
82 | =head2 index |
83 | |
84 | Provides the project listing. |
85 | |
86 | =cut |
87 | |
86382b95 |
88 | sub index :Path :Args(0) { |
d5cc37a4 |
89 | my ( $self, $c ) = @_; |
86382b95 |
90 | |
e66db0fb |
91 | # Leave actions up to gitweb at this point. |
92 | return $self->run_gitweb($c) |
93 | if $c->req->param('a'); |
94 | |
d5cc37a4 |
95 | my $list = $c->model('Git')->list_projects; |
96 | unless(@$list) { |
86382b95 |
97 | die "No projects found"; |
98 | } |
99 | |
d5cc37a4 |
100 | $c->stash( |
101 | searchtext => $c->req->param('searchtext') || '', |
102 | projects => $list, |
103 | action => 'index', |
104 | ); |
04d1d917 |
105 | } |
106 | |
790ce598 |
107 | =head2 summary |
108 | |
109 | A summary of what's happening in the repo. |
110 | |
111 | =cut |
112 | |
113 | sub summary : Local { |
114 | my ( $self, $c ) = @_; |
115 | |
9dc3b9a5 |
116 | my $commit = $self->_get_commit($c); |
790ce598 |
117 | $c->stash( |
118 | commit => $commit, |
119 | info => $c->model('Git')->project_info($c->model('Git')->project), |
0ee97fec |
120 | log_lines => [$c->model('Git')->list_revs(sha1 => $commit->sha1, count => 16)], |
121 | refs => $c->model('Git')->references, |
790ce598 |
122 | heads => [$c->model('Git')->heads], |
0ee97fec |
123 | HEAD => $c->model('Git')->head_hash, |
790ce598 |
124 | action => 'summary', |
125 | ); |
126 | } |
127 | |
128 | =head2 heads |
129 | |
130 | The current list of heads (aka branches) in the repo. |
131 | |
132 | =cut |
133 | |
134 | sub heads : Local { |
135 | my ( $self, $c ) = @_; |
136 | |
137 | $c->stash( |
9dc3b9a5 |
138 | commit => $self->_get_commit($c), |
790ce598 |
139 | heads => [$c->model('Git')->heads], |
0ee97fec |
140 | HEAD => $c->model('Git')->head_hash, |
790ce598 |
141 | action => 'heads', |
142 | ); |
143 | } |
144 | |
1625cd5f |
145 | =head2 blob |
146 | |
147 | The blob action i.e the contents of a file. |
148 | |
149 | =cut |
150 | |
295c9703 |
151 | sub blob : Local { |
152 | my ( $self, $c ) = @_; |
153 | |
c8870bd3 |
154 | my $h = $c->req->param('h') |
155 | || $c->model('Git')->hash_by_path($c->req->param('f')) |
156 | || die "No file or sha1 provided."; |
157 | my $hb = $c->req->param('hb') |
158 | || $c->model('Git')->head_hash |
159 | || die "Couldn't discern the corresponding head."; |
160 | |
f5da8e7a |
161 | my $filename = $c->req->param('f') || ''; |
162 | |
295c9703 |
163 | $c->stash( |
c8870bd3 |
164 | blob => $c->model('Git')->get_object($h)->content, |
165 | head => $c->model('Git')->get_object($hb), |
f5da8e7a |
166 | filename => $filename, |
167 | # XXX Hack hack hack, see View::SyntaxHighlight |
168 | language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''), |
c8870bd3 |
169 | action => 'blob', |
295c9703 |
170 | ); |
7e54e579 |
171 | |
c8870bd3 |
172 | $c->forward('View::SyntaxHighlight'); |
295c9703 |
173 | } |
174 | |
1625cd5f |
175 | =head2 commit |
176 | |
47495599 |
177 | Exposes a given commit. |
1625cd5f |
178 | |
179 | =cut |
180 | |
1feb3d6b |
181 | sub commit : Local { |
d7c9a32f |
182 | my ( $self, $c ) = @_; |
b7aca93a |
183 | |
9dc3b9a5 |
184 | my $commit = $self->_get_commit($c); |
b7aca93a |
185 | $c->stash( |
790ce598 |
186 | commit => $commit, |
187 | diff_tree => [$c->model('Git')->diff_tree($commit)], |
188 | branches_on => [$c->model('Git')->refs_for($commit->sha1)], |
189 | action => 'commit', |
b7aca93a |
190 | ); |
d7c9a32f |
191 | } |
192 | |
2247133f |
193 | =head2 commitdiff |
194 | |
195 | Exposes a given diff of a commit. |
196 | |
197 | =cut |
198 | |
199 | sub commitdiff : Local { |
200 | my ( $self, $c ) = @_; |
201 | |
9dc3b9a5 |
202 | my $commit = $self->_get_commit($c); |
f5da8e7a |
203 | my @difflist = $c->model('Git')->diff($commit->parent_sha1, $commit->sha1); |
2247133f |
204 | $c->stash( |
f5da8e7a |
205 | commit => $commit, |
206 | diff_tree => [$c->model('Git')->diff_tree($commit)], |
207 | diff => \@difflist, |
208 | # XXX Hack hack hack, see View::SyntaxHighlight |
209 | blobs => [map $_->{diff}, @difflist], |
210 | language => 'Diff', |
211 | action => 'commitdiff', |
2247133f |
212 | ); |
f5da8e7a |
213 | |
214 | $c->forward('View::SyntaxHighlight'); |
2247133f |
215 | } |
216 | |
47495599 |
217 | =head2 shortlog |
218 | |
219 | Expose an abbreviated log of a given sha1. |
220 | |
221 | =cut |
222 | |
223 | sub shortlog : Local { |
224 | my ( $self, $c ) = @_; |
225 | |
9dc3b9a5 |
226 | my $commit = $self->_get_commit($c); |
790ce598 |
227 | # XXX Needs paging. |
47495599 |
228 | $c->stash( |
790ce598 |
229 | commit => $commit, |
0ee97fec |
230 | log_lines => [$c->model('Git')->list_revs(sha1 => $commit->sha1)], |
231 | refs => $c->model('Git')->references, |
790ce598 |
232 | action => 'shortlog', |
47495599 |
233 | ); |
234 | } |
235 | |
790ce598 |
236 | =head2 log |
237 | |
238 | Calls shortlog internally. Perhaps that should be reversed ... |
239 | |
240 | =cut |
241 | sub log : Local { |
242 | $_[0]->shortlog($_[1]); |
243 | $_[1]->stash->{action} = 'log'; |
244 | } |
245 | |
b3fa97cd |
246 | =head2 tree |
247 | |
248 | The tree of a given commit. |
249 | |
250 | =cut |
251 | |
252 | sub tree : Local { |
253 | my ( $self, $c ) = @_; |
254 | |
9dc3b9a5 |
255 | my $commit = $self->_get_commit($c); |
b3fa97cd |
256 | $c->stash( |
257 | # XXX Useful defaults needed ... |
790ce598 |
258 | commit => $commit, |
259 | tree => $c->model('Git')->get_object($c->req->param('hb')), |
9dc3b9a5 |
260 | tree_list => [$c->model('Git')->list_tree($commit->sha1)], |
790ce598 |
261 | action => 'tree', |
b3fa97cd |
262 | ); |
263 | } |
264 | |
9dc3b9a5 |
265 | =head2 reflog |
266 | |
267 | Expose the local reflog. This may go away. |
268 | |
269 | =cut |
270 | |
271 | sub reflog : Local { |
272 | my ( $self, $c ) = @_; |
273 | |
274 | my @log = $c->model('Git')->reflog( |
275 | '--since=yesterday' |
276 | ); |
277 | |
278 | $c->stash( |
279 | log => \@log, |
280 | action => 'reflog', |
281 | ); |
282 | } |
283 | |
1625cd5f |
284 | =head2 auto |
285 | |
286 | Populate the header and footer. Perhaps not the best location. |
287 | |
288 | =cut |
289 | |
04d1d917 |
290 | sub auto : Private { |
291 | my($self, $c) = @_; |
292 | |
1ef8dc7d |
293 | # XXX Temp hack until a decent solution is invented. |
294 | $c->model('Git')->project($c->req->param('p')) |
295 | if $c->req->param('p'); |
296 | |
04d1d917 |
297 | # Yes, this is hideous. |
298 | $self->header($c); |
299 | $self->footer($c); |
300 | } |
301 | |
c5065c66 |
302 | # XXX This could probably be dropped altogether. |
04d1d917 |
303 | use Gitalist::Util qw(to_utf8); |
04d1d917 |
304 | # Formally git_header_html |
305 | sub header { |
306 | my($self, $c) = @_; |
307 | |
c5065c66 |
308 | my $title = $c->config->{sitename}; |
04d1d917 |
309 | |
310 | my $project = $c->req->param('project') || $c->req->param('p'); |
311 | my $action = $c->req->param('action') || $c->req->param('a'); |
312 | my $file_name = $c->req->param('filename') || $c->req->param('f'); |
c5065c66 |
313 | if(defined $project) { |
314 | $title .= " - " . to_utf8($project); |
315 | if (defined $action) { |
316 | $title .= "/$action"; |
317 | if (defined $file_name) { |
318 | $title .= " - " . $file_name; |
319 | if ($action eq "tree" && $file_name !~ m|/$|) { |
320 | $title .= "/"; |
321 | } |
322 | } |
323 | } |
324 | } |
325 | |
326 | $c->stash->{version} = $c->config->{version}; |
327 | $c->stash->{git_version} = $c->model('Git')->run_cmd('--version'); |
328 | $c->stash->{title} = $title; |
04d1d917 |
329 | |
330 | #$c->stash->{baseurl} = $ENV{PATH_INFO} && uri_escape($base_url); |
c5065c66 |
331 | $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css'; |
04d1d917 |
332 | |
c5065c66 |
333 | $c->stash->{project} = $project; |
04d1d917 |
334 | my @links; |
c5065c66 |
335 | if($project) { |
336 | my %href_params = $self->feed_info($c); |
337 | $href_params{'-title'} ||= 'log'; |
04d1d917 |
338 | |
c5065c66 |
339 | foreach my $format qw(RSS Atom) { |
340 | my $type = lc($format); |
04d1d917 |
341 | push @links, { |
c5065c66 |
342 | rel => 'alternate', |
343 | title => "$project - $href_params{'-title'} - $format feed", |
344 | |
345 | # XXX A bit hacky and could do with using gitweb::href() features |
346 | href => "?a=$type;p=$project", |
347 | type => "application/$type+xml" |
04d1d917 |
348 | }, { |
c5065c66 |
349 | rel => 'alternate', |
350 | |
351 | # XXX This duplication also feels a bit awkward |
352 | title => "$project - $href_params{'-title'} - $format feed (no merges)", |
353 | href => "?a=$type;p=$project;opt=--no-merges", |
354 | type => "application/$type+xml" |
04d1d917 |
355 | }; |
c5065c66 |
356 | } |
357 | } else { |
04d1d917 |
358 | push @links, { |
c5065c66 |
359 | rel => "alternate", |
360 | title => $c->config->{sitename}." projects list", |
361 | href => '?a=project_index', |
362 | type => "text/plain; charset=utf-8" |
363 | }, { |
364 | rel => "alternate", |
365 | title => $c->config->{sitename}." projects feeds", |
366 | href => '?a=opml', |
367 | type => "text/plain; charset=utf-8" |
368 | }; |
369 | } |
04d1d917 |
370 | |
c5065c66 |
371 | $c->stash->{favicon} = $c->config->{favicon}; |
04d1d917 |
372 | |
c5065c66 |
373 | # </head><body> |
86382b95 |
374 | |
c5065c66 |
375 | $c->stash( |
376 | logo_url => $c->config->{logo_url}, |
377 | logo_label => $c->config->{logo_label}, |
378 | logo_img => $c->config->{logo}, |
379 | home_link => $c->config->{home_link}, |
04d1d917 |
380 | home_link_str => $c->config->{home_link_str}, |
c5065c66 |
381 | ); |
04d1d917 |
382 | |
c5065c66 |
383 | if(defined $project) { |
04d1d917 |
384 | $c->stash( |
295c9703 |
385 | search_text => ( $c->req->param('s') || $c->req->param('searchtext') ), |
386 | search_hash => ( $c->req->param('hb') || $c->req->param('hashbase') |
c5065c66 |
387 | || $c->req->param('h') || $c->req->param('hash') |
388 | || 'HEAD' ), |
389 | ); |
390 | } |
04d1d917 |
391 | } |
392 | |
393 | # Formally git_footer_html |
394 | sub footer { |
395 | my($self, $c) = @_; |
396 | |
c5065c66 |
397 | my $feed_class = 'rss_logo'; |
04d1d917 |
398 | |
399 | my @feeds; |
400 | my $project = $c->req->param('project') || $c->req->param('p'); |
c5065c66 |
401 | if(defined $project) { |
d5cc37a4 |
402 | (my $pstr = $project) =~ s[/?\.git$][]; |
c5065c66 |
403 | my $descr = $c->model('Git')->project_info($project)->{description}; |
404 | $c->stash->{project_description} = defined $descr |
405 | ? $descr |
406 | : ''; |
04d1d917 |
407 | |
c5065c66 |
408 | my %href_params = $self->feed_info($c); |
409 | if (!%href_params) { |
410 | $feed_class .= ' generic'; |
411 | } |
412 | $href_params{'-title'} ||= 'log'; |
04d1d917 |
413 | |
414 | @feeds = [ |
415 | map +{ |
416 | class => $feed_class, |
417 | title => "$href_params{'-title'} $_ feed", |
418 | href => "/?p=$project;a=\L$_", |
419 | name => lc $_, |
c5065c66 |
420 | }, qw(RSS Atom) |
421 | ]; |
422 | } else { |
04d1d917 |
423 | @feeds = [ |
424 | map { |
425 | class => $feed_class, |
c5065c66 |
426 | title => '', |
427 | href => "/?a=$_->[0]", |
428 | name => $_->[1], |
429 | }, [opml=>'OPML'],[project_index=>'TXT'], |
430 | ]; |
431 | } |
89de6a33 |
432 | } |
433 | |
04d1d917 |
434 | # XXX This feels wrong here, should probably be refactored. |
435 | # returns hash to be passed to href to generate gitweb URL |
436 | # in -title key it returns description of link |
437 | sub feed_info { |
438 | my($self, $c) = @_; |
439 | |
c5065c66 |
440 | my $format = shift || 'Atom'; |
441 | my %res = (action => lc($format)); |
04d1d917 |
442 | |
c5065c66 |
443 | # feed links are possible only for project views |
444 | return unless $c->req->param('project'); |
04d1d917 |
445 | |
c5065c66 |
446 | # some views should link to OPML, or to generic project feed, |
447 | # or don't have specific feed yet (so they should use generic) |
448 | return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x; |
449 | |
450 | my $branch; |
04d1d917 |
451 | my $hash = $c->req->param('h') || $c->req->param('hash'); |
452 | my $hash_base = $c->req->param('hb') || $c->req->param('hashbase'); |
c5065c66 |
453 | |
454 | # branches refs uses 'refs/heads/' prefix (fullname) to differentiate |
455 | # from tag links; this also makes possible to detect branch links |
456 | if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) || |
457 | (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) { |
458 | $branch = $1; |
459 | } |
460 | |
461 | # find log type for feed description (title) |
462 | my $type = 'log'; |
04d1d917 |
463 | my $file_name = $c->req->param('f') || $c->req->param('filename'); |
c5065c66 |
464 | if (defined $file_name) { |
465 | $type = "history of $file_name"; |
466 | $type .= "/" if $c->req->param('action') eq 'tree'; |
467 | $type .= " on '$branch'" if (defined $branch); |
468 | } else { |
469 | $type = "log of $branch" if (defined $branch); |
470 | } |
471 | |
472 | $res{-title} = $type; |
473 | $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef); |
474 | $res{'file_name'} = $file_name; |
475 | |
476 | return %res; |
04d1d917 |
477 | } |
89de6a33 |
478 | =head2 end |
479 | |
480 | Attempt to render a view, if needed. |
481 | |
482 | =cut |
483 | |
484 | sub end : ActionClass('RenderView') {} |
485 | |
486 | =head1 AUTHOR |
487 | |
488 | Dan Brook,,, |
489 | |
490 | =head1 LICENSE |
491 | |
492 | This library is free software. You can redistribute it and/or modify |
493 | it under the same terms as Perl itself. |
494 | |
495 | =cut |
496 | |
42fe5d11 |
497 | __PACKAGE__->meta->make_immutable; |