]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SolverGit.pm
solver: check one git coderepo and inbox at a time
[public-inbox.git] / lib / PublicInbox / SolverGit.pm
index bf806f65661a5d6637ef89401f34104d25c7ffde..12024dbcc6dfa143b444e0e24959e1b405991a0c 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2020 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
@@ -9,12 +9,11 @@
 # 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_iter msg_part_text);
+use PublicInbox::MsgIter qw(msg_part_text);
 use PublicInbox::Qspawn;
 use PublicInbox::Tmpfile;
 use URI::Escape qw(uri_escape_utf8);
@@ -34,6 +33,12 @@ my $OID_MIN = 7;
 # work fairly.  Other PSGI servers may have trouble, though.
 my $MAX_PATCH = 9999;
 
+my $LF = qr!\r?\n!;
+my $ANY = qr![^\r\n]+!;
+my $MODE = '100644|120000|100755';
+my $FN = qr!(?:("?[^/\n]+/[^\r\n]+)|/dev/null)!;
+my %BAD_COMPONENT = ('' => 1, '.' => 1, '..' => 1);
+
 # di = diff info / a hashref with information about a diff ($di):
 # {
 #      oid_a => abbreviated pre-image oid,
@@ -42,7 +47,7 @@ my $MAX_PATCH = 9999;
 #      hdr_lines => string of various header lines for mode information
 #      mode_a => original mode of oid_a (string, not integer),
 #      ibx => PublicInbox::Inbox object containing the diff
-#      smsg => PublicInbox::SearchMsg object containing diff
+#      smsg => PublicInbox::Smsg object containing diff
 #      path_a => pre-image path
 #      path_b => post-image path
 #      n => numeric path of the patch (relative to worktree)
@@ -65,34 +70,38 @@ sub ERR ($$) {
        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 ($$) {
@@ -110,10 +119,6 @@ sub extract_diff ($$) {
                $s =~ s/\r\n/\n/sg;
        }
 
-       state $LF = qr!\r?\n!;
-       state $ANY = qr![^\r\n]+!;
-       state $MODE = '100644|120000|100755';
-       state $FN = qr!(?:("?[^/\n]+/[^\r\n]+)|/dev/null)!;
 
        $s =~ m!( # $1 start header lines we save for debugging:
 
@@ -174,8 +179,7 @@ sub extract_diff ($$) {
 
        # get rid of path-traversal attempts and junk patches:
        # it's junk at best, an attack attempt at worse:
-       state $bad_component = { map { $_ => 1 } ('', '.', '..') };
-       foreach (@a, @b) { return if $bad_component->{$_} }
+       foreach (@a, @b) { return if $BAD_COMPONENT{$_} }
 
        $di->{path_a} = join('/', @a) if @a;
        $di->{path_b} = join('/', @b);
@@ -228,12 +232,11 @@ sub find_extract_diffs ($$$) {
                }
        }
 
-       my $msgs = $srch->query($q, { relevance => 1 });
-
+       my $mset = $srch->mset($q, { relevance => 1 });
        my $diffs = [];
-       foreach my $smsg (@$msgs) {
-               $ibx->smsg_mime($smsg) or next;
-               msg_iter(delete $smsg->{mime}, \&extract_diff,
+       for my $smsg (@{$srch->mset_to_smsg($ibx, $mset)}) {
+               my $eml = $ibx->smsg_eml($smsg) or next;
+               $eml->each_part(\&extract_diff,
                                [$self, $diffs, $pre, $post, $ibx, $smsg], 1);
        }
        @$diffs ? $diffs : undef;
@@ -270,7 +273,7 @@ sub prepare_index ($) {
        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 };
@@ -524,10 +527,12 @@ sub resolve_patch ($$) {
 
        # 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",
@@ -540,13 +545,17 @@ sub resolve_patch ($$) {
                }
                mark_found($self, $cur_want, $existing);
                return next_step($self); # onto patch application
+       } elsif ($existing > 0) {
+               push @{$self->{todo}}, $want;
+               return next_step($self); # retry solve_existing
+       } 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;
-
+       my $ibx = shift(@{$want->{try_ibxs}}) or die 'BUG: {try_ibxs} empty';
+       if (my $diffs = find_extract_diffs($self, $ibx, $want)) {
                unshift @{$self->{patches}}, @$diffs;
                dbg($self, "found $cur_want in ".
                        join(" ||\n\t", map { di_url($self, $_) } @$diffs));
@@ -563,7 +572,14 @@ sub resolve_patch ($$) {
                }
                return next_step($self); # onto the next todo item
        }
-       if (length($cur_want) > $OID_MIN) {
+
+       if (scalar @{$want->{try_ibxs}}) { # do we have more inboxes to try?
+               push @{$self->{todo}}, $want;
+               return next_step($self);
+       }
+
+       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;
@@ -593,7 +609,7 @@ sub new {
 }
 
 # 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