1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
8 use parent qw(PublicInbox::WwwListing);
11 use bytes (); # length
12 use PublicInbox::Inbox;
14 use IO::Compress::Gzip qw(gzip);
15 use HTTP::Date qw(time2str);
16 *try_cat = \&PublicInbox::Inbox::try_cat;
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;
25 # called by WwwListing
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');
35 # TODO: convert to qspawn for fairness when there's
37 my ($fh, $pid) = $git->popen('show-ref');
38 my $dig = Digest::SHA->new(1);
39 while (read($fh, my $buf, 65536)) {
44 return if $?; # empty, uninitialized git repo
48 sub manifest_add ($$;$$) {
49 my ($ctx, $ibx, $epoch, $default_desc) = @_;
50 my $url_path = "/$ibx->{name}";
51 my $git_dir = $ibx->{inboxdir};
53 $git_dir .= "/git/$epoch.git";
54 $url_path .= "/git/$epoch.git";
56 return unless -d $git_dir;
57 my $git = PublicInbox::Git->new($git_dir);
58 my $fingerprint = fingerprint($git) or return; # no empty repos
60 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
61 chomp(my $desc = try_cat("$git_dir/description"));
64 $owner = undef if $owner eq '';
65 $desc = 'Unnamed repository' if $desc eq '';
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);
75 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
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);
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
88 $ctx->{-abs2urlpath}->{$git_dir} = $url_path;
89 my $modified = $git->modified;
90 if ($modified > ($ctx->{-mtime} // 0)) {
91 $ctx->{-mtime} = $modified;
93 $ctx->{manifest}->{$url_path} = {
95 reference => $reference,
97 modified => $modified,
98 fingerprint => $fingerprint,
103 my ($ctx, $ibx) = @_;
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);
111 manifest_add($ctx, $ibx);
117 sub hide_key { 'manifest' }
119 # overrides WwwListing->psgi_triple
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};
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 ] ]