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