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