+sub apply_result ($$) {
+ my ($bref, $self) = @_;
+ my ($qsp, $di) = delete @$self{qw(-qsp -cur_di)};
+ dbg($self, $$bref);
+ my $patches = $self->{patches};
+ if (my $err = $qsp->{err}) {
+ my $msg = "git apply error: $err";
+ my $nxt = $patches->[0];
+ if ($nxt && oids_same_ish($nxt->{oid_b}, $di->{oid_b})) {
+ dbg($self, $msg);
+ dbg($self, 'trying '.di_url($self, $nxt));
+ return do_git_apply($self);
+ } else {
+ ERR($self, $msg);
+ }
+ } else {
+ skip_identical($self, $patches, $di->{oid_b});
+ }
+
+ my @cmd = qw(git ls-files -s -z);
+ $qsp = PublicInbox::Qspawn->new(\@cmd, $self->{git_env});
+ $self->{-cur_di} = $di;
+ $self->{-qsp} = $qsp;
+ $qsp->psgi_qx($self->{psgi_env}, undef, \&ls_files_result, $self);
+}
+
+sub do_git_apply ($) {
+ my ($self) = @_;
+ my $dn = $self->{tmp}->dirname;
+ my $patches = $self->{patches};
+
+ # we need --ignore-whitespace because some patches are CRLF
+ my @cmd = (qw(git apply --cached --ignore-whitespace
+ --unidiff-zero --whitespace=warn --verbose));
+ my $len = length(join(' ', @cmd));
+ my $total = $self->{tot};
+ my $di; # keep track of the last one for "git ls-files"
+ my $prv_oid_b;
+
+ do {
+ my $i = ++$self->{nr};
+ $di = shift @$patches;
+ dbg($self, "\napplying [$i/$total] " . di_url($self, $di) .
+ "\n" . $di->{hdr_lines});
+ my $path = $di->{n};
+ $len += length($path) + 1;
+ push @cmd, $path;
+ $prv_oid_b = $di->{oid_b};
+ } while (@$patches && $len < $ARG_SIZE_MAX &&
+ !oids_same_ish($patches->[0]->{oid_b}, $prv_oid_b));
+
+ my $opt = { 2 => 1, -C => $dn, quiet => 1 };
+ my $qsp = PublicInbox::Qspawn->new(\@cmd, $self->{git_env}, $opt);
+ $self->{-cur_di} = $di;
+ $self->{-qsp} = $qsp;
+ $qsp->psgi_qx($self->{psgi_env}, undef, \&apply_result, $self);
+}
+
+sub di_url ($$) {
+ my ($self, $di) = @_;
+ # note: we don't pass the PSGI env unconditionally, here,
+ # different inboxes can have different HTTP_HOST on the same instance.
+ my $ibx = $di->{ibx};
+ my $env = $self->{psgi_env} if $ibx eq $self->{inboxes}->[0];
+ my $url = $ibx->base_url($env);