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 | |
d17ce39c |
13 | use IO::Capture::Stdout; |
14 | use XML::Atom::Feed; |
15 | use XML::Atom::Entry; |
16 | use Sys::Hostname (); |
17 | |
89de6a33 |
18 | =head1 NAME |
19 | |
20 | Gitalist::Controller::Root - Root Controller for Gitalist |
21 | |
22 | =head1 DESCRIPTION |
23 | |
24 | [enter your description here] |
25 | |
26 | =head1 METHODS |
27 | |
28 | =cut |
29 | |
30 | =head2 index |
31 | |
32 | =cut |
33 | |
1625cd5f |
34 | =head2 run_gitweb |
35 | |
f71a83ab |
36 | The C<gitweb> shim. It should now only be explicitly accessible by |
37 | modifying the URL. |
1625cd5f |
38 | |
39 | =cut |
40 | |
e66db0fb |
41 | sub run_gitweb { |
86382b95 |
42 | my ( $self, $c ) = @_; |
43 | |
e66db0fb |
44 | # XXX A slippery slope to be sure. |
45 | if($c->req->param('a')) { |
46 | my $capture = IO::Capture::Stdout->new(); |
47 | $capture->start(); |
48 | eval { |
49 | my $action = gitweb::main($c); |
50 | $action->(); |
51 | }; |
52 | $capture->stop(); |
5a2f0948 |
53 | |
e66db0fb |
54 | use Data::Dumper; |
55 | die Dumper($@) |
56 | if $@; |
5a2f0948 |
57 | |
e66db0fb |
58 | my $output = join '', $capture->read; |
59 | $c->stash->{gitweb_output} = $output; |
60 | $c->stash->{template} = 'gitweb.tt2'; |
61 | } |
86382b95 |
62 | } |
63 | |
832cbc81 |
64 | sub _get_object { |
b4b4d0fd |
65 | my($self, $c, $haveh) = @_; |
9dc3b9a5 |
66 | |
c1f608c8 |
67 | my $h = $haveh || $c->req->param('h') || ''; |
0ee97fec |
68 | my $f = $c->req->param('f'); |
8dbe8024 |
69 | |
1aad4e81 |
70 | my $m = $c->stash->{Project}; |
71 | my $pd = $m->path; |
0ee97fec |
72 | |
9dc3b9a5 |
73 | # Either use the provided h(ash) parameter, the f(ile) parameter or just use HEAD. |
a7cc1ede |
74 | my $hash = ($h =~ /[^a-f0-9]/ ? $m->head_hash($h) : $h) |
75 | || ($f && $m->hash_by_path($f)) |
76 | || $m->head_hash |
9dc3b9a5 |
77 | # XXX This could definitely use more context. |
78 | || Carp::croak("Couldn't find a hash for the commit object!"); |
79 | |
a7cc1ede |
80 | my $commit = $m->get_object($hash) |
81 | or Carp::croak("Couldn't find a commit object for '$hash' in '$pd'!"); |
9dc3b9a5 |
82 | |
83 | return $commit; |
84 | } |
85 | |
1625cd5f |
86 | =head2 index |
87 | |
88 | Provides the project listing. |
89 | |
90 | =cut |
91 | |
86382b95 |
92 | sub index :Path :Args(0) { |
7bc165b3 |
93 | my ( $self, $c ) = @_; |
94 | |
95 | $c->detach($c->req->param('a')) |
96 | if $c->req->param('a'); |
97 | |
98 | my @list = @{ $c->model()->projects }; |
99 | die 'No projects found in '. $c->model->repo_dir |
100 | unless @list; |
101 | |
102 | my $search = $c->req->param('s') || ''; |
103 | if($search) { |
104 | @list = grep { |
105 | index($_->name, $search) > -1 |
106 | or ( $_->description !~ /^Unnamed repository/ and index($_->description, $search) > -1 ) |
107 | } @list |
108 | } |
109 | |
110 | $c->stash( |
111 | search_text => $search, |
112 | projects => \@list, |
113 | action => 'index', |
114 | ); |
04d1d917 |
115 | } |
116 | |
790ce598 |
117 | =head2 summary |
118 | |
119 | A summary of what's happening in the repo. |
120 | |
121 | =cut |
122 | |
123 | sub summary : Local { |
124 | my ( $self, $c ) = @_; |
b47ae2c0 |
125 | my $project = $c->stash->{Project}; |
3bbb1202 |
126 | $c->detach('error_404') unless $project; |
832cbc81 |
127 | my $commit = $self->_get_object($c); |
c65cccc2 |
128 | my @heads = @{$project->heads}; |
06281e11 |
129 | my $maxitems = Gitalist->config->{paging}{summary} || 10; |
790ce598 |
130 | $c->stash( |
131 | commit => $commit, |
4111e151 |
132 | log_lines => [$project->list_revs( |
133 | sha1 => $commit->sha1, |
06281e11 |
134 | count => $maxitems, |
fde5091f |
135 | )], |
4111e151 |
136 | refs => $project->references, |
0969b411 |
137 | heads => [ @heads[0 .. ($#heads < $maxitems ? $#heads : $maxitems)] ], |
790ce598 |
138 | action => 'summary', |
06281e11 |
139 | ); |
790ce598 |
140 | } |
141 | |
142 | =head2 heads |
143 | |
144 | The current list of heads (aka branches) in the repo. |
145 | |
146 | =cut |
147 | |
148 | sub heads : Local { |
149 | my ( $self, $c ) = @_; |
b47ae2c0 |
150 | my $project = $c->stash->{Project}; |
790ce598 |
151 | $c->stash( |
832cbc81 |
152 | commit => $self->_get_object($c), |
c65cccc2 |
153 | heads => $project->heads, |
790ce598 |
154 | action => 'heads', |
155 | ); |
156 | } |
157 | |
1625cd5f |
158 | =head2 blob |
159 | |
160 | The blob action i.e the contents of a file. |
161 | |
162 | =cut |
163 | |
295c9703 |
164 | sub blob : Local { |
165 | my ( $self, $c ) = @_; |
8bb7649b |
166 | my $project = $c->stash->{Project}; |
c8870bd3 |
167 | my $h = $c->req->param('h') |
8bb7649b |
168 | || $project->hash_by_path($c->req->param('hb'), $c->req->param('f')) |
c8870bd3 |
169 | || die "No file or sha1 provided."; |
170 | my $hb = $c->req->param('hb') |
8bb7649b |
171 | || $project->head_hash |
c8870bd3 |
172 | || die "Couldn't discern the corresponding head."; |
173 | |
f5da8e7a |
174 | my $filename = $c->req->param('f') || ''; |
175 | |
295c9703 |
176 | $c->stash( |
77b5d22c |
177 | blob => $project->get_object($h)->content, |
8bb7649b |
178 | head => $project->get_object($hb), |
f5da8e7a |
179 | filename => $filename, |
180 | # XXX Hack hack hack, see View::SyntaxHighlight |
181 | language => ($filename =~ /\.p[lm]$/ ? 'Perl' : ''), |
c8870bd3 |
182 | action => 'blob', |
295c9703 |
183 | ); |
7e54e579 |
184 | |
c8a42dd5 |
185 | $c->forward('View::SyntaxHighlight') |
186 | unless $c->stash->{no_wrapper}; |
187 | } |
188 | |
189 | sub blob_plain : Local { |
190 | my($self, $c) = @_; |
191 | |
192 | $c->stash(no_wrapper => 1); |
193 | $c->response->content_type('text/plain; charset=utf-8'); |
194 | |
195 | $c->forward('blob'); |
196 | } |
197 | |
198 | sub blobdiff_plain : Local { |
199 | my($self, $c) = @_; |
200 | |
201 | $c->stash(no_wrapper => 1); |
202 | $c->response->content_type('text/plain; charset=utf-8'); |
203 | |
204 | $c->forward('blobdiff'); |
205 | |
295c9703 |
206 | } |
207 | |
6cf4366a |
208 | =head2 blobdiff |
209 | |
210 | Exposes a given diff of a blob. |
211 | |
212 | =cut |
213 | |
214 | sub blobdiff : Local { |
215 | my ( $self, $c ) = @_; |
832cbc81 |
216 | my $commit = $self->_get_object($c, $c->req->param('hb')); |
6cf4366a |
217 | my $filename = $c->req->param('f') |
218 | || croak("No file specified!"); |
6cfcd548 |
219 | my($tree, $patch) = $c->stash->{Project}->diff( |
ad8884fc |
220 | commit => $commit, |
ad8884fc |
221 | patch => 1, |
c8a42dd5 |
222 | parent => $c->req->param('hpb') || undef, |
223 | file => $filename, |
6cf4366a |
224 | ); |
225 | $c->stash( |
226 | commit => $commit, |
ad8884fc |
227 | diff => $patch, |
6cf4366a |
228 | # XXX Hack hack hack, see View::SyntaxHighlight |
ad8884fc |
229 | blobs => [$patch->[0]->{diff}], |
6cf4366a |
230 | language => 'Diff', |
231 | action => 'blobdiff', |
232 | ); |
233 | |
c8a42dd5 |
234 | $c->forward('View::SyntaxHighlight') |
235 | unless $c->stash->{no_wrapper}; |
6cf4366a |
236 | } |
237 | |
1625cd5f |
238 | =head2 commit |
239 | |
47495599 |
240 | Exposes a given commit. |
1625cd5f |
241 | |
242 | =cut |
243 | |
1feb3d6b |
244 | sub commit : Local { |
d7c9a32f |
245 | my ( $self, $c ) = @_; |
77edf882 |
246 | my $project = $c->stash->{Project}; |
832cbc81 |
247 | my $commit = $self->_get_object($c); |
b7aca93a |
248 | $c->stash( |
790ce598 |
249 | commit => $commit, |
77edf882 |
250 | diff_tree => ($project->diff(commit => $commit))[0], |
251 | refs => $project->references, |
790ce598 |
252 | action => 'commit', |
b7aca93a |
253 | ); |
d7c9a32f |
254 | } |
255 | |
2247133f |
256 | =head2 commitdiff |
257 | |
258 | Exposes a given diff of a commit. |
259 | |
260 | =cut |
261 | |
262 | sub commitdiff : Local { |
263 | my ( $self, $c ) = @_; |
832cbc81 |
264 | my $commit = $self->_get_object($c); |
fc948aee |
265 | my($tree, $patch) = $c->stash->{Project}->diff( |
ad8884fc |
266 | commit => $commit, |
fc948aee |
267 | parent => $c->req->param('hp') || undef, |
ad8884fc |
268 | patch => 1, |
269 | ); |
2247133f |
270 | $c->stash( |
f5da8e7a |
271 | commit => $commit, |
ad8884fc |
272 | diff_tree => $tree, |
273 | diff => $patch, |
f5da8e7a |
274 | # XXX Hack hack hack, see View::SyntaxHighlight |
ad8884fc |
275 | blobs => [map $_->{diff}, @$patch], |
f5da8e7a |
276 | language => 'Diff', |
277 | action => 'commitdiff', |
2247133f |
278 | ); |
f5da8e7a |
279 | |
c8a42dd5 |
280 | $c->forward('View::SyntaxHighlight') |
281 | unless $c->stash->{no_wrapper}; |
282 | } |
283 | |
284 | sub commitdiff_plain : Local { |
285 | my($self, $c) = @_; |
286 | |
287 | $c->stash(no_wrapper => 1); |
288 | $c->response->content_type('text/plain; charset=utf-8'); |
289 | |
290 | $c->forward('commitdiff'); |
2247133f |
291 | } |
292 | |
47495599 |
293 | =head2 shortlog |
294 | |
295 | Expose an abbreviated log of a given sha1. |
296 | |
297 | =cut |
298 | |
299 | sub shortlog : Local { |
300 | my ( $self, $c ) = @_; |
f740f8a9 |
301 | my $project = $c->stash->{Project}; |
832cbc81 |
302 | my $commit = $self->_get_object($c); |
fde5091f |
303 | my %logargs = ( |
304 | sha1 => $commit->sha1, |
f740f8a9 |
305 | count => Gitalist->config->{paging}{log} || 25, |
63e220b9 |
306 | ($c->req->param('f') ? (file => $c->req->param('f')) : ()) |
307 | ); |
fde5091f |
308 | |
309 | my $page = $c->req->param('pg') || 0; |
310 | $logargs{skip} = $c->req->param('pg') * $logargs{count} |
311 | if $c->req->param('pg'); |
312 | |
47495599 |
313 | $c->stash( |
790ce598 |
314 | commit => $commit, |
f740f8a9 |
315 | log_lines => [$project->list_revs(%logargs)], |
316 | refs => $project->references, |
790ce598 |
317 | action => 'shortlog', |
b4b4d0fd |
318 | page => $page, |
47495599 |
319 | ); |
320 | } |
321 | |
790ce598 |
322 | =head2 log |
323 | |
324 | Calls shortlog internally. Perhaps that should be reversed ... |
325 | |
326 | =cut |
327 | sub log : Local { |
328 | $_[0]->shortlog($_[1]); |
329 | $_[1]->stash->{action} = 'log'; |
330 | } |
331 | |
c8a42dd5 |
332 | # For legacy support. |
333 | sub history : Local { |
334 | $_[0]->shortlog(@_[1 .. $#_]); |
335 | } |
336 | |
b3fa97cd |
337 | =head2 tree |
338 | |
339 | The tree of a given commit. |
340 | |
341 | =cut |
342 | |
343 | sub tree : Local { |
344 | my ( $self, $c ) = @_; |
74af77a3 |
345 | my $project = $c->stash->{Project}; |
832cbc81 |
346 | my $commit = $self->_get_object($c, $c->req->param('hb')); |
347 | my $tree = $self->_get_object($c, $c->req->param('h') || $commit->tree_sha1); |
b3fa97cd |
348 | $c->stash( |
790ce598 |
349 | commit => $commit, |
b4b4d0fd |
350 | tree => $tree, |
74af77a3 |
351 | tree_list => [$project->list_tree($tree->sha1)], |
832cbc81 |
352 | path => $c->req->param('f') || '', |
790ce598 |
353 | action => 'tree', |
b3fa97cd |
354 | ); |
355 | } |
356 | |
9dc3b9a5 |
357 | =head2 reflog |
358 | |
359 | Expose the local reflog. This may go away. |
360 | |
361 | =cut |
362 | |
363 | sub reflog : Local { |
364 | my ( $self, $c ) = @_; |
d8abdf1c |
365 | my @log = $c->stash->{Project}->reflog( |
9dc3b9a5 |
366 | '--since=yesterday' |
367 | ); |
368 | |
369 | $c->stash( |
370 | log => \@log, |
371 | action => 'reflog', |
372 | ); |
373 | } |
374 | |
14664e1c |
375 | sub search : Local { |
4df2f62f |
376 | my($self, $c) = @_; |
d8abdf1c |
377 | $c->stash(current_action => 'GitRepos'); |
378 | my $project = $c->stash->{Project}; |
832cbc81 |
379 | my $commit = $self->_get_object($c); |
4df2f62f |
380 | # Lifted from /shortlog. |
381 | my %logargs = ( |
382 | sha1 => $commit->sha1, |
383 | count => Gitalist->config->{paging}{log}, |
384 | ($c->req->param('f') ? (file => $c->req->param('f')) : ()), |
72d72d4d |
385 | search => { |
386 | type => $c->req->param('type'), |
387 | text => $c->req->param('text'), |
388 | regexp => $c->req->param('regexp') || 0, |
389 | }, |
4df2f62f |
390 | ); |
391 | |
392 | $c->stash( |
393 | commit => $commit, |
d8abdf1c |
394 | results => [$project->list_revs(%logargs)], |
4df2f62f |
395 | action => 'search', |
396 | # This could be added - page => $page, |
397 | ); |
14664e1c |
398 | } |
399 | |
400 | sub search_help : Local { |
2646511e |
401 | my ($self, $c) = @_; |
402 | $c->stash(template => 'search_help.tt2'); |
6cfcd548 |
403 | } |
404 | |
405 | sub atom : Local { |
d17ce39c |
406 | my($self, $c) = @_; |
6cfcd548 |
407 | |
d17ce39c |
408 | my $feed = XML::Atom::Feed->new; |
6cfcd548 |
409 | |
d17ce39c |
410 | my $host = lc Sys::Hostname::hostname(); |
411 | $feed->title($host . ' - ' . Gitalist->config->{name}); |
412 | $feed->updated(~~DateTime->now); |
413 | |
414 | my $project = $c->stash->{Project}; |
415 | my %logargs = ( |
416 | sha1 => $project->head_hash, |
417 | count => Gitalist->config->{paging}{log} || 25, |
418 | ($c->req->param('f') ? (file => $c->req->param('f')) : ()) |
419 | ); |
420 | |
421 | my $mk_title = $c->stash->{short_cmt}; |
422 | for my $commit ($project->list_revs(%logargs)) { |
423 | my $entry = XML::Atom::Entry->new; |
424 | $entry->title( $mk_title->($commit->comment) ); |
425 | $entry->id($c->uri_for('commit', {h=>$commit->sha1})); |
426 | # XXX Needs work ... |
427 | $entry->content($commit->comment); |
428 | $feed->add_entry($entry); |
429 | } |
430 | |
431 | $c->stash( |
72d72d4d |
432 | feed => $feed->as_xml, |
d17ce39c |
433 | no_wrapper => 1, |
434 | ); |
72d72d4d |
435 | $c->response->content_type('application/atom+xml') |
6cfcd548 |
436 | } |
437 | |
6cfcd548 |
438 | sub rss : Local { |
439 | # FIXME - implement rss |
14664e1c |
440 | Carp::croak "Not implemented."; |
441 | } |
442 | |
6cfcd548 |
443 | sub patch : Local { |
377bf360 |
444 | my ($self, $c) = @_; |
61ba8635 |
445 | $c->detach('patches', [1]); |
446 | } |
447 | |
448 | sub patches : Local { |
449 | my ($self, $c, $count) = @_; |
450 | $count ||= Gitalist->config->{patches}{max}; |
377bf360 |
451 | my $commit = $self->_get_object($c); |
452 | my $parent = $c->req->param('hp') || undef; |
f707d264 |
453 | my $patch = $commit->get_patch( $parent, $count ); |
377bf360 |
454 | $c->response->body($patch); |
455 | $c->response->content_type('text/plain'); |
456 | $c->response->status(200); |
6cfcd548 |
457 | } |
458 | |
459 | sub snapshot : Local { |
30db8f5b |
460 | my ($self, $c) = @_; |
63afe2db |
461 | my $format = $c->req->param('sf') || 'tgz'; |
30db8f5b |
462 | die unless $format; |
2e79039a |
463 | my $sha1 = $c->req->param('h') || $self->_get_object($c)->sha1; |
30db8f5b |
464 | $c->response->status(200); |
465 | $c->response->headers->header( 'Content-Disposition' => |
466 | 'attachment; filename=export.tgz'); |
2e79039a |
467 | $c->response->body( |
468 | $c->stash->{Project}->snapshot( |
469 | sha1 => $sha1, |
470 | format => $format |
471 | ) |
472 | ); |
6cfcd548 |
473 | } |
474 | |
1625cd5f |
475 | =head2 auto |
476 | |
477 | Populate the header and footer. Perhaps not the best location. |
478 | |
479 | =cut |
480 | |
04d1d917 |
481 | sub auto : Private { |
4621ecf0 |
482 | my($self, $c) = @_; |
04d1d917 |
483 | |
4621ecf0 |
484 | # XXX Move these to a plugin! |
485 | $c->stash( |
486 | time_since => sub { |
ef11b09e |
487 | return 'never' unless $_[0]; |
4621ecf0 |
488 | return age_string(time - $_[0]->epoch); |
489 | }, |
490 | short_cmt => sub { |
491 | my $cmt = shift; |
492 | my($line) = split /\n/, $cmt; |
493 | $line =~ s/^(.{70,80}\b).*/$1 …/; |
494 | return $line; |
495 | }, |
5cd9b9f9 |
496 | abridged_description => sub { |
497 | join(' ', grep { defined } (split / /, shift)[0..10]); |
498 | }, |
4621ecf0 |
499 | ); |
500 | |
501 | # Yes, this is hideous. |
502 | $self->header($c); |
503 | $self->footer($c); |
04d1d917 |
504 | } |
505 | |
c5065c66 |
506 | # XXX This could probably be dropped altogether. |
04d1d917 |
507 | use Gitalist::Util qw(to_utf8); |
04d1d917 |
508 | # Formally git_header_html |
509 | sub header { |
510 | my($self, $c) = @_; |
511 | |
c5065c66 |
512 | my $title = $c->config->{sitename}; |
04d1d917 |
513 | |
514 | my $project = $c->req->param('project') || $c->req->param('p'); |
515 | my $action = $c->req->param('action') || $c->req->param('a'); |
516 | my $file_name = $c->req->param('filename') || $c->req->param('f'); |
c5065c66 |
517 | if(defined $project) { |
518 | $title .= " - " . to_utf8($project); |
519 | if (defined $action) { |
520 | $title .= "/$action"; |
521 | if (defined $file_name) { |
522 | $title .= " - " . $file_name; |
523 | if ($action eq "tree" && $file_name !~ m|/$|) { |
524 | $title .= "/"; |
525 | } |
526 | } |
527 | } |
528 | } |
529 | |
1aad4e81 |
530 | $c->stash->{version} = $Gitalist::VERSION; |
a54e6cb0 |
531 | # check git's version by running it on the first project in the list. |
c5065c66 |
532 | $c->stash->{title} = $title; |
04d1d917 |
533 | |
c5065c66 |
534 | $c->stash->{stylesheet} = $c->config->{stylesheet} || 'gitweb.css'; |
04d1d917 |
535 | |
c5065c66 |
536 | $c->stash->{project} = $project; |
04d1d917 |
537 | my @links; |
c5065c66 |
538 | if($project) { |
539 | my %href_params = $self->feed_info($c); |
540 | $href_params{'-title'} ||= 'log'; |
04d1d917 |
541 | |
c5065c66 |
542 | foreach my $format qw(RSS Atom) { |
543 | my $type = lc($format); |
04d1d917 |
544 | push @links, { |
c5065c66 |
545 | rel => 'alternate', |
546 | title => "$project - $href_params{'-title'} - $format feed", |
547 | |
548 | # XXX A bit hacky and could do with using gitweb::href() features |
549 | href => "?a=$type;p=$project", |
550 | type => "application/$type+xml" |
04d1d917 |
551 | }, { |
c5065c66 |
552 | rel => 'alternate', |
553 | |
554 | # XXX This duplication also feels a bit awkward |
555 | title => "$project - $href_params{'-title'} - $format feed (no merges)", |
556 | href => "?a=$type;p=$project;opt=--no-merges", |
557 | type => "application/$type+xml" |
04d1d917 |
558 | }; |
c5065c66 |
559 | } |
560 | } else { |
04d1d917 |
561 | push @links, { |
c5065c66 |
562 | rel => "alternate", |
563 | title => $c->config->{sitename}." projects list", |
564 | href => '?a=project_index', |
565 | type => "text/plain; charset=utf-8" |
566 | }, { |
567 | rel => "alternate", |
568 | title => $c->config->{sitename}." projects feeds", |
569 | href => '?a=opml', |
570 | type => "text/plain; charset=utf-8" |
571 | }; |
572 | } |
04d1d917 |
573 | |
c5065c66 |
574 | $c->stash->{favicon} = $c->config->{favicon}; |
04d1d917 |
575 | |
c5065c66 |
576 | # </head><body> |
86382b95 |
577 | |
c5065c66 |
578 | $c->stash( |
579 | logo_url => $c->config->{logo_url}, |
580 | logo_label => $c->config->{logo_label}, |
581 | logo_img => $c->config->{logo}, |
582 | home_link => $c->config->{home_link}, |
04d1d917 |
583 | home_link_str => $c->config->{home_link_str}, |
c5065c66 |
584 | ); |
04d1d917 |
585 | |
3bbb1202 |
586 | if (defined $project) { |
587 | eval { |
588 | $c->stash(Project => $c->model('GitRepos')->project($project)); |
589 | }; |
590 | if ($@) { |
591 | $c->detach('error_404'); |
592 | } |
b47ae2c0 |
593 | $c->stash( |
594 | search_text => ( $c->req->param('s') || |
595 | $c->req->param('searchtext') || ''), |
596 | search_hash => ( $c->req->param('hb') || $c->req->param('hashbase') |
597 | || $c->req->param('h') || $c->req->param('hash') |
598 | || 'HEAD' ), |
c5065c66 |
599 | ); |
600 | } |
6bb7c4dd |
601 | my $a_project = $c->stash->{Project} || $c->model()->projects->[0]; |
602 | $c->stash->{git_version} = $a_project->run_cmd('--version'); |
04d1d917 |
603 | } |
604 | |
605 | # Formally git_footer_html |
606 | sub footer { |
607 | my($self, $c) = @_; |
608 | |
c5065c66 |
609 | my $feed_class = 'rss_logo'; |
04d1d917 |
610 | |
611 | my @feeds; |
612 | my $project = $c->req->param('project') || $c->req->param('p'); |
c5065c66 |
613 | if(defined $project) { |
d5cc37a4 |
614 | (my $pstr = $project) =~ s[/?\.git$][]; |
c66193fb |
615 | my $descr = $c->stash->{project_description} |
616 | = $c->stash->{Project} ? $c->stash->{Project}->description : ''; |
04d1d917 |
617 | |
c5065c66 |
618 | my %href_params = $self->feed_info($c); |
619 | if (!%href_params) { |
620 | $feed_class .= ' generic'; |
621 | } |
622 | $href_params{'-title'} ||= 'log'; |
04d1d917 |
623 | |
624 | @feeds = [ |
625 | map +{ |
626 | class => $feed_class, |
627 | title => "$href_params{'-title'} $_ feed", |
628 | href => "/?p=$project;a=\L$_", |
629 | name => lc $_, |
c5065c66 |
630 | }, qw(RSS Atom) |
631 | ]; |
632 | } else { |
04d1d917 |
633 | @feeds = [ |
634 | map { |
635 | class => $feed_class, |
c5065c66 |
636 | title => '', |
637 | href => "/?a=$_->[0]", |
638 | name => $_->[1], |
639 | }, [opml=>'OPML'],[project_index=>'TXT'], |
640 | ]; |
641 | } |
89de6a33 |
642 | } |
643 | |
04d1d917 |
644 | # XXX This feels wrong here, should probably be refactored. |
645 | # returns hash to be passed to href to generate gitweb URL |
646 | # in -title key it returns description of link |
647 | sub feed_info { |
648 | my($self, $c) = @_; |
649 | |
c5065c66 |
650 | my $format = shift || 'Atom'; |
651 | my %res = (action => lc($format)); |
04d1d917 |
652 | |
c5065c66 |
653 | # feed links are possible only for project views |
654 | return unless $c->req->param('project'); |
04d1d917 |
655 | |
c5065c66 |
656 | # some views should link to OPML, or to generic project feed, |
657 | # or don't have specific feed yet (so they should use generic) |
658 | return if $c->req->param('action') =~ /^(?:tags|heads|forks|tag|search)$/x; |
659 | |
660 | my $branch; |
04d1d917 |
661 | my $hash = $c->req->param('h') || $c->req->param('hash'); |
662 | my $hash_base = $c->req->param('hb') || $c->req->param('hashbase'); |
c5065c66 |
663 | |
664 | # branches refs uses 'refs/heads/' prefix (fullname) to differentiate |
665 | # from tag links; this also makes possible to detect branch links |
666 | if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) || |
667 | (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) { |
668 | $branch = $1; |
669 | } |
670 | |
671 | # find log type for feed description (title) |
672 | my $type = 'log'; |
04d1d917 |
673 | my $file_name = $c->req->param('f') || $c->req->param('filename'); |
c5065c66 |
674 | if (defined $file_name) { |
675 | $type = "history of $file_name"; |
676 | $type .= "/" if $c->req->param('action') eq 'tree'; |
677 | $type .= " on '$branch'" if (defined $branch); |
678 | } else { |
679 | $type = "log of $branch" if (defined $branch); |
680 | } |
681 | |
682 | $res{-title} = $type; |
683 | $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef); |
684 | $res{'file_name'} = $file_name; |
685 | |
686 | return %res; |
04d1d917 |
687 | } |
d9a9b56b |
688 | |
89de6a33 |
689 | =head2 end |
690 | |
691 | Attempt to render a view, if needed. |
692 | |
693 | =cut |
694 | |
fde5091f |
695 | sub end : ActionClass('RenderView') { |
1aad4e81 |
696 | my ($self, $c) = @_; |
697 | # Give project views the current HEAD. |
698 | if ($c->stash->{Project}) { |
699 | $c->stash->{HEAD} = $c->stash->{Project}->head_hash; |
700 | } |
1fd8159c |
701 | } |
d9a9b56b |
702 | |
4111e151 |
703 | sub error_404 :Private { |
704 | my ($self, $c) = @_; |
705 | $c->response->status(404); |
706 | $c->stash( |
707 | title => 'Page not found', |
708 | content => 'Page not found', |
1aad4e81 |
709 | ); |
4111e151 |
710 | } |
711 | |
1fd8159c |
712 | sub age_string { |
713 | my $age = shift; |
714 | my $age_str; |
715 | |
716 | if ($age > 60*60*24*365*2) { |
717 | $age_str = (int $age/60/60/24/365); |
718 | $age_str .= " years ago"; |
719 | } elsif ($age > 60*60*24*(365/12)*2) { |
720 | $age_str = int $age/60/60/24/(365/12); |
721 | $age_str .= " months ago"; |
722 | } elsif ($age > 60*60*24*7*2) { |
723 | $age_str = int $age/60/60/24/7; |
724 | $age_str .= " weeks ago"; |
725 | } elsif ($age > 60*60*24*2) { |
726 | $age_str = int $age/60/60/24; |
727 | $age_str .= " days ago"; |
728 | } elsif ($age > 60*60*2) { |
729 | $age_str = int $age/60/60; |
730 | $age_str .= " hours ago"; |
731 | } elsif ($age > 60*2) { |
732 | $age_str = int $age/60; |
733 | $age_str .= " min ago"; |
734 | } elsif ($age > 2) { |
735 | $age_str = int $age; |
736 | $age_str .= " sec ago"; |
737 | } else { |
738 | $age_str .= " right now"; |
739 | } |
740 | return $age_str; |
7a88ffa4 |
741 | } |
742 | |
89de6a33 |
743 | =head1 AUTHOR |
744 | |
f71a83ab |
745 | Dan Brook |
89de6a33 |
746 | |
747 | =head1 LICENSE |
748 | |
749 | This library is free software. You can redistribute it and/or modify |
750 | it under the same terms as Perl itself. |
751 | |
752 | =cut |
753 | |
42fe5d11 |
754 | __PACKAGE__->meta->make_immutable; |