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 package PublicInbox::ManifestJsGz;
9 use PublicInbox::Inbox;
11 use IO::Compress::Gzip qw(gzip);
12 use HTTP::Date qw(time2str);
13 *try_cat = \&PublicInbox::Inbox::try_cat;
16 for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
17 eval "require $mod" or next;
18 # ->ascii encodes non-ASCII to "\uXXXX"
19 $json = $mod->new->ascii(1) and last;
23 my ($env, $list) = @_;
24 $json or return [ 404, [], [] ];
33 # PSGI server will call this immediately and give us a callback (-wcb)
35 $self->{-wcb} = $_[0]; # HTTP write callback
42 # TODO: convert to qspawn for fairness when there's
44 my ($fh, $pid) = $git->popen('show-ref');
45 my $dig = Digest::SHA->new(1);
46 while (read($fh, my $buf, 65536)) {
51 return if $?; # empty, uninitialized git repo
55 sub manifest_add ($$;$$) {
56 my ($self, $ibx, $epoch, $default_desc) = @_;
57 my $url_path = "/$ibx->{name}";
58 my $git_dir = $ibx->{inboxdir};
60 $git_dir .= "/git/$epoch.git";
61 $url_path .= "/git/$epoch.git";
63 return unless -d $git_dir;
64 my $git = PublicInbox::Git->new($git_dir);
65 my $fingerprint = fingerprint($git) or return; # no empty repos
67 chomp(my $owner = $git->qx('config', 'gitweb.owner'));
68 chomp(my $desc = try_cat("$git_dir/description"));
71 $owner = undef if $owner eq '';
72 $desc = 'Unnamed repository' if $desc eq '';
74 # templates/hooks--update.sample and git-multimail in git.git
75 # only match "Unnamed repository", not the full contents of
76 # templates/this--description in git.git
77 if ($desc =~ /\AUnnamed repository/) {
78 $desc = "$default_desc [epoch $epoch]" if defined($epoch);
82 chomp(my $alt = try_cat("$git_dir/objects/info/alternates"));
84 # n.b.: GitPython doesn't seem to handle comments or C-quoted
85 # strings like native git does; and we don't for now, either.
86 my @alt = split(/\n+/, $alt);
88 # grokmirror only supports 1 alternate for "reference",
89 if (scalar(@alt) == 1) {
90 my $objdir = "$git_dir/objects";
91 $reference = File::Spec->rel2abs($alt[0], $objdir);
92 $reference =~ s!/[^/]+/?\z!!; # basename
95 $self->{-abs2urlpath}->{$git_dir} = $url_path;
96 my $modified = $git->modified;
97 if ($modified > $self->{-mtime}) {
98 $self->{-mtime} = $modified;
100 $self->{manifest}->{$url_path} = {
102 reference => $reference,
103 description => $desc,
104 modified => $modified,
105 fingerprint => $fingerprint,
111 if (my $async = $self->{psgi_env}->{'pi-httpd.async'}) {
112 # PublicInbox::HTTPD::Async->new
113 $async->(undef, undef, $self);
115 event_step($self) while $self->{-wcb};
121 while (my $ibx = shift(@{$self->{-list}})) {
123 if (defined(my $max = $ibx->max_git_epoch)) {
124 my $desc = $ibx->description;
125 for my $epoch (0..$max) {
126 manifest_add($self, $ibx, $epoch, $desc)
129 manifest_add($self, $ibx);
133 if (my $async = $self->{psgi_env}->{'pi-httpd.async'}) {
134 # PublicInbox::HTTPD::Async->new
135 $async->(undef, undef, $self);
137 return; # more steps needed
139 my $abs2urlpath = delete $self->{-abs2urlpath};
140 my $wcb = delete $self->{-wcb};
141 my $manifest = delete $self->{manifest};
142 while (my ($url_path, $repo) = each %$manifest) {
143 defined(my $abs = $repo->{reference}) or next;
144 $repo->{reference} = $abs2urlpath->{$abs};
146 $manifest = $json->encode($manifest);
147 gzip(\$manifest => \(my $out));
148 $wcb->([ 200, [ qw(Content-Type application/gzip),
149 'Last-Modified', time2str($self->{-mtime}),
150 'Content-Length', bytes::length($out) ], [ $out ] ]);