]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SolverGit.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / SolverGit.pm
index ae3997ca6d06e238176ec797f42965771dc444b6..62b5a3439b2be772b004f13a5a8c2380a171f200 100644 (file)
@@ -1,4 +1,4 @@
-# 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
@@ -111,8 +111,6 @@ sub extract_diff ($$) {
        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/) {
@@ -122,11 +120,12 @@ sub extract_diff ($$) {
        # Email::MIME::Encodings forces QP to be CRLF upon decoding,
        # change it back to LF:
        my $cte = $part->header('Content-Transfer-Encoding') || '';
+       my ($s, undef) = msg_part_text($part, $ct);
+       defined $s or return;
+       delete $part->{bdy};
        if ($cte =~ /\bquoted-printable\b/i && $part->crlf eq "\n") {
                $s =~ s/\r\n/\n/sg;
        }
-
-
        $s =~ m!( # $1 start header lines we save for debugging:
 
                # everything before ^index is optional, but we don't
@@ -169,6 +168,7 @@ sub extract_diff ($$) {
                # because git-apply(1) handles that case, too
                (?:^(?:[\@\+\x20\-\\][^\n]*|)$LF)+
        )!smx or return;
+       undef $s; # free memory
 
        my $di = {
                hdr_lines => $1,
@@ -216,7 +216,7 @@ sub filename_query ($) {
 
 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";
@@ -293,14 +293,21 @@ sub do_git_init ($) {
                mkdir("$git_dir/$_") or die "mkdir $_: $!";
        }
        open my $fh, '>', "$git_dir/config" or die "open git/config: $!";
-       print $fh <<'EOF' or die "print git/config $!";
+       my $first = $self->{gits}->[0];
+       my $fmt = $first->object_format;
+       my $v = defined($$fmt) ? 1 : 0;
+       print $fh <<EOF or die "print git/config $!";
 [core]
-       repositoryFormatVersion = 0
+       repositoryFormatVersion = $v
        filemode = true
        bare = false
        fsyncObjectfiles = false
        logAllRefUpdates = false
 EOF
+       print $fh <<EOM if defined($$fmt);
+[extensions]
+       objectformat = $$fmt
+EOM
        close $fh or die "close git/config: $!";
 
        open $fh, '>', "$git_dir/HEAD" or die "open git/HEAD: $!";
@@ -517,16 +524,16 @@ sub di_url ($$) {
 }
 
 sub retry_current {
-       my ($self, $want) = @_;
-       push @{$_[0]->{todo}}, $_[1];
-       goto \&next_step # retry solve_existing
+       my ($self, $want) = @_;
+       push @{$self->{todo}}, $want;
+       next_step($self); # retry solve_existing
 }
 
-sub try_harder {
+sub try_harder ($$) {
        my ($self, $want) = @_;
 
        # do we have more inboxes to try?
-       goto \&retry_current if scalar @{$want->{try_ibxs}};
+       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
@@ -534,7 +541,7 @@ sub try_harder {
                chop($cur_want);
                dbg($self, "retrying $want->{oid_b} as $cur_want");
                $want->{oid_b} = $cur_want;
-               goto \&retry_current; # retry with shorter abbrev
+               return retry_current($self, $want); # retry with shorter abbrev
        }
 
        dbg($self, "could not find $cur_want");
@@ -564,9 +571,9 @@ sub extract_diffs_done {
                        my $job = { oid_b => $src, path_b => $di->{path_a} };
                        push @{$self->{todo}}, $job;
                }
-               goto \&next_step; # onto the next todo item
+               return next_step($self); # onto the next todo item
        }
-       goto \&try_harder;
+       try_harder($self, $want);
 }
 
 sub extract_diff_async {
@@ -578,9 +585,8 @@ sub extract_diff_async {
                PublicInbox::Eml->new($bref)->each_part(\&extract_diff, $x, 1);
        }
 
-       scalar(@{$want->{try_smsgs}}) ?
-               retry_current($self, $want) :
-               extract_diffs_done($self, $want);
+       scalar(@{$want->{try_smsgs}}) ? retry_current($self, $want)
+                                       : extract_diffs_done($self, $want);
 }
 
 sub resolve_patch ($$) {
@@ -594,8 +600,7 @@ sub resolve_patch ($$) {
        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},
+                       return ibx_async_cat($want->{cur_ibx}, $smsg->{blob},
                                                \&extract_diff_async,
                                                [$self, $want, $smsg]);
                } else {
@@ -605,7 +610,8 @@ sub resolve_patch ($$) {
                        }
                }
 
-               goto(scalar @$msgs ? \&retry_current : \&extract_diffs_done);
+               return scalar(@$msgs) ? retry_current($self, $want)
+                                       : extract_diffs_done($self, $want);
        }
 
        # see if we can find the blob in an existing git repo:
@@ -626,9 +632,9 @@ sub resolve_patch ($$) {
                        return;
                }
                mark_found($self, $cur_want, $existing);
-               goto \&next_step; # onto patch application
+               return next_step($self); # onto patch application
        } elsif ($existing > 0) {
-               goto \&retry_current;
+               return retry_current($self, $want);
        } else { # $existing == 0: we may retry if inbox scan (below) fails
                delete $want->{try_gits};
        }
@@ -640,9 +646,9 @@ sub resolve_patch ($$) {
                $want->{try_smsgs} = $msgs;
                $want->{cur_ibx} = $ibx;
                $self->{tmp_diffs} = [];
-               goto \&retry_current;
+               return retry_current($self, $want);
        }
-       goto \&try_harder;
+       try_harder($self, $want);
 }
 
 # this API is designed to avoid creating self-referential structures;
@@ -681,7 +687,7 @@ sub solve ($$$$$) {
        $self->{todo} = [ { %$hints, oid_b => $oid_want } ];
        $self->{patches} = []; # [ $di, $di, ... ]
        $self->{found} = {}; # { abbr => [ ::Git, oid, type, size, $di ] }
-       $self->{tmp} = File::Temp->newdir("solver.$oid_want-XXXXXXXX", TMPDIR => 1);
+       $self->{tmp} = File::Temp->newdir("solver.$oid_want-XXXX", TMPDIR => 1);
 
        dbg($self, "solving $oid_want ...");
        if (my $async = $env->{'pi-httpd.async'}) {