]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SolverGit.pm
solver: check one git coderepo and inbox at a time
[public-inbox.git] / lib / PublicInbox / SolverGit.pm
1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "Solve" blobs which don't exist in git code repositories by
5 # searching inboxes for post-image blobs.
6
7 # this emits a lot of debugging/tracing information which may be
8 # publicly viewed over HTTP(S).  Be careful not to expose
9 # local filesystem layouts in the process.
10 package PublicInbox::SolverGit;
11 use strict;
12 use v5.10.1;
13 use File::Temp 0.19 (); # 0.19 for ->newdir
14 use Fcntl qw(SEEK_SET);
15 use PublicInbox::Git qw(git_unquote git_quote);
16 use PublicInbox::MsgIter qw(msg_part_text);
17 use PublicInbox::Qspawn;
18 use PublicInbox::Tmpfile;
19 use URI::Escape qw(uri_escape_utf8);
20
21 # POSIX requires _POSIX_ARG_MAX >= 4096, and xargs is required to
22 # subtract 2048 bytes.  We also don't factor in environment variable
23 # headroom into this.
24 use POSIX qw(sysconf _SC_ARG_MAX);
25 my $ARG_SIZE_MAX = (sysconf(_SC_ARG_MAX) || 4096) - 2048;
26 my $OID_MIN = 7;
27
28 # By default, "git format-patch" generates filenames with a four-digit
29 # prefix, so that means 9999 patch series are OK, right? :>
30 # Maybe we can make this configurable, main concern is disk space overhead
31 # for uncompressed patch fragments.  Aside from space, public-inbox-httpd
32 # is otherwise unaffected by having many patches, here, as it can share
33 # work fairly.  Other PSGI servers may have trouble, though.
34 my $MAX_PATCH = 9999;
35
36 my $LF = qr!\r?\n!;
37 my $ANY = qr![^\r\n]+!;
38 my $MODE = '100644|120000|100755';
39 my $FN = qr!(?:("?[^/\n]+/[^\r\n]+)|/dev/null)!;
40 my %BAD_COMPONENT = ('' => 1, '.' => 1, '..' => 1);
41
42 # di = diff info / a hashref with information about a diff ($di):
43 # {
44 #       oid_a => abbreviated pre-image oid,
45 #       oid_b => abbreviated post-image oid,
46 #       tmp => anonymous file handle with the diff,
47 #       hdr_lines => string of various header lines for mode information
48 #       mode_a => original mode of oid_a (string, not integer),
49 #       ibx => PublicInbox::Inbox object containing the diff
50 #       smsg => PublicInbox::Smsg object containing diff
51 #       path_a => pre-image path
52 #       path_b => post-image path
53 #       n => numeric path of the patch (relative to worktree)
54 # }
55
56 sub dbg ($$) {
57         print { $_[0]->{out} } $_[1], "\n" or ERR($_[0], "print(dbg): $!");
58 }
59
60 sub done ($$) {
61         my ($self, $res) = @_;
62         my $ucb = delete($self->{user_cb}) or return;
63         $ucb->($res, $self->{uarg});
64 }
65
66 sub ERR ($$) {
67         my ($self, $err) = @_;
68         print { $self->{out} } $err, "\n";
69         eval { done($self, $err) };
70         die $err;
71 }
72
73 # look for existing objects already in git repos, returns arrayref
74 # if found, number of remaining git coderepos to try if not.
75 sub solve_existing ($$) {
76         my ($self, $want) = @_;
77         my $try = $want->{try_gits} //= [ @{$self->{gits}} ]; # array copy
78         my $git = shift @$try or die 'BUG {try_gits} empty';
79         my $oid_b = $want->{oid_b};
80         my ($oid_full, $type, $size) = $git->check($oid_b);
81
82         # other than {oid_b, try_gits, try_ibxs}
83         my $have_hints = scalar keys %$want > 3;
84         if (defined($type) && (!$have_hints || $type eq 'blob')) {
85                 delete $want->{try_gits};
86                 return [ $git, $oid_full, $type, int($size) ]; # done, success
87         }
88
89         # TODO: deal with 40-char "abbreviations" with future SHA-256 git
90         return scalar(@$try) if length($oid_b) >= 40;
91
92         # parse stderr of "git cat-file --batch-check"
93         my $err = $git->last_check_err;
94         my (@oids) = ($err =~ /\b([a-f0-9]{40,})\s+blob\b/g);
95         return scalar(@$try) unless scalar(@oids);
96
97         # TODO: do something with the ambiguous array?
98         # push @ambiguous, [ $git, @oids ];
99
100         dbg($self, "`$oid_b' ambiguous in " .
101                         join("\n\t", $git->pub_urls($self->{psgi_env}))
102                         . "\n" .
103                         join('', map { "$_ blob\n" } @oids));
104         scalar(@$try);
105 }
106
107 sub extract_diff ($$) {
108         my ($p, $arg) = @_;
109         my ($self, $diffs, $pre, $post, $ibx, $smsg) = @$arg;
110         my ($part) = @$p; # ignore $depth and @idx;
111         my $ct = $part->content_type || 'text/plain';
112         my ($s, undef) = msg_part_text($part, $ct);
113         defined $s or return;
114
115         # Email::MIME::Encodings forces QP to be CRLF upon decoding,
116         # change it back to LF:
117         my $cte = $part->header('Content-Transfer-Encoding') || '';
118         if ($cte =~ /\bquoted-printable\b/i && $part->crlf eq "\n") {
119                 $s =~ s/\r\n/\n/sg;
120         }
121
122
123         $s =~ m!( # $1 start header lines we save for debugging:
124
125                 # everything before ^index is optional, but we don't
126                 # want to match ^(old|copy|rename|deleted|...) unless
127                 # we match /^diff --git/ first:
128                 (?: # begin optional stuff:
129
130                 # try to get the pre-and-post filenames as $2 and $3
131                 (?:^diff\x20--git\x20$FN\x20$FN$LF)
132
133                 (?:^(?: # pass all this to git-apply:
134                         # old mode $4
135                         (?:old\x20mode\x20($MODE))
136                         |
137                         # new mode (possibly new file) ($5)
138                         (?:new\x20(?:file\x20)?mode\x20($MODE))
139                         |
140                         (?:(?:copy|rename|deleted|
141                                 dissimilarity|similarity)$ANY)
142                 )$LF)*
143
144                 )? # end of optional stuff, everything below is required
145
146                 # match the pre and post-image OIDs as $6 $7
147                 ^index\x20(${pre}[a-f0-9]*)\.\.(${post}[a-f0-9]*)
148                         # mode if unchanged $8
149                         (?:\x20(100644|120000|100755))?$LF
150         ) # end of header lines ($1)
151         ( # $9 is the patch body
152                 # "--- a/foo.c" sets pre-filename ($10) in case
153                 # $2 is missing
154                 (?:^---\x20$FN$LF)
155
156                 # "+++ b/foo.c" sets post-filename ($11) in case
157                 # $3 is missing
158                 (?:^\+{3}\x20$FN$LF)
159
160                 # the meat of the diff, including "^\\No newline ..."
161                 # We also allow for totally blank lines w/o leading spaces,
162                 # because git-apply(1) handles that case, too
163                 (?:^(?:[\@\+\x20\-\\][^\n]*|)$LF)+
164         )!smx or return;
165
166         my $di = {
167                 hdr_lines => $1,
168                 oid_a => $6,
169                 oid_b => $7,
170                 mode_a => $5 // $8 // $4, # new (file) // unchanged // old
171         };
172         my $path_a = $2 // $10;
173         my $path_b = $3 // $11;
174         my $patch = $9;
175
176         # don't care for leading 'a/' and 'b/'
177         my (undef, @a) = split(m{/}, git_unquote($path_a)) if defined($path_a);
178         my (undef, @b) = split(m{/}, git_unquote($path_b));
179
180         # get rid of path-traversal attempts and junk patches:
181         # it's junk at best, an attack attempt at worse:
182         foreach (@a, @b) { return if $BAD_COMPONENT{$_} }
183
184         $di->{path_a} = join('/', @a) if @a;
185         $di->{path_b} = join('/', @b);
186
187         my $path = ++$self->{tot};
188         $di->{n} = $path;
189         open(my $tmp, '>:utf8', $self->{tmp}->dirname . "/$path") or
190                 die "open(tmp): $!";
191         print $tmp $di->{hdr_lines}, $patch or die "print(tmp): $!";
192         close $tmp or die "close(tmp): $!";
193
194         # for debugging/diagnostics:
195         $di->{ibx} = $ibx;
196         $di->{smsg} = $smsg;
197
198         push @$diffs, $di;
199 }
200
201 sub path_searchable ($) { defined($_[0]) && $_[0] =~ m!\A[\w/\. \-]+\z! }
202
203 # ".." appears in path names, which confuses Xapian into treating
204 # it as a range query.  So we split on ".." since Xapian breaks
205 # on punctuation anyways:
206 sub filename_query ($) {
207         join('', map { qq( dfn:"$_") } split(/\.\./, $_[0]));
208 }
209
210 sub find_extract_diffs ($$$) {
211         my ($self, $ibx, $want) = @_;
212         my $srch = $ibx->search or return;
213
214         my $post = $want->{oid_b} or die 'BUG: no {oid_b}';
215         $post =~ /\A[a-f0-9]+\z/ or die "BUG: oid_b not hex: $post";
216
217         my $q = "dfpost:$post";
218         my $pre = $want->{oid_a};
219         if (defined $pre && $pre =~ /\A[a-f0-9]+\z/) {
220                 $q .= " dfpre:$pre";
221         } else {
222                 $pre = '[a-f0-9]{7}'; # for $re below
223         }
224
225         my $path_b = $want->{path_b};
226         if (path_searchable($path_b)) {
227                 $q .= filename_query($path_b);
228
229                 my $path_a = $want->{path_a};
230                 if (path_searchable($path_a) && $path_a ne $path_b) {
231                         $q .= filename_query($path_a);
232                 }
233         }
234
235         my $mset = $srch->mset($q, { relevance => 1 });
236         my $diffs = [];
237         for my $smsg (@{$srch->mset_to_smsg($ibx, $mset)}) {
238                 my $eml = $ibx->smsg_eml($smsg) or next;
239                 $eml->each_part(\&extract_diff,
240                                 [$self, $diffs, $pre, $post, $ibx, $smsg], 1);
241         }
242         @$diffs ? $diffs : undef;
243 }
244
245 sub update_index_result ($$) {
246         my ($bref, $self) = @_;
247         my ($qsp, $msg) = delete @$self{qw(-qsp -msg)};
248         if (my $err = $qsp->{err}) {
249                 ERR($self, "git update-index error: $err");
250         }
251         dbg($self, $msg);
252         next_step($self); # onto do_git_apply
253 }
254
255 sub prepare_index ($) {
256         my ($self) = @_;
257         my $patches = $self->{patches};
258         $self->{nr} = 0;
259
260         my $di = $patches->[0] or die 'no patches';
261         my $oid_a = $di->{oid_a} or die '{oid_a} unset';
262         my $existing = $self->{found}->{$oid_a};
263
264         # no index creation for added files
265         $oid_a =~ /\A0+\z/ and return next_step($self);
266
267         die "BUG: $oid_a not not found" unless $existing;
268
269         my $oid_full = $existing->[1];
270         my $path_a = $di->{path_a} or die "BUG: path_a missing for $oid_full";
271         my $mode_a = $di->{mode_a} // '100644';
272
273         my $in = tmpfile("update-index.$oid_full") or die "tmpfile: $!";
274         print $in "$mode_a $oid_full\t$path_a\0" or die "print: $!";
275         $in->flush or die "flush: $!";
276         sysseek($in, 0, SEEK_SET) or die "seek: $!";
277
278         dbg($self, 'preparing index');
279         my $rdr = { 0 => $in };
280         my $cmd = [ qw(git update-index -z --index-info) ];
281         my $qsp = PublicInbox::Qspawn->new($cmd, $self->{git_env}, $rdr);
282         $path_a = git_quote($path_a);
283         $self->{-qsp} = $qsp;
284         $self->{-msg} = "index prepared:\n$mode_a $oid_full\t$path_a";
285         $qsp->psgi_qx($self->{psgi_env}, undef, \&update_index_result, $self);
286 }
287
288 # pure Perl "git init"
289 sub do_git_init ($) {
290         my ($self) = @_;
291         my $dir = $self->{tmp}->dirname;
292         my $git_dir = "$dir/git";
293
294         foreach ('', qw(objects refs objects/info refs/heads)) {
295                 mkdir("$git_dir/$_") or die "mkdir $_: $!";
296         }
297         open my $fh, '>', "$git_dir/config" or die "open git/config: $!";
298         print $fh <<'EOF' or die "print git/config $!";
299 [core]
300         repositoryFormatVersion = 0
301         filemode = true
302         bare = false
303         fsyncObjectfiles = false
304         logAllRefUpdates = false
305 EOF
306         close $fh or die "close git/config: $!";
307
308         open $fh, '>', "$git_dir/HEAD" or die "open git/HEAD: $!";
309         print $fh "ref: refs/heads/master\n" or die "print git/HEAD: $!";
310         close $fh or die "close git/HEAD: $!";
311
312         my $f = 'objects/info/alternates';
313         open $fh, '>', "$git_dir/$f" or die "open: $f: $!";
314         foreach my $git (@{$self->{gits}}) {
315                 print $fh $git->git_path('objects'),"\n" or die "print $f: $!";
316         }
317         close $fh or die "close: $f: $!";
318         my $tmp_git = $self->{tmp_git} = PublicInbox::Git->new($git_dir);
319         $tmp_git->{-tmp} = $self->{tmp};
320         $self->{git_env} = {
321                 GIT_DIR => $git_dir,
322                 GIT_INDEX_FILE => "$git_dir/index",
323         };
324         prepare_index($self);
325 }
326
327 sub do_finish ($) {
328         my ($self) = @_;
329         my ($found, $oid_want) = @$self{qw(found oid_want)};
330         if (my $exists = $found->{$oid_want}) {
331                 return done($self, $exists);
332         }
333
334         # let git disambiguate if oid_want was too short,
335         # but long enough to be unambiguous:
336         my $tmp_git = $self->{tmp_git};
337         if (my @res = $tmp_git->check($oid_want)) {
338                 return done($self, $found->{$res[0]});
339         }
340         if (my $err = $tmp_git->last_check_err) {
341                 dbg($self, $err);
342         }
343         done($self, undef);
344 }
345
346 sub event_step ($) {
347         my ($self) = @_;
348         eval {
349                 # step 1: resolve blobs to patches in the todo queue
350                 if (my $want = pop @{$self->{todo}}) {
351                         # this populates {patches} and {todo}
352                         resolve_patch($self, $want);
353
354                 # step 2: then we instantiate a working tree once
355                 # the todo queue is finally empty:
356                 } elsif (!defined($self->{tmp_git})) {
357                         do_git_init($self);
358
359                 # step 3: apply each patch in the stack
360                 } elsif (scalar @{$self->{patches}}) {
361                         do_git_apply($self);
362
363                 # step 4: execute the user-supplied callback with
364                 # our result: (which may be undef)
365                 # Other steps may call user_cb to terminate prematurely
366                 # on error
367                 } elsif (exists $self->{user_cb}) {
368                         do_finish($self);
369                 } else {
370                         die 'about to call user_cb twice'; # Oops :x
371                 }
372         }; # eval
373         my $err = $@;
374         if ($err) {
375                 $err =~ s/^\s*Exception:\s*//; # bad word to show users :P
376                 dbg($self, "E: $err");
377                 eval { done($self, $err) };
378         }
379 }
380
381 sub next_step ($) {
382         my ($self) = @_;
383         # if outside of public-inbox-httpd, caller is expected to be
384         # looping event_step, anyways
385         my $async = $self->{psgi_env}->{'pi-httpd.async'} or return;
386         # PublicInbox::HTTPD::Async->new
387         $async->(undef, undef, $self);
388 }
389
390 sub mark_found ($$$) {
391         my ($self, $oid, $found_info) = @_;
392         my $found = $self->{found};
393         $found->{$oid} = $found_info;
394         my $oid_cur = $found_info->[1];
395         while ($oid_cur ne $oid && length($oid_cur) > $OID_MIN) {
396                 $found->{$oid_cur} = $found_info;
397                 chop($oid_cur);
398         }
399 }
400
401 sub parse_ls_files ($$) {
402         my ($self, $bref) = @_;
403         my ($qsp, $di) = delete @$self{qw(-qsp -cur_di)};
404         if (my $err = $qsp->{err}) {
405                 die "git ls-files error: $err";
406         }
407
408         my ($line, @extra) = split(/\0/, $$bref);
409         scalar(@extra) and die "BUG: extra files in index: <",
410                                 join('> <', @extra), ">";
411
412         my ($info, $file) = split(/\t/, $line, 2);
413         my ($mode_b, $oid_b_full, $stage) = split(/ /, $info);
414         if ($file ne $di->{path_b}) {
415                 die
416 "BUG: index mismatch: file=$file != path_b=$di->{path_b}";
417         }
418
419         my $tmp_git = $self->{tmp_git} or die 'no git working tree';
420         my (undef, undef, $size) = $tmp_git->check($oid_b_full);
421         defined($size) or die "check $oid_b_full failed";
422
423         dbg($self, "index at:\n$mode_b $oid_b_full\t$file");
424         my $created = [ $tmp_git, $oid_b_full, 'blob', $size, $di ];
425         mark_found($self, $di->{oid_b}, $created);
426         next_step($self); # onto the next patch
427 }
428
429 sub ls_files_result {
430         my ($bref, $self) = @_;
431         eval { parse_ls_files($self, $bref) };
432         ERR($self, $@) if $@;
433 }
434
435 sub oids_same_ish ($$) {
436         (index($_[0], $_[1]) == 0) || (index($_[1], $_[0]) == 0);
437 }
438
439 sub skip_identical ($$$) {
440         my ($self, $patches, $cur_oid_b) = @_;
441         while (my $nxt = $patches->[0]) {
442                 if (oids_same_ish($cur_oid_b, $nxt->{oid_b})) {
443                         dbg($self, 'skipping '.di_url($self, $nxt).
444                                 " for $cur_oid_b");
445                         shift @$patches;
446                 } else {
447                         return;
448                 }
449         }
450 }
451
452 sub apply_result ($$) {
453         my ($bref, $self) = @_;
454         my ($qsp, $di) = delete @$self{qw(-qsp -cur_di)};
455         dbg($self, $$bref);
456         my $patches = $self->{patches};
457         if (my $err = $qsp->{err}) {
458                 my $msg = "git apply error: $err";
459                 my $nxt = $patches->[0];
460                 if ($nxt && oids_same_ish($nxt->{oid_b}, $di->{oid_b})) {
461                         dbg($self, $msg);
462                         dbg($self, 'trying '.di_url($self, $nxt));
463                         return do_git_apply($self);
464                 } else {
465                         ERR($self, $msg);
466                 }
467         } else {
468                 skip_identical($self, $patches, $di->{oid_b});
469         }
470
471         my @cmd = qw(git ls-files -s -z);
472         $qsp = PublicInbox::Qspawn->new(\@cmd, $self->{git_env});
473         $self->{-cur_di} = $di;
474         $self->{-qsp} = $qsp;
475         $qsp->psgi_qx($self->{psgi_env}, undef, \&ls_files_result, $self);
476 }
477
478 sub do_git_apply ($) {
479         my ($self) = @_;
480         my $dn = $self->{tmp}->dirname;
481         my $patches = $self->{patches};
482
483         # we need --ignore-whitespace because some patches are CRLF
484         my @cmd = (qw(git apply --cached --ignore-whitespace
485                         --unidiff-zero --whitespace=warn --verbose));
486         my $len = length(join(' ', @cmd));
487         my $total = $self->{tot};
488         my $di; # keep track of the last one for "git ls-files"
489         my $prv_oid_b;
490
491         do {
492                 my $i = ++$self->{nr};
493                 $di = shift @$patches;
494                 dbg($self, "\napplying [$i/$total] " . di_url($self, $di) .
495                         "\n" . $di->{hdr_lines});
496                 my $path = $di->{n};
497                 $len += length($path) + 1;
498                 push @cmd, $path;
499                 $prv_oid_b = $di->{oid_b};
500         } while (@$patches && $len < $ARG_SIZE_MAX &&
501                  !oids_same_ish($patches->[0]->{oid_b}, $prv_oid_b));
502
503         my $opt = { 2 => 1, -C => $dn, quiet => 1 };
504         my $qsp = PublicInbox::Qspawn->new(\@cmd, $self->{git_env}, $opt);
505         $self->{-cur_di} = $di;
506         $self->{-qsp} = $qsp;
507         $qsp->psgi_qx($self->{psgi_env}, undef, \&apply_result, $self);
508 }
509
510 sub di_url ($$) {
511         my ($self, $di) = @_;
512         # note: we don't pass the PSGI env unconditionally, here,
513         # different inboxes can have different HTTP_HOST on the same instance.
514         my $ibx = $di->{ibx};
515         my $env = $self->{psgi_env} if $ibx eq $self->{inboxes}->[0];
516         my $url = $ibx->base_url($env);
517         my $mid = $di->{smsg}->{mid};
518         defined($url) ? "$url$mid/" : "<$mid>";
519 }
520
521 sub resolve_patch ($$) {
522         my ($self, $want) = @_;
523
524         if (scalar(@{$self->{patches}}) > $MAX_PATCH) {
525                 die "Aborting, too many steps to $self->{oid_want}";
526         }
527
528         # see if we can find the blob in an existing git repo:
529         my $cur_want = $want->{oid_b};
530         if (!$want->{try_ibxs} && $self->{seen_oid}->{$cur_want}++) {
531                 die "Loop detected solving $cur_want\n";
532         }
533         $want->{try_ibxs} //= [ @{$self->{inboxes}} ]; # array copy
534         my $existing = solve_existing($self, $want);
535         if (ref $existing) {
536                 my ($found_git, undef, $type, undef) = @$existing;
537                 dbg($self, "found $cur_want in " .
538                         join(" ||\n\t",
539                                 $found_git->pub_urls($self->{psgi_env})));
540
541                 if ($cur_want eq $self->{oid_want} || $type ne 'blob') {
542                         eval { done($self, $existing) };
543                         die "E: $@" if $@;
544                         return;
545                 }
546                 mark_found($self, $cur_want, $existing);
547                 return next_step($self); # onto patch application
548         } elsif ($existing > 0) {
549                 push @{$self->{todo}}, $want;
550                 return next_step($self); # retry solve_existing
551         } else { # $existing == 0: we may retry if inbox scan (below) fails
552                 delete $want->{try_gits};
553         }
554
555         # scan through inboxes to look for emails which results in
556         # the oid we want:
557         my $ibx = shift(@{$want->{try_ibxs}}) or die 'BUG: {try_ibxs} empty';
558         if (my $diffs = find_extract_diffs($self, $ibx, $want)) {
559                 unshift @{$self->{patches}}, @$diffs;
560                 dbg($self, "found $cur_want in ".
561                         join(" ||\n\t", map { di_url($self, $_) } @$diffs));
562
563                 # good, we can find a path to the oid we $want, now
564                 # lets see if we need to apply more patches:
565                 my $di = $diffs->[0];
566                 my $src = $di->{oid_a};
567
568                 unless ($src =~ /\A0+\z/) {
569                         # we have to solve it using another oid, fine:
570                         my $job = { oid_b => $src, path_b => $di->{path_a} };
571                         push @{$self->{todo}}, $job;
572                 }
573                 return next_step($self); # onto the next todo item
574         }
575
576         if (scalar @{$want->{try_ibxs}}) { # do we have more inboxes to try?
577                 push @{$self->{todo}}, $want;
578                 return next_step($self);
579         }
580
581         if (length($cur_want) > $OID_MIN) { # maybe a shorter OID will work
582                 delete $want->{try_ibxs}; # drop empty arrayref
583                 chop($cur_want);
584                 dbg($self, "retrying $want->{oid_b} as $cur_want");
585                 $want->{oid_b} = $cur_want;
586                 push @{$self->{todo}}, $want;
587                 return next_step($self); # retry with shorter abbrev
588         }
589
590         dbg($self, "could not find $cur_want");
591         eval { done($self, undef) };
592         die "E: $@" if $@;
593 }
594
595 # this API is designed to avoid creating self-referential structures;
596 # so user_cb never references the SolverGit object
597 sub new {
598         my ($class, $ibx, $user_cb, $uarg) = @_;
599
600         bless {
601                 gits => $ibx->{-repo_objs},
602                 user_cb => $user_cb,
603                 uarg => $uarg,
604                 # -cur_di, -qsp, -msg => temporary fields for Qspawn callbacks
605
606                 # TODO: config option for searching related inboxes
607                 inboxes => [ $ibx ],
608         }, $class;
609 }
610
611 # recreate $oid_want using $hints
612 # hints keys: path_a, path_b, oid_a (note: `oid_b' is NOT a hint)
613 # Calls {user_cb} with: [ ::Git object, oid_full, type, size, di (diff_info) ]
614 # with found object, or undef if nothing was found
615 # Calls {user_cb} with a string error on fatal errors
616 sub solve ($$$$$) {
617         my ($self, $env, $out, $oid_want, $hints) = @_;
618
619         # should we even get here? Probably not, but somebody
620         # could be manually typing URLs:
621         return done($self, undef) if $oid_want =~ /\A0+\z/;
622
623         $self->{oid_want} = $oid_want;
624         $self->{out} = $out;
625         $self->{seen_oid} = {};
626         $self->{tot} = 0;
627         $self->{psgi_env} = $env;
628         $self->{todo} = [ { %$hints, oid_b => $oid_want } ];
629         $self->{patches} = []; # [ $di, $di, ... ]
630         $self->{found} = {}; # { abbr => [ ::Git, oid, type, size, $di ] }
631         $self->{tmp} = File::Temp->newdir("solver.$oid_want-XXXXXXXX", TMPDIR => 1);
632
633         dbg($self, "solving $oid_want ...");
634         if (my $async = $env->{'pi-httpd.async'}) {
635                 # PublicInbox::HTTPD::Async->new
636                 $async->(undef, undef, $self);
637         } else {
638                 event_step($self) while $self->{user_cb};
639         }
640 }
641
642 1;