about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-01-13 10:35:50 +0000
committerEric Wong <e@80x24.org>2023-01-13 19:14:42 +0000
commit1c8b8a0a7a8bf81303bd2b1f6ad5b377ec1fa4b2 (patch)
tree84dcb77250d996be62dba56ee24e2a09755447a8
parentd967c043322e636fd6ff810d54b70d8cd9fe91df (diff)
downloadpublic-inbox-1c8b8a0a7a8bf81303bd2b1f6ad5b377ec1fa4b2.tar.gz
And another opportunity to simplify our code between different
PSGI-ish implementations.  The snapshot retrieval is simpler,
but potentially slower since we waste cycles scanning for tags
even after we've found one.  It's probably not a big deal since
it's only short info lines and we can utilize pipelining.
-rw-r--r--lib/PublicInbox/RepoSnapshot.pm51
-rw-r--r--lib/PublicInbox/RepoTree.pm8
-rw-r--r--lib/PublicInbox/ViewVCS.pm14
3 files changed, 33 insertions, 40 deletions
diff --git a/lib/PublicInbox/RepoSnapshot.pm b/lib/PublicInbox/RepoSnapshot.pm
index 826392a8..93ba4db6 100644
--- a/lib/PublicInbox/RepoSnapshot.pm
+++ b/lib/PublicInbox/RepoSnapshot.pm
@@ -4,9 +4,8 @@
 # cgit-compatible /snapshot/ endpoint for WWW coderepos
 package PublicInbox::RepoSnapshot;
 use v5.12;
-use PublicInbox::Git;
 use PublicInbox::Qspawn;
-use PublicInbox::GitAsyncCat;
+use PublicInbox::ViewVCS;
 use PublicInbox::WwwStatic qw(r);
 
 # Not using standard mime types since the compressed tarballs are
@@ -42,31 +41,25 @@ sub archive_hdr { # parse_hdr for Qspawn
                 'ETag', qq("$ctx->{etag}") ] ];
 }
 
-sub archive_cb {
-        my ($ctx) = @_;
-        my @cfg;
-        if (my $cmd = $FMT_CFG{$ctx->{snap_fmt}}) {
-                @cfg = ('-c', "tar.$ctx->{snap_fmt}.command=$cmd");
-        }
-        my $qsp = PublicInbox::Qspawn->new(['git', @cfg,
-                        "--git-dir=$ctx->{git}->{git_dir}", 'archive',
-                        "--prefix=$ctx->{snap_pfx}/",
-                        "--format=$ctx->{snap_fmt}", $ctx->{treeish}]);
-        $qsp->psgi_return($ctx->{env}, undef, \&archive_hdr, $ctx);
-}
-
 sub ver_check { # git->check_async callback
         my ($oid, $type, $size, $ctx) = @_;
-        if ($type eq 'missing') { # try 'v' and 'V' prefixes
-                my $pfx = shift @{$ctx->{try_pfx}} or return
+        return if defined $ctx->{etag};
+        my $treeish = shift @{$ctx->{-try}} // die 'BUG: no {-try}';
+        if ($type eq 'missing') {
+                scalar(@{$ctx->{-try}}) or
                         delete($ctx->{env}->{'qspawn.wcb'})->(r(404));
-                my $v = $ctx->{treeish} = $pfx.$ctx->{snap_ver};
-                return $ctx->{env}->{'pi-httpd.async'} ?
-                        async_check($ctx, $v, \&ver_check, $ctx) :
-                        $ctx->{git}->check_async($v, \&ver_check, $ctx);
+        } else { # found, done:
+                $ctx->{etag} = $oid;
+                my @cfg;
+                if (my $cmd = $FMT_CFG{$ctx->{snap_fmt}}) {
+                        @cfg = ('-c', "tar.$ctx->{snap_fmt}.command=$cmd");
+                }
+                my $qsp = PublicInbox::Qspawn->new(['git', @cfg,
+                                "--git-dir=$ctx->{git}->{git_dir}", 'archive',
+                                "--prefix=$ctx->{snap_pfx}/",
+                                "--format=$ctx->{snap_fmt}", $treeish]);
+                $qsp->psgi_return($ctx->{env}, undef, \&archive_hdr, $ctx);
         }
-        $ctx->{etag} = $oid;
-        archive_cb($ctx);
 }
 
 sub srv {
@@ -81,16 +74,12 @@ sub srv {
         substr($fn, 0, length($pfx)) eq $pfx or return;
         $ctx->{snap_pfx} = $fn;
         my $v = $ctx->{snap_ver} = substr($fn, length($pfx), length($fn));
-        $ctx->{treeish} = $v; # try without [vV] prefix, first
-        @{$ctx->{try_pfx}} = qw(v V); # cf. cgit:ui-snapshot.c
+        # try without [vV] prefix, first
+        my @try = map { "$_$v" } ('', 'v', 'V'); # cf. cgit:ui-snapshot.c
+        @{$ctx->{-try}} = @try;
         sub {
                 $ctx->{env}->{'qspawn.wcb'} = $_[0];
-                if ($ctx->{env}->{'pi-httpd.async'}) {
-                        async_check($ctx, $v, \&ver_check, $ctx);
-                } else {
-                        $ctx->{git}->check_async($v, \&ver_check, $ctx);
-                        $ctx->{git}->check_async_wait;
-                }
+                PublicInbox::ViewVCS::do_check_async($ctx, \&ver_check, @try);
         }
 }
 
diff --git a/lib/PublicInbox/RepoTree.pm b/lib/PublicInbox/RepoTree.pm
index 84e20589..7434e9b2 100644
--- a/lib/PublicInbox/RepoTree.pm
+++ b/lib/PublicInbox/RepoTree.pm
@@ -5,7 +5,6 @@
 package PublicInbox::RepoTree;
 use v5.12;
 use PublicInbox::ViewDiff qw(uri_escape_path);
-use PublicInbox::GitAsyncCat;
 use PublicInbox::WwwStatic qw(r);
 use PublicInbox::Qspawn;
 use PublicInbox::WwwStream qw(html_oneshot);
@@ -78,12 +77,7 @@ sub srv_tree {
         return if index($obj, "\n") >= 0;
         sub {
                 $ctx->{-wcb} = $_[0]; # HTTP::{Chunked,Identity}
-                if ($ctx->{env}->{'pi-httpd.async'}) {
-                        async_check($ctx, $obj, \&tree_show, $ctx);
-                } else {
-                        $ctx->{git}->check_async($obj, \&tree_show, $ctx);
-                        $ctx->{git}->async_wait_all;
-                }
+                PublicInbox::ViewVCS::do_check_async($ctx, \&tree_show, $obj);
         };
 }
 
diff --git a/lib/PublicInbox/ViewVCS.pm b/lib/PublicInbox/ViewVCS.pm
index eae5b7f4..37b688ed 100644
--- a/lib/PublicInbox/ViewVCS.pm
+++ b/lib/PublicInbox/ViewVCS.pm
@@ -126,9 +126,9 @@ sub cmt_title { # git->cat_async callback
 }
 
 sub do_cat_async {
-        my ($ctx, $cb, @oids) = @_;
+        my ($ctx, $cb, @req) = @_;
         # favor git(1) over Gcf2 (libgit2) for SHA-256 support
-        $ctx->{git}->cat_async($_, $cb, $ctx) for @oids;
+        $ctx->{git}->cat_async($_, $cb, $ctx) for @req;
         if ($ctx->{env}->{'pi-httpd.async'}) {
                 PublicInbox::GitAsyncCat::watch_cat($ctx->{git});
         } else { # synchronous, generic PSGI
@@ -136,6 +136,16 @@ sub do_cat_async {
         }
 }
 
+sub do_check_async {
+        my ($ctx, $cb, @req) = @_;
+        if ($ctx->{env}->{'pi-httpd.async'}) {
+                async_check($ctx, $_, $cb, $ctx) for @req;
+        } else { # synchronous, generic PSGI
+                $ctx->{git}->check_async($_, $cb, $ctx) for @req;
+                $ctx->{git}->check_async_wait;
+        }
+}
+
 sub show_commit_start { # ->psgi_qx callback
         my ($bref, $ctx) = @_;
         if (my $qsp_err = delete $ctx->{-qsp_err}) {