]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/RepoSnapshot.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / RepoSnapshot.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # cgit-compatible /snapshot/ endpoint for WWW coderepos
5 package PublicInbox::RepoSnapshot;
6 use v5.12;
7 use PublicInbox::Qspawn;
8 use PublicInbox::ViewVCS;
9 use PublicInbox::WwwStatic qw(r);
10
11 # Not using standard mime types since the compressed tarballs are
12 # special or do not match my /etc/mime.types.  Choose what gitweb
13 # and cgit agree on for compatibility.
14 our %FMT_TYPES = (
15         'tar' => 'application/x-tar',
16         'tar.gz' => 'application/x-gzip',
17         'tar.bz2' => 'application/x-bzip2',
18         'tar.xz' => 'application/x-xz',
19         'zip' => 'application/x-zip',
20 );
21
22 our %FMT_CFG = (
23         'tar.xz' => 'xz -c',
24         'tar.bz2' => 'bzip2 -c',
25         # not supporting lz nor zstd for now to avoid format proliferation
26         # and increased cache overhead required to handle extra formats.
27 );
28
29 my $SUFFIX = join('|', map { quotemeta } keys %FMT_TYPES);
30
31 # TODO deal with tagged blobs
32
33 sub archive_hdr { # parse_hdr for Qspawn
34         my ($r, $bref, $ctx) = @_;
35         $r or return [500, [qw(Content-Type text/plain Content-Length 0)], []];
36         my $fn = "$ctx->{snap_pfx}.$ctx->{snap_fmt}";
37         my $type = $FMT_TYPES{$ctx->{snap_fmt}} //
38                                 die "BUG: bad fmt: $ctx->{snap_fmt}";
39         [ 200, [ 'Content-Type', "$type; charset=UTF-8",
40                 'Content-Disposition', qq(inline; filename="$fn"),
41                 'ETag', qq("$ctx->{etag}") ] ];
42 }
43
44 sub ver_check { # git->check_async callback
45         my ($oid, $type, $size, $ctx) = @_;
46         return if defined $ctx->{etag};
47         my $treeish = shift @{$ctx->{-try}} // die 'BUG: no {-try}';
48         if ($type eq 'missing') {
49                 scalar(@{$ctx->{-try}}) or
50                         delete($ctx->{env}->{'qspawn.wcb'})->(r(404));
51         } else { # found, done:
52                 $ctx->{etag} = $oid;
53                 my @cfg;
54                 if (my $cmd = $FMT_CFG{$ctx->{snap_fmt}}) {
55                         @cfg = ('-c', "tar.$ctx->{snap_fmt}.command=$cmd");
56                 }
57                 my $qsp = PublicInbox::Qspawn->new(['git', @cfg,
58                                 "--git-dir=$ctx->{git}->{git_dir}", 'archive',
59                                 "--prefix=$ctx->{snap_pfx}/",
60                                 "--format=$ctx->{snap_fmt}", $treeish]);
61                 $qsp->psgi_return($ctx->{env}, undef, \&archive_hdr, $ctx);
62         }
63 }
64
65 sub srv {
66         my ($ctx, $fn) = @_;
67         return if $fn =~ /["\s]/s;
68         my $fmt = $ctx->{wcr}->{snapshots}; # TODO per-repo snapshots
69         $fn =~ s/\.($SUFFIX)\z//o and $fmt->{$1} or return;
70         $ctx->{snap_fmt} = $1;
71         my $pfx = $ctx->{git}->local_nick // return;
72         $pfx =~ s/(?:\.git)?\z/-/;
73         ($pfx) = ($pfx =~ m!([^/]+)\z!);
74         substr($fn, 0, length($pfx)) eq $pfx or return;
75         $ctx->{snap_pfx} = $fn;
76         my $v = $ctx->{snap_ver} = substr($fn, length($pfx), length($fn));
77         # try without [vV] prefix, first
78         my @try = map { "$_$v" } ('', 'v', 'V'); # cf. cgit:ui-snapshot.c
79         @{$ctx->{-try}} = @try;
80         sub {
81                 $ctx->{env}->{'qspawn.wcb'} = $_[0];
82                 PublicInbox::ViewVCS::do_check_async($ctx, \&ver_check, @try);
83         }
84 }
85
86 1;