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