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;
13 use PublicInbox::Config;
15 use IO::Compress::Gzip qw(gzip);
16 use HTTP::Date qw(time2str);
17 *try_cat = \&PublicInbox::Inbox::try_cat;
19 our $json = PublicInbox::Config::json();
21 # called by WwwListing
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');
31 # TODO: convert to qspawn for fairness when there's
33 my ($fh, $pid) = $git->popen('show-ref');
34 my $dig = Digest::SHA->new(1);
35 while (read($fh, my $buf, 65536)) {
40 return if $?; # empty, uninitialized git repo
44 sub manifest_add ($$;$$) {
45 my ($ctx, $ibx, $epoch, $default_desc) = @_;
46 my $url_path = "/$ibx->{name}";
47 my $git_dir = $ibx->{inboxdir};
49 $git_dir .= "/git/$epoch.git";
50 $url_path .= "/git/$epoch.git";
52 return unless -d $git_dir;
53 my $git = PublicInbox::Git->new($git_dir);
54 my $fingerprint = fingerprint($git) or return; # no empty repos
56 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
57 chomp(my $desc = try_cat("$git_dir/description"));
60 $owner = undef if $owner eq '';
61 $desc = 'Unnamed repository' if $desc eq '';
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);
71 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
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);
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
84 $ctx->{-abs2urlpath}->{$git_dir} = $url_path;
85 my $modified = $git->modified;
86 if ($modified > ($ctx->{-mtime} // 0)) {
87 $ctx->{-mtime} = $modified;
89 $ctx->{manifest}->{$url_path} = {
91 reference => $reference,
93 modified => $modified,
94 fingerprint => $fingerprint,
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);
107 manifest_add($ctx, $ibx);
113 sub hide_key { 'manifest' }
115 # overrides WwwListing->psgi_triple
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};
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 ] ]