-# Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# "Solve" blobs which don't exist in git code repositories by
# local filesystem layouts in the process.
package PublicInbox::SolverGit;
use strict;
-use warnings;
-use 5.010_001;
+use v5.10.1;
use File::Temp 0.19 (); # 0.19 for ->newdir
use Fcntl qw(SEEK_SET);
use PublicInbox::Git qw(git_unquote git_quote);
use PublicInbox::MsgIter qw(msg_part_text);
use PublicInbox::Qspawn;
use PublicInbox::Tmpfile;
+use PublicInbox::GitAsyncCat;
+use PublicInbox::Eml;
use URI::Escape qw(uri_escape_utf8);
# POSIX requires _POSIX_ARG_MAX >= 4096, and xargs is required to
die $err;
}
-# look for existing objects already in git repos
+# look for existing objects already in git repos, returns arrayref
+# if found, number of remaining git coderepos to try if not.
sub solve_existing ($$) {
my ($self, $want) = @_;
+ my $try = $want->{try_gits} //= [ @{$self->{gits}} ]; # array copy
+ my $git = shift @$try or die 'BUG {try_gits} empty';
my $oid_b = $want->{oid_b};
- my $have_hints = scalar keys %$want > 1;
- my @ambiguous; # Array of [ git, $oids]
- foreach my $git (@{$self->{gits}}) {
- my ($oid_full, $type, $size) = $git->check($oid_b);
- if (defined($type) && (!$have_hints || $type eq 'blob')) {
- return [ $git, $oid_full, $type, int($size) ];
- }
+ my ($oid_full, $type, $size) = $git->check($oid_b);
- next if length($oid_b) == 40;
+ # other than {oid_b, try_gits, try_ibxs}
+ my $have_hints = scalar keys %$want > 3;
+ if (defined($type) && (!$have_hints || $type eq 'blob')) {
+ delete $want->{try_gits};
+ return [ $git, $oid_full, $type, int($size) ]; # done, success
+ }
- # parse stderr of "git cat-file --batch-check"
- my $err = $git->last_check_err;
- my (@oids) = ($err =~ /\b([a-f0-9]{40})\s+blob\b/g);
- next unless scalar(@oids);
+ # TODO: deal with 40-char "abbreviations" with future SHA-256 git
+ return scalar(@$try) if length($oid_b) >= 40;
- # TODO: do something with the ambiguous array?
- # push @ambiguous, [ $git, @oids ];
+ # parse stderr of "git cat-file --batch-check"
+ my $err = $git->last_check_err;
+ my (@oids) = ($err =~ /\b([a-f0-9]{40,})\s+blob\b/g);
+ return scalar(@$try) unless scalar(@oids);
- dbg($self, "`$oid_b' ambiguous in " .
- join("\n\t", $git->pub_urls($self->{psgi_env}))
- . "\n" .
- join('', map { "$_ blob\n" } @oids));
- }
- scalar(@ambiguous) ? \@ambiguous : undef;
+ # TODO: do something with the ambiguous array?
+ # push @ambiguous, [ $git, @oids ];
+
+ dbg($self, "`$oid_b' ambiguous in " .
+ join("\n\t", $git->pub_urls($self->{psgi_env}))
+ . "\n" .
+ join('', map { "$_ blob\n" } @oids));
+ scalar(@$try);
}
sub extract_diff ($$) {
my ($p, $arg) = @_;
- my ($self, $diffs, $pre, $post, $ibx, $smsg) = @$arg;
+ my ($self, $want, $smsg) = @$arg;
my ($part) = @$p; # ignore $depth and @idx;
my $ct = $part->content_type || 'text/plain';
my ($s, undef) = msg_part_text($part, $ct);
defined $s or return;
+ my $post = $want->{oid_b};
+ my $pre = $want->{oid_a};
+ if (!defined($pre) || $pre !~ /\A[a-f0-9]+\z/) {
+ $pre = '[a-f0-9]{7}'; # for RE below
+ }
# Email::MIME::Encodings forces QP to be CRLF upon decoding,
# change it back to LF:
close $tmp or die "close(tmp): $!";
# for debugging/diagnostics:
- $di->{ibx} = $ibx;
+ $di->{ibx} = $want->{cur_ibx};
$di->{smsg} = $smsg;
- push @$diffs, $di;
+ push @{$self->{tmp_diffs}}, $di;
}
sub path_searchable ($) { defined($_[0]) && $_[0] =~ m!\A[\w/\. \-]+\z! }
join('', map { qq( dfn:"$_") } split(/\.\./, $_[0]));
}
-sub find_extract_diffs ($$$) {
+sub find_smsgs ($$$) {
my ($self, $ibx, $want) = @_;
- my $srch = $ibx->search or return;
+ my $srch = $ibx->isrch or return;
my $post = $want->{oid_b} or die 'BUG: no {oid_b}';
$post =~ /\A[a-f0-9]+\z/ or die "BUG: oid_b not hex: $post";
my $pre = $want->{oid_a};
if (defined $pre && $pre =~ /\A[a-f0-9]+\z/) {
$q .= " dfpre:$pre";
- } else {
- $pre = '[a-f0-9]{7}'; # for $re below
}
my $path_b = $want->{path_b};
$q .= filename_query($path_a);
}
}
-
- my $msgs = $srch->query($q, { relevance => 1 });
-
- my $diffs = [];
- foreach my $smsg (@$msgs) {
- my $eml = $ibx->smsg_eml($smsg) or next;
- $eml->each_part(\&extract_diff,
- [$self, $diffs, $pre, $post, $ibx, $smsg], 1);
- }
- @$diffs ? $diffs : undef;
+ my $mset = $srch->mset($q, { relevance => 1 });
+ $mset->size ? $srch->mset_to_smsg($ibx, $mset) : undef;
}
sub update_index_result ($$) {
# no index creation for added files
$oid_a =~ /\A0+\z/ and return next_step($self);
- die "BUG: $oid_a not not found" unless $existing;
+ die "BUG: $oid_a 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 $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: $!";
+ sysseek($in, 0, SEEK_SET) or die "seek: $!";
dbg($self, 'preparing index');
my $rdr = { 0 => $in };
defined($url) ? "$url$mid/" : "<$mid>";
}
+sub retry_current {
+ my ($self, $want) = @_;
+ push @{$self->{todo}}, $want;
+ next_step($self); # retry solve_existing
+}
+
+sub try_harder ($$) {
+ my ($self, $want) = @_;
+
+ # do we have more inboxes to try?
+ return retry_current($self, $want) if scalar @{$want->{try_ibxs}};
+
+ my $cur_want = $want->{oid_b};
+ if (length($cur_want) > $OID_MIN) { # maybe a shorter OID will work
+ delete $want->{try_ibxs}; # drop empty arrayref
+ chop($cur_want);
+ dbg($self, "retrying $want->{oid_b} as $cur_want");
+ $want->{oid_b} = $cur_want;
+ return retry_current($self, $want); # retry with shorter abbrev
+ }
+
+ dbg($self, "could not find $cur_want");
+ eval { done($self, undef) };
+ die "E: $@" if $@;
+}
+
+sub extract_diffs_done {
+ my ($self, $want) = @_;
+
+ delete $want->{try_smsgs};
+ delete $want->{cur_ibx};
+
+ my $diffs = delete $self->{tmp_diffs};
+ if (scalar @$diffs) {
+ unshift @{$self->{patches}}, @$diffs;
+ dbg($self, "found $want->{oid_b} in " . join(" ||\n\t",
+ map { di_url($self, $_) } @$diffs));
+
+ # good, we can find a path to the oid we $want, now
+ # lets see if we need to apply more patches:
+ my $di = $diffs->[0];
+ my $src = $di->{oid_a};
+
+ unless ($src =~ /\A0+\z/) {
+ # we have to solve it using another oid, fine:
+ my $job = { oid_b => $src, path_b => $di->{path_a} };
+ push @{$self->{todo}}, $job;
+ }
+ return next_step($self); # onto the next todo item
+ }
+ try_harder($self, $want);
+}
+
+sub extract_diff_async {
+ my ($bref, $oid, $type, $size, $x) = @_;
+ my ($self, $want, $smsg) = @$x;
+ if (defined($oid)) {
+ $smsg->{blob} eq $oid or
+ ERR($self, "BUG: $smsg->{blob} != $oid");
+ PublicInbox::Eml->new($bref)->each_part(\&extract_diff, $x, 1);
+ }
+
+ scalar(@{$want->{try_smsgs}}) ? retry_current($self, $want)
+ : extract_diffs_done($self, $want);
+}
+
sub resolve_patch ($$) {
my ($self, $want) = @_;
+ my $cur_want = $want->{oid_b};
if (scalar(@{$self->{patches}}) > $MAX_PATCH) {
die "Aborting, too many steps to $self->{oid_want}";
}
+ if (my $msgs = $want->{try_smsgs}) {
+ my $smsg = shift @$msgs;
+ if ($self->{psgi_env}->{'pi-httpd.async'}) {
+ return git_async_cat($want->{cur_ibx}->git,
+ $smsg->{blob},
+ \&extract_diff_async,
+ [$self, $want, $smsg]);
+ } else {
+ if (my $eml = $want->{cur_ibx}->smsg_eml($smsg)) {
+ $eml->each_part(\&extract_diff,
+ [ $self, $want, $smsg ], 1);
+ }
+ }
+
+ return scalar(@$msgs) ? retry_current($self, $want)
+ : extract_diffs_done($self, $want);
+ }
+
# see if we can find the blob in an existing git repo:
- my $cur_want = $want->{oid_b};
- if ($self->{seen_oid}->{$cur_want}++) {
+ if (!$want->{try_ibxs} && $self->{seen_oid}->{$cur_want}++) {
die "Loop detected solving $cur_want\n";
}
- if (my $existing = solve_existing($self, $want)) {
+ $want->{try_ibxs} //= [ @{$self->{inboxes}} ]; # array copy
+ my $existing = solve_existing($self, $want);
+ if (ref $existing) {
my ($found_git, undef, $type, undef) = @$existing;
dbg($self, "found $cur_want in " .
join(" ||\n\t",
}
mark_found($self, $cur_want, $existing);
return next_step($self); # onto patch application
+ } elsif ($existing > 0) {
+ return retry_current($self, $want);
+ } else { # $existing == 0: we may retry if inbox scan (below) fails
+ delete $want->{try_gits};
}
# scan through inboxes to look for emails which results in
# the oid we want:
- foreach my $ibx (@{$self->{inboxes}}) {
- my $diffs = find_extract_diffs($self, $ibx, $want) or next;
-
- unshift @{$self->{patches}}, @$diffs;
- dbg($self, "found $cur_want in ".
- join(" ||\n\t", map { di_url($self, $_) } @$diffs));
-
- # good, we can find a path to the oid we $want, now
- # lets see if we need to apply more patches:
- my $di = $diffs->[0];
- my $src = $di->{oid_a};
-
- unless ($src =~ /\A0+\z/) {
- # we have to solve it using another oid, fine:
- my $job = { oid_b => $src, path_b => $di->{path_a} };
- push @{$self->{todo}}, $job;
- }
- return next_step($self); # onto the next todo item
- }
- if (length($cur_want) > $OID_MIN) {
- chop($cur_want);
- dbg($self, "retrying $want->{oid_b} as $cur_want");
- $want->{oid_b} = $cur_want;
- push @{$self->{todo}}, $want;
- return next_step($self); # retry with shorter abbrev
+ my $ibx = shift(@{$want->{try_ibxs}}) or die 'BUG: {try_ibxs} empty';
+ if (my $msgs = find_smsgs($self, $ibx, $want)) {
+ $want->{try_smsgs} = $msgs;
+ $want->{cur_ibx} = $ibx;
+ $self->{tmp_diffs} = [];
+ return retry_current($self, $want);
}
-
- dbg($self, "could not find $cur_want");
- eval { done($self, undef) };
- die "E: $@" if $@;
+ try_harder($self, $want);
}
# this API is designed to avoid creating self-referential structures;
}
# recreate $oid_want using $hints
-# hints keys: path_a, path_b, oid_a
+# hints keys: path_a, path_b, oid_a (note: `oid_b' is NOT a hint)
# Calls {user_cb} with: [ ::Git object, oid_full, type, size, di (diff_info) ]
# with found object, or undef if nothing was found
# Calls {user_cb} with a string error on fatal errors