]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
wwwlisting: avoid hogging event loop
[public-inbox.git] / lib / PublicInbox / ManifestJsGz.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::WwwListing);
9 use Digest::SHA ();
10 use File::Spec ();
11 use bytes (); # length
12 use PublicInbox::Inbox;
13 use PublicInbox::Git;
14 use IO::Compress::Gzip qw(gzip);
15 use HTTP::Date qw(time2str);
16 *try_cat = \&PublicInbox::Inbox::try_cat;
17
18 our $json;
19 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
20         eval "require $mod" or next;
21         # ->ascii encodes non-ASCII to "\uXXXX"
22         $json = $mod->new->ascii(1) and last;
23 }
24
25 # called by WwwListing
26 sub url_regexp {
27         my ($ctx) = @_;
28         # grokmirror uses relative paths, so it's domain-dependent
29         # SUPER calls PublicInbox::WwwListing::url_regexp
30         $ctx->SUPER::url_regexp('publicInbox.grokManifest', 'match=domain');
31 }
32
33 sub fingerprint ($) {
34         my ($git) = @_;
35         # TODO: convert to qspawn for fairness when there's
36         # thousands of repos
37         my ($fh, $pid) = $git->popen('show-ref');
38         my $dig = Digest::SHA->new(1);
39         while (read($fh, my $buf, 65536)) {
40                 $dig->add($buf);
41         }
42         close $fh;
43         waitpid($pid, 0);
44         return if $?; # empty, uninitialized git repo
45         $dig->hexdigest;
46 }
47
48 sub manifest_add ($$;$$) {
49         my ($ctx, $ibx, $epoch, $default_desc) = @_;
50         my $url_path = "/$ibx->{name}";
51         my $git_dir = $ibx->{inboxdir};
52         if (defined $epoch) {
53                 $git_dir .= "/git/$epoch.git";
54                 $url_path .= "/git/$epoch.git";
55         }
56         return unless -d $git_dir;
57         my $git = PublicInbox::Git->new($git_dir);
58         my $fingerprint = fingerprint($git) or return; # no empty repos
59
60         chomp(my $owner = $git->qx('config', 'gitweb.owner'));
61         chomp(my $desc = try_cat("$git_dir/description"));
62         utf8::decode($owner);
63         utf8::decode($desc);
64         $owner = undef if $owner eq '';
65         $desc = 'Unnamed repository' if $desc eq '';
66
67         # templates/hooks--update.sample and git-multimail in git.git
68         # only match "Unnamed repository", not the full contents of
69         # templates/this--description in git.git
70         if ($desc =~ /\AUnnamed repository/) {
71                 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
72         }
73
74         my $reference;
75         chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
76         if ($alt) {
77                 # n.b.: GitPython doesn't seem to handle comments or C-quoted
78                 # strings like native git does; and we don't for now, either.
79                 my @alt = split(/\n+/, $alt);
80
81                 # grokmirror only supports 1 alternate for "reference",
82                 if (scalar(@alt) == 1) {
83                         my $objdir = "$git_dir/objects";
84                         $reference = File::Spec->rel2abs($alt[0], $objdir);
85                         $reference =~ s!/[^/]+/?\z!!; # basename
86                 }
87         }
88         $ctx->{-abs2urlpath}->{$git_dir} = $url_path;
89         my $modified = $git->modified;
90         if ($modified > ($ctx->{-mtime} // 0)) {
91                 $ctx->{-mtime} = $modified;
92         }
93         $ctx->{manifest}->{$url_path} = {
94                 owner => $owner,
95                 reference => $reference,
96                 description => $desc,
97                 modified => $modified,
98                 fingerprint => $fingerprint,
99         };
100 }
101
102 sub ibx_entry {
103         my ($ctx, $ibx) = @_;
104         eval {
105                 if (defined(my $max = $ibx->max_git_epoch)) {
106                         my $desc = $ibx->description;
107                         for my $epoch (0..$max) {
108                                 manifest_add($ctx, $ibx, $epoch, $desc);
109                         }
110                 } else {
111                         manifest_add($ctx, $ibx);
112                 }
113         };
114         warn "E: $@" if $@;
115 }
116
117 sub hide_key { 'manifest' }
118
119 # overrides WwwListing->psgi_triple
120 sub psgi_triple {
121         my ($ctx) = @_;
122         my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
123         my $manifest = delete($ctx->{manifest}) // {};
124         while (my ($url_path, $repo) = each %$manifest) {
125                 defined(my $abs = $repo->{reference}) or next;
126                 $repo->{reference} = $abs2urlpath->{$abs};
127         }
128         $manifest = $json->encode($manifest);
129         gzip(\$manifest => \(my $out));
130         [ 200, [ qw(Content-Type application/gzip),
131                  'Last-Modified', time2str($ctx->{-mtime}),
132                  'Content-Length', bytes::length($out) ], [ $out ] ]
133 }
134
135 1;