- $head =~ /^[0-9a-f]{40} \S+ ([0-9]+)$/ or
- fail($self, "Unexpected result from git cat-file: $head");
-
- my $size = $1;
- my $ref_type = $ref ? ref($ref) : '';
-
- my $rv;
- my $left = $size;
- $$ref = $size if ($ref_type eq 'SCALAR');
- my $cb_err;
-
- if ($ref_type eq 'CODE') {
- $rv = eval { $ref->($in, \$left) };
- $cb_err = $@;
- # drain the rest
- my $max = 8192;
- while ($left > 0) {
- my $r = read($in, my $x, $left > $max ? $max : $left);
- defined($r) or fail($self, "read failed: $!");
- $r == 0 and fail($self, 'exited unexpectedly');
- $left -= $r;
+}
+
+sub cat_async_retry ($$$$$) {
+ my ($self, $inflight, $req, $cb, $arg) = @_;
+
+ # {inflight} may be non-existent, but if it isn't we delete it
+ # here to prevent cleanup() from waiting:
+ delete $self->{inflight};
+ cleanup($self);
+
+ $self->{inflight} = $inflight;
+ batch_prepare($self);
+ my $buf = "$req\n";
+ for (my $i = 0; $i < @$inflight; $i += 3) {
+ $buf .= "$inflight->[$i]\n";
+ }
+ print { $self->{out} } $buf or $self->fail("write error: $!");
+ unshift(@$inflight, \$req, $cb, $arg); # \$ref to indicate retried
+
+ cat_async_step($self, $inflight); # take one step
+}
+
+sub cat_async_step ($$) {
+ my ($self, $inflight) = @_;
+ die 'BUG: inflight empty or odd' if scalar(@$inflight) < 3;
+ my ($req, $cb, $arg) = splice(@$inflight, 0, 3);
+ my $rbuf = delete($self->{cat_rbuf}) // \(my $new = '');
+ my ($bref, $oid, $type, $size);
+ my $head = my_readline($self->{in}, $rbuf);
+ # ->fail may be called via Gcf2Client.pm
+ if ($head =~ /^([0-9a-f]{40,}) (\S+) ([0-9]+)$/) {
+ ($oid, $type, $size) = ($1, $2, $3 + 0);
+ $bref = my_read($self->{in}, $rbuf, $size + 1) or
+ $self->fail(defined($bref) ? 'read EOF' : "read: $!");
+ chop($$bref) eq "\n" or $self->fail('LF missing after blob');
+ } elsif ($head =~ s/ missing\n//s) {
+ $oid = $head;
+ # ref($req) indicates it's already been retried
+ # -gcf2 retries internally, so it never hits this path:
+ if (!ref($req) && !$in_cleanup && $self->alternates_changed) {
+ return cat_async_retry($self, $inflight,
+ $req, $cb, $arg);