]> Sergey Matveev's repositories - public-inbox.git/commitdiff
coderepo: consolidate git --batch-check users
authorEric Wong <e@80x24.org>
Fri, 13 Jan 2023 10:35:50 +0000 (10:35 +0000)
committerEric Wong <e@80x24.org>
Fri, 13 Jan 2023 19:14:42 +0000 (19:14 +0000)
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.

lib/PublicInbox/RepoSnapshot.pm
lib/PublicInbox/RepoTree.pm
lib/PublicInbox/ViewVCS.pm

index 826392a85a8b02b64263037a660a1a2c634251a1..93ba4db6e332a289afa89b177866201bf8134a43 100644 (file)
@@ -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);
        }
 }
 
index 84e2058983027a1ad2c3a5e63682168c3494e930..7434e9b275f08d2dd8f5c19e1333bb0b2b40ff0e 100644 (file)
@@ -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);
        };
 }
 
index eae5b7f416cf88f8c35823dc5647ef01b0c415ca..37b688ed586e4e6ae42837cad03679d5ebb7411b 100644 (file)
@@ -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}) {