1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # cgit-compatible $REPO/tree/[PATH]?h=$tip redirector
5 package PublicInbox::RepoTree;
7 use PublicInbox::ViewDiff qw(uri_escape_path);
8 use PublicInbox::GitAsyncCat;
9 use PublicInbox::WwwStatic qw(r);
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" ] ])
23 my ($ctx, $path) = @_;
24 return if index($path, '//') >= 0 || index($path, '/') == 0;
25 my $tip = $ctx->{qp}->{h} // 'HEAD';
27 my $obj = $ctx->{-obj} = "$tip:$path";
28 $ctx->{-path} = $path;
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;
34 $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
35 if ($ctx->{env}->{'pi-httpd.async'}) {
36 async_check($ctx, $obj, \&tree_30x, $ctx);
38 $ctx->{git}->check_async($obj, \&tree_30x, $ctx);
39 $ctx->{git}->async_wait_all;