+sub prepare_index ($) {
+ my ($self) = @_;
+ my $patches = $self->{patches};
+ $self->{nr} = 0;
+
+ my $di = $patches->[0] or die 'no patches';
+ my $oid_a = $di->{oid_a} or die '{oid_a} unset';
+ my $existing = $self->{found}->{$oid_a};
+
+ # no index creation for added files
+ $oid_a =~ /\A0+\z/ and return next_step($self);
+
+ die "BUG: $oid_a not not found" unless $existing;
+
+ my $oid_full = $existing->[1];
+ my $path_a = $di->{path_a} or die "BUG: path_a missing for $oid_full";
+ my $mode_a = $di->{mode_a} || extract_old_mode($di);
+
+ my $in = tmpfile("update-index.$oid_full") or die "tmpfile: $!";
+ print $in "$mode_a $oid_full\t$path_a\0" or die "print: $!";
+ $in->flush or die "flush: $!";
+ sysseek($in, 0, 0) or die "seek: $!";
+
+ dbg($self, 'preparing index');
+ my $rdr = { 0 => fileno($in), -hold => $in };
+ my $cmd = [ qw(git update-index -z --index-info) ];
+ my $qsp = PublicInbox::Qspawn->new($cmd, $self->{git_env}, $rdr);
+ $qsp->psgi_qx($self->{psgi_env}, undef, sub {
+ my ($bref) = @_;
+ if (my $err = $qsp->{err}) {
+ ERR($self, "git update-index error: $err");
+ }
+ dbg($self, "index prepared:\n" .
+ "$mode_a $oid_full\t" . git_quote($path_a));
+ next_step($self); # onto do_git_apply
+ });
+}
+