]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www_coderepo: wire up snapshot support
authorEric Wong <e@80x24.org>
Tue, 4 Oct 2022 19:12:38 +0000 (19:12 +0000)
committerEric Wong <e@80x24.org>
Wed, 5 Oct 2022 21:15:28 +0000 (21:15 +0000)
These should be compatible with cgit results

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

index cf6d97e1fc347b228dab1c9b2d548b8dfd31eaa3..29f368def8d5b7ac5c14debc86019b62bb7c6e31 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -306,6 +306,7 @@ lib/PublicInbox/PktOp.pm
 lib/PublicInbox/ProcessPipe.pm
 lib/PublicInbox/Qspawn.pm
 lib/PublicInbox/Reply.pm
+lib/PublicInbox/RepoSnapshot.pm
 lib/PublicInbox/SaPlugin/ListMirror.pm
 lib/PublicInbox/SaPlugin/ListMirror.pod
 lib/PublicInbox/Search.pm
index 691462eda4880533cae6357f20bc663a235936e3..2ed3a29bcd6fdafa2b453a4979b66f82b0f5d37e 100644 (file)
@@ -426,6 +426,7 @@ sub cleanup {
                                scalar(@{$self->{inflight} // []}));
        local $in_cleanup = 1;
        delete $self->{async_cat};
+       delete $self->{async_chk};
        async_wait_all($self);
        delete $self->{inflight};
        delete $self->{inflight_c};
index 613dbf7e9966ed153c09144cc0a507ad03070eed..2e0725a61b2248d0124c3a75e38befd97c76f55e 100644 (file)
@@ -1,14 +1,14 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # internal class used by PublicInbox::Git + PublicInbox::DS
 # This parses the output pipe of "git cat-file --batch"
 package PublicInbox::GitAsyncCat;
-use strict;
+use v5.12;
 use parent qw(PublicInbox::DS Exporter);
 use POSIX qw(WNOHANG);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
-our @EXPORT = qw(ibx_async_cat ibx_async_prefetch);
+our @EXPORT = qw(ibx_async_cat ibx_async_prefetch async_check);
 use PublicInbox::Git ();
 
 our $GCF2C; # singleton PublicInbox::Gcf2Client
@@ -74,6 +74,18 @@ sub ibx_async_cat ($$$$) {
        }
 }
 
+sub async_check ($$$$) {
+       my ($ibx, $oidish, $cb, $arg) = @_;
+       my $git = $ibx->{git} // $ibx->git;
+       $git->check_async($oidish, $cb, $arg);
+       $git->{async_chk} //= do {
+               my $self = bless { git => $git }, 'PublicInbox::GitAsyncCheck';
+               $git->{in_c}->blocking(0);
+               $self->SUPER::new($git->{in_c}, EPOLLIN|EPOLLET);
+               \undef; # this is a true ref()
+       };
+}
+
 # this is safe to call inside $cb, but not guaranteed to enqueue
 # returns true if successful, undef if not.  For fairness, we only
 # prefetch if there's no in-flight requests.
@@ -96,3 +108,34 @@ sub ibx_async_prefetch {
 }
 
 1;
+package PublicInbox::GitAsyncCheck;
+use v5.12;
+our @ISA = qw(PublicInbox::GitAsyncCat);
+use POSIX qw(WNOHANG);
+use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
+
+sub event_step {
+       my ($self) = @_;
+       my $git = $self->{git} or return;
+       return $self->close if ($git->{in_c} // 0) != ($self->{sock} // 1);
+       my $inflight = $git->{inflight_c};
+       if ($inflight && @$inflight) {
+               $git->check_async_step($inflight);
+
+               # child death?
+               if (($git->{in_c} // 0) != ($self->{sock} // 1)) {
+                       $self->close;
+               } elsif (@$inflight || exists $git->{rbuf_c}) {
+                       # ok, more to do, requeue for fairness
+                       $self->requeue;
+               }
+       } elsif ((my $pid = waitpid($git->{pid_c}, WNOHANG)) > 0) {
+               # May happen if the child process is killed by a BOFH
+               # (or segfaults)
+               delete $git->{pid_c};
+               warn "E: git $pid exited with \$?=$?\n";
+               $self->close;
+       }
+}
+
+1;
diff --git a/lib/PublicInbox/RepoSnapshot.pm b/lib/PublicInbox/RepoSnapshot.pm
new file mode 100644 (file)
index 0000000..460340e
--- /dev/null
@@ -0,0 +1,95 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# cgit-compatible /snapshot/ endpoint for WWW coderepos
+package PublicInbox::RepoSnapshot;
+use v5.12;
+use PublicInbox::Git;
+use PublicInbox::Qspawn;
+use PublicInbox::GitAsyncCat;
+use PublicInbox::WwwStatic qw(r);
+
+# Not using standard mime types since the compressed tarballs are
+# special or do not match my /etc/mime.types.  Choose what gitweb
+# and cgit agree on for compatibility.
+our %FMT_TYPES = (
+       'tar' => 'application/x-tar',
+       'tar.gz' => 'application/x-gzip',
+       'tar.bz2' => 'application/x-bzip2',
+       'tar.xz' => 'application/x-xz',
+       'zip' => 'application/x-zip',
+);
+
+our %FMT_CFG = (
+       'tar.xz' => 'xz -c',
+       'tar.bz2' => 'bzip2 -c',
+       # not supporting lz nor zstd for now to avoid format proliferation
+       # and increased cache overhead required to handle extra formats.
+);
+
+my $SUFFIX = join('|', map { quotemeta } keys %FMT_TYPES);
+
+# TODO deal with tagged blobs
+
+sub archive_hdr { # parse_hdr for Qspawn
+       my ($r, $bref, $ctx) = @_;
+       $r or return [500, [qw(Content-Type text/plain Content-Length 0)], []];
+       my $fn = "$ctx->{snap_pfx}.$ctx->{snap_fmt}";
+       my $type = $FMT_TYPES{$ctx->{snap_fmt}} //
+                               die "BUG: bad fmt: $ctx->{snap_fmt}";
+       [ 200, [ 'Content-Type', "$type; charset=UTF-8",
+               'Content-Disposition', qq(inline; filename="$fn"),
+               '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
+                       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);
+       }
+       $ctx->{etag} = $oid;
+       archive_cb($ctx);
+}
+
+sub srv {
+       my ($ctx, $fn) = @_;
+       return if $fn =~ /["\s]/s;
+       $fn =~ s/\.($SUFFIX)\z//o or return;
+       $ctx->{snap_fmt} = $1;
+       my $pfx = $ctx->{git}->local_nick // return;
+       $pfx =~ s/(?:\.git)?\z/-/;
+       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
+       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;
+               }
+       }
+}
+
+1;
index e0fc90459d209b1a7299a550f09235723880d360..fb510b2848b6fc4351dfd1d217421cf99614a7db 100644 (file)
@@ -175,6 +175,14 @@ sub srv { # endpoint called by PublicInbox::WWW
                        ($ctx->{git} = $self->{"\0$1"}) and
                return PublicInbox::ViewVCS::show($ctx, $2);
 
+       # snapshots:
+       if ($path_info =~ m!\A/(.+?)/snapshot/([^/]+)\z! and
+                       ($ctx->{git} = $self->{"\0$1"})) {
+               require PublicInbox::RepoSnapshot;
+               return PublicInbox::RepoSnapshot::srv($ctx, $2) // r(404);
+       }
+
+       # enforce trailing slash:
        if ($path_info =~ m!\A/(.+?)\z! and ($git = $self->{"\0$1"})) {
                my $qs = $ctx->{env}->{QUERY_STRING};
                my $url = $git->base_url($ctx->{env});
index d6936c47e1214d302aa5adcc24e47df42033a688..71b9554a1ba3e0017a306f7dece4befa4de83509 100644 (file)
@@ -34,6 +34,7 @@ File::Path::mkpath([map { $md.$_ } (qw(/ /cur /new /tmp))]);
 symlink(abs_path('t/solve/0001-simple-mod.patch'), "$md/cur/foo:2,") or
        xbail "symlink: $!";
 
+my $v1_0_0_rev = '8a918a8523bc9904123460f85999d75f6d604916';
 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
 my $v1_0_0_tag_short = substr($v1_0_0_tag, 0, 16);
 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
@@ -331,6 +332,25 @@ EOF
                is($res->code, 200, 'coderepo summary (public-inbox)');
                $res = $cb->(GET('/public-inbox'));
                is($res->code, 301, 'redirected');
+
+               my $fn = 'public-inbox-1.0.0.tar.gz';
+               $res = $cb->(GET("/public-inbox/snapshot/$fn"));
+               is($res->code, 200, 'tar.gz snapshot');
+               is($res->header('Content-Disposition'),
+                       qq'inline; filename="$fn"', 'c-d header');
+               is($res->header('ETag'), qq'"$v1_0_0_rev"', 'etag header');
+               my $exp = xqx([qw(git archive --format=tar.gz
+                               --prefix=public-inbox-1.0.0/ v1.0.0)],
+                               { GIT_DIR => $git_dir });
+               my $got = $res->content;
+               is(length($got), length($exp),
+                       "length matches installed `git archive' output") and
+               is(git_sha(1, \$got)->hexdigest, git_sha(1, \$exp)->hexdigest,
+                       "content matches installed `git archive' output");
+
+               $fn = 'public-inbox-1.0.2.tar.gz';
+               $res = $cb->(GET("/public-inbox/snapshot/$fn"));
+               is($res->code, 404, '404 on non-existent tag');
        };
        test_psgi(sub { $www->call(@_) }, $client);
        my $env = { PI_CONFIG => $cfgpath, TMPDIR => $tmpdir };