]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiBlob.pm
lei: simplify workers_start API
[public-inbox.git] / lib / PublicInbox / LeiBlob.pm
index ed0754a3add5500de63ecda34f2ae0661bdf4bda..710430a2c1981e2170c607d83a4cb0c782c54996 100644 (file)
@@ -10,25 +10,18 @@ use parent qw(PublicInbox::IPC);
 use PublicInbox::Spawn qw(spawn popen_rd which);
 use PublicInbox::DS;
 
-sub sol_done_wait { # dwaitpid callback
-       my ($arg, $pid) = @_;
-       my (undef, $lei) = @$arg;
-       $lei->child_error($?) if $?;
-       $lei->dclose;
-}
-
-sub sol_done { # EOF callback for main daemon
-       my ($lei) = @_;
-       my $sol = delete $lei->{sol} // return $lei->dclose; # already failed
-       $sol->wq_wait_old(\&sol_done_wait, $lei);
-}
-
-sub get_git_dir ($) {
-       my ($d) = @_;
+sub get_git_dir ($$) {
+       my ($lei, $d) = @_;
        return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
 
        my $cmd = [ qw(git rev-parse --git-dir) ];
-       my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, { '-C' => $d });
+       my $opt = { '-C' => $d };
+       if (defined($lei->{opt}->{cwd})) { # --cwd used, report errors
+               $opt->{2} = $lei->{2};
+       } else { # implicit --cwd, quiet errors
+               open $opt->{2}, '>', '/dev/null' or die "open /dev/null: $!";
+       }
+       my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, $opt);
        chomp(my $gd = do { local $/; <$r> });
        waitpid($pid, 0) == $pid or die "BUG: waitpid @$cmd ($!)";
        $? == 0 ? $gd : undef;
@@ -85,12 +78,25 @@ sub do_solve_blob { # via wq_do
        $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
 }
 
+sub cat_attach_i { # Eml->each_part callback
+       my ($part, $depth, $idx) = @{$_[0]};
+       my $lei = $_[1];
+       my $want = $lei->{-attach_idx} // return;
+       return if $idx ne $want; # [0-9]+(?:\.[0-9]+)+
+       delete $lei->{-attach_idx};
+       $lei->out($part->body);
+}
+
 sub lei_blob {
        my ($lei, $blob) = @_;
        $lei->start_pager if -t $lei->{1};
        my $opt = $lei->{opt};
        my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)});
        my $lxs;
+       if ($blob =~ s/:([0-9\.]+)\z//) {
+               $lei->{-attach_idx} = $1;
+               $opt->{mail} = 1;
+       }
 
        # first, see if it's a blob returned by "lei q" JSON output:k
        if ($opt->{mail} // ($has_hints ? 0 : 1)) {
@@ -98,7 +104,7 @@ sub lei_blob {
                        $lxs = $lei->lxs_prepare;
                        $lei->ale->refresh_externals($lxs);
                }
-               my $rdr = { 1 => $lei->{1} };
+               my $rdr = {};
                if ($opt->{mail}) {
                        $rdr->{2} = $lei->{2};
                } else {
@@ -106,7 +112,22 @@ sub lei_blob {
                }
                my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
                                'cat-file', 'blob', $blob ];
-               waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
+               if (defined $lei->{-attach_idx}) {
+                       my $fh = popen_rd($cmd, $lei->{env}, $rdr);
+                       require PublicInbox::Eml;
+                       my $str = do { local $/; <$fh> };
+                       if (close $fh) {
+                               my $eml = PublicInbox::Eml->new(\$str);
+                               $eml->each_part(\&cat_attach_i, $lei, 1);
+                               my $idx = delete $lei->{-attach_idx};
+                               defined($idx) and return $lei->fail(<<EOM);
+E: attachment $idx not found in $blob
+EOM
+                       }
+               } else {
+                       $rdr->{1} = $lei->{1};
+                       waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
+               }
                return if $? == 0;
                return $lei->child_error($?) if $opt->{mail};
        }
@@ -114,7 +135,7 @@ sub lei_blob {
        # maybe it's a non-email (code) blob from a coderepo
        my $git_dirs = $opt->{'git-dir'} //= [];
        if ($opt->{'cwd'} // 1) {
-               my $cgd = get_git_dir('.');
+               my $cgd = get_git_dir($lei, '.');
                unshift(@$git_dirs, $cgd) if defined $cgd;
        }
        return $lei->fail('no --git-dir to try') unless @$git_dirs;
@@ -130,9 +151,8 @@ sub lei_blob {
        }
        require PublicInbox::SolverGit;
        my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
-       my ($op_c, $ops) = $lei->workers_start($self, 'lei_solve', 1,
-               { '' => [ \&sol_done, $lei ] });
-       $lei->{sol} = $self;
+       my ($op_c, $ops) = $lei->workers_start($self, 1);
+       $lei->{wq1} = $self;
        $self->wq_io_do('do_solve_blob', []);
        $self->wq_close(1);
        $op_c->op_wait_event($ops);
@@ -141,7 +161,6 @@ sub lei_blob {
 sub ipc_atfork_child {
        my ($self) = @_;
        $self->{lei}->_lei_atfork_child;
-       $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
        $self->SUPER::ipc_atfork_child;
 }