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