]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www_coderepo: /tree/ redirects to /$OID/s/
authorEric Wong <e@80x24.org>
Thu, 12 Jan 2023 14:14:34 +0000 (14:14 +0000)
committerEric Wong <e@80x24.org>
Fri, 13 Jan 2023 03:56:14 +0000 (03:56 +0000)
This is for compatibility with cgit to ease migration.

MANIFEST
lib/PublicInbox/GitAsyncCat.pm
lib/PublicInbox/RepoTree.pm [new file with mode: 0644]
lib/PublicInbox/WwwCoderepo.pm
t/solver_git.t

index 3626e4d23461b5a32a410ad86c46830b827593ed..c494d6f7a148d4c26c74da241fb25bb5447d93b5 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -311,6 +311,7 @@ lib/PublicInbox/Qspawn.pm
 lib/PublicInbox/Reply.pm
 lib/PublicInbox/RepoAtom.pm
 lib/PublicInbox/RepoSnapshot.pm
+lib/PublicInbox/RepoTree.pm
 lib/PublicInbox/SaPlugin/ListMirror.pm
 lib/PublicInbox/SaPlugin/ListMirror.pod
 lib/PublicInbox/Search.pm
index 2e0725a61b2248d0124c3a75e38befd97c76f55e..6dda7340ad72c9336a6643b650ec749fdd9fd0a8 100644 (file)
@@ -75,7 +75,7 @@ sub ibx_async_cat ($$$$) {
 }
 
 sub async_check ($$$$) {
-       my ($ibx, $oidish, $cb, $arg) = @_;
+       my ($ibx, $oidish, $cb, $arg) = @_; # $ibx may be $ctx
        my $git = $ibx->{git} // $ibx->git;
        $git->check_async($oidish, $cb, $arg);
        $git->{async_chk} //= do {
diff --git a/lib/PublicInbox/RepoTree.pm b/lib/PublicInbox/RepoTree.pm
new file mode 100644 (file)
index 0000000..7f2ff20
--- /dev/null
@@ -0,0 +1,44 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+#
+# cgit-compatible $REPO/tree/[PATH]?h=$tip redirector
+package PublicInbox::RepoTree;
+use v5.12;
+use PublicInbox::ViewDiff qw(uri_escape_path);
+use PublicInbox::GitAsyncCat;
+use PublicInbox::WwwStatic qw(r);
+
+sub tree_30x { # git check_async callback
+       my ($oid, $type, $size, $ctx) = @_;
+       my $wcb = delete $ctx->{-wcb};
+       return $wcb->(r(404)) if $type eq 'missing';
+       my $u = $ctx->{git}->base_url($ctx->{env});
+       my $path = uri_escape_path(delete $ctx->{-path});
+       $u .= "$oid/s/?b=$path";
+       $wcb->([ 302, [ Location => $u, 'Content-Type' => 'text/plain' ],
+               [ "Redirecting to $u\n" ] ])
+}
+
+sub srv_tree {
+       my ($ctx, $path) = @_;
+       return if index($path, '//') >= 0 || index($path, '/') == 0;
+       my $tip = $ctx->{qp}->{h} // 'HEAD';
+       $path =~ s!/\z!!;
+       my $obj = $ctx->{-obj} = "$tip:$path";
+       $ctx->{-path} = $path;
+
+       # "\n" breaks with `git cat-file --batch-check', and there's no
+       # legitimate use of "\n" in filenames anyways.
+       return if index($obj, "\n") >= 0;
+       sub {
+               $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
+               if ($ctx->{env}->{'pi-httpd.async'}) {
+                       async_check($ctx, $obj, \&tree_30x, $ctx);
+               } else {
+                       $ctx->{git}->check_async($obj, \&tree_30x, $ctx);
+                       $ctx->{git}->async_wait_all;
+               }
+       };
+}
+
+1;
index 2fba0cd0be4571152b1e1f4fe49c068f2a22b499..668b639881a06a9be1a9fcb253e0dc1a03944496 100644 (file)
@@ -18,6 +18,7 @@ use PublicInbox::Hval qw(ascii_html);
 use PublicInbox::ViewDiff qw(uri_escape_path);
 use PublicInbox::RepoSnapshot;
 use PublicInbox::RepoAtom;
+use PublicInbox::RepoTree;
 
 my $EACH_REF = "git for-each-ref --sort=-creatordate --format='%(HEAD)%00".
        join('%00', map { "%($_)" }
@@ -226,6 +227,11 @@ sub srv { # endpoint called by PublicInbox::WWW
                        ($ctx->{git} = $cr->{$1}) and
                return PublicInbox::ViewVCS::show($ctx, $2);
 
+       if ($path_info =~ m!\A/(.+?)/tree/(.*)\z! and
+                       ($ctx->{git} = $cr->{$1})) {
+               return PublicInbox::RepoTree::srv_tree($ctx, $2) // r(404);
+       }
+
        # snapshots:
        if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
                        ($ctx->{git} = $cr->{$1})) {
index 89ed03626673c459661427e88b108299ae9580df..5519fa1817e1d2971d9e34d0b8cdf2ebb41ff2ee 100644 (file)
@@ -381,6 +381,13 @@ EOF
                        $res = $cb->(GET('/public-inbox/atom/README.md'));
                        is($res->code, 404, '404 on non-existent file Atom feed');
                }
+
+               $res = $cb->(GET('/public-inbox/tree/'));
+               is($res->code, 302, 'got redirect');
+               $res = $cb->(GET('/public-inbox/tree/README'));
+               is($res->code, 302, 'got redirect for regular file');
+               $res = $cb->(GET('/public-inbox/tree/Documentation'));
+               is($res->code, 302, 'got redirect for directory');
        };
        test_psgi(sub { $www->call(@_) }, $client);
        my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };