]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/RepoTree.pm
www_coderepo: /tree/ redirects to /$OID/s/
[public-inbox.git] / lib / PublicInbox / RepoTree.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # cgit-compatible $REPO/tree/[PATH]?h=$tip redirector
5 package PublicInbox::RepoTree;
6 use v5.12;
7 use PublicInbox::ViewDiff qw(uri_escape_path);
8 use PublicInbox::GitAsyncCat;
9 use PublicInbox::WwwStatic qw(r);
10
11 sub tree_30x { # git check_async callback
12         my ($oid, $type, $size, $ctx) = @_;
13         my $wcb = delete $ctx->{-wcb};
14         return $wcb->(r(404)) if $type eq 'missing';
15         my $u = $ctx->{git}->base_url($ctx->{env});
16         my $path = uri_escape_path(delete $ctx->{-path});
17         $u .= "$oid/s/?b=$path";
18         $wcb->([ 302, [ Location => $u, 'Content-Type' => 'text/plain' ],
19                 [ "Redirecting to $u\n" ] ])
20 }
21
22 sub srv_tree {
23         my ($ctx, $path) = @_;
24         return if index($path, '//') >= 0 || index($path, '/') == 0;
25         my $tip = $ctx->{qp}->{h} // 'HEAD';
26         $path =~ s!/\z!!;
27         my $obj = $ctx->{-obj} = "$tip:$path";
28         $ctx->{-path} = $path;
29
30         # "\n" breaks with `git cat-file --batch-check', and there's no
31         # legitimate use of "\n" in filenames anyways.
32         return if index($obj, "\n") >= 0;
33         sub {
34                 $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
35                 if ($ctx->{env}->{'pi-httpd.async'}) {
36                         async_check($ctx, $obj, \&tree_30x, $ctx);
37                 } else {
38                         $ctx->{git}->check_async($obj, \&tree_30x, $ctx);
39                         $ctx->{git}->async_wait_all;
40                 }
41         };
42 }
43
44 1;