+sub cat_async_begin {
+ my ($self) = @_;
+ cleanup($self) if $self->alternates_changed;
+ $self->batch_prepare;
+ die 'BUG: already in async' if $self->{inflight};
+ $self->{inflight} = [];
+}
+
+sub cat_async ($$$;$) {
+ my ($self, $oid, $cb, $arg) = @_;
+ my $inflight = $self->{inflight} // cat_async_begin($self);
+ while (scalar(@$inflight) >= MAX_INFLIGHT) {
+ cat_async_step($self, $inflight);
+ }
+ print { $self->{out} } $oid, "\n" or $self->fail("write error: $!");
+ push(@$inflight, $oid, $cb, $arg);
+}
+
+sub async_prefetch {
+ my ($self, $oid, $cb, $arg) = @_;
+ if (my $inflight = $self->{inflight}) {
+ # we could use MAX_INFLIGHT here w/o the halving,
+ # but lets not allow one client to monopolize a git process
+ if (scalar(@$inflight) < int(MAX_INFLIGHT/2)) {
+ print { $self->{out} } $oid, "\n" or
+ $self->fail("write error: $!");
+ return push(@$inflight, $oid, $cb, $arg);
+ }
+ }
+ undef;
+}
+
+sub extract_cmt_time {
+ my ($bref, undef, undef, undef, $modified) = @_;
+
+ if ($$bref =~ /^committer .*?> ([0-9]+) [\+\-]?[0-9]+/sm) {
+ my $cmt_time = $1 + 0;
+ $$modified = $cmt_time if $cmt_time > $$modified;
+ }
+}
+
+# returns the modified time of a git repo, same as the "modified" field
+# of a grokmirror manifest
+sub modified ($) {
+ my ($self) = @_;
+ my $modified = 0;
+ my $fh = popen($self, qw(rev-parse --branches));
+ local $/ = "\n";
+ while (my $oid = <$fh>) {
+ chomp $oid;
+ cat_async($self, $oid, \&extract_cmt_time, \$modified);
+ }
+ cat_async_wait($self);
+ $modified || time;
+}
+
+# for grokmirror, which doesn't read gitweb.description
+# templates/hooks--update.sample and git-multimail in git.git
+# only match "Unnamed repository", not the full contents of
+# templates/this--description in git.git
+sub manifest_entry {
+ my ($self, $epoch, $default_desc) = @_;
+ my ($fh, $pid) = $self->popen('show-ref');
+ my $dig = Digest::SHA->new(1);
+ while (read($fh, my $buf, 65536)) {
+ $dig->add($buf);
+ }
+ close $fh;
+ waitpid($pid, 0);
+ return if $?; # empty, uninitialized git repo
+ my $git_dir = $self->{git_dir};
+ my $ent = {
+ fingerprint => $dig->hexdigest,
+ reference => undef,
+ modified => modified($self),
+ };
+ chomp(my $owner = $self->qx('config', 'gitweb.owner'));
+ utf8::decode($owner);
+ $ent->{owner} = $owner eq '' ? undef : $owner;
+ my $desc = '';
+ if (open($fh, '<', "$git_dir/description")) {
+ local $/ = "\n";
+ chomp($desc = <$fh>);
+ utf8::decode($desc);
+ }
+ $desc = 'Unnamed repository' if $desc eq '';
+ if (defined $epoch && $desc =~ /\AUnnamed repository/) {
+ $desc = "$default_desc [epoch $epoch]";
+ }
+ $ent->{description} = $desc;
+ if (open($fh, '<', "$git_dir/objects/info/alternates")) {
+ # n.b.: GitPython doesn't seem to handle comments or C-quoted
+ # strings like native git does; and we don't for now, either.
+ local $/ = "\n";
+ chomp(my @alt = <$fh>);
+
+ # grokmirror only supports 1 alternate for "reference",
+ if (scalar(@alt) == 1) {
+ my $objdir = "$git_dir/objects";
+ my $ref = File::Spec->rel2abs($alt[0], $objdir);
+ $ref =~ s!/[^/]+/?\z!!; # basename
+ $ent->{reference} = $ref;
+ }
+ }
+ $ent;
+}
+