]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiBlob.pm
lei blob: support --no-mail switch
[public-inbox.git] / lib / PublicInbox / LeiBlob.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # "lei blob $OID" command
5 package PublicInbox::LeiBlob;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Spawn qw(spawn popen_rd);
10 use PublicInbox::DS;
11
12 sub sol_done_wait { # dwaitpid callback
13         my ($arg, $pid) = @_;
14         my (undef, $lei) = @$arg;
15         $lei->child_error($?) if $?;
16         $lei->dclose;
17 }
18
19 sub sol_done { # EOF callback for main daemon
20         my ($lei) = @_;
21         my $sol = delete $lei->{sol} // return $lei->dclose; # already failed
22         $sol->wq_wait_old(\&sol_done_wait, $lei);
23 }
24
25 sub get_git_dir ($) {
26         my ($d) = @_;
27         return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
28
29         my $cmd = [ qw(git rev-parse --git-dir) ];
30         my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, { '-C' => $d });
31         chomp(my $gd = do { local $/; <$r> });
32         waitpid($pid, 0) == $pid or die "BUG: waitpid @$cmd ($!)";
33         $? == 0 ? $gd : undef;
34 }
35
36 sub solver_user_cb { # called by solver when done
37         my ($res, $self) = @_;
38         my $lei = $self->{lei};
39         my $log_buf = delete $lei->{'log_buf'};
40         $$log_buf =~ s/^/# /sgm;
41         ref($res) eq 'ARRAY' or return $lei->fail($$log_buf);
42         $lei->qerr($$log_buf);
43         my ($git, $oid, $type, $size, $di) = @$res;
44         my $gd = $git->{git_dir};
45
46         # don't try to support all the git-show(1) options for non-blob,
47         # this is just a convenience:
48         $type ne 'blob' and
49                 $lei->err("# $oid is a $type of $size bytes in:\n#\t$gd");
50
51         my $cmd = [ 'git', "--git-dir=$gd", 'show', $oid ];
52         my $rdr = { 1 => $lei->{1}, 2 => $lei->{2} };
53         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
54         $lei->child_error($?) if $?;
55 }
56
57 sub do_solve_blob { # via wq_do
58         my ($self) = @_;
59         my $lei = $self->{lei};
60         my $git_dirs = $lei->{opt}->{'git-dir'};
61         my $hints = {};
62         for my $x (qw(oid-a path-a path-b)) {
63                 my $v = $lei->{opt}->{$x} // next;
64                 $x =~ tr/-/_/;
65                 $hints->{$x} = $v;
66         }
67         open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
68         $lei->{log_buf} = \$log_buf;
69         my $git = $lei->ale->git;
70         my $solver = bless {
71                 gits => [ map {
72                                 PublicInbox::Git->new($lei->rel2abs($_))
73                         } @$git_dirs ],
74                 user_cb => \&solver_user_cb,
75                 uarg => $self,
76                 # -cur_di, -qsp, -msg => temporary fields for Qspawn callbacks
77                 inboxes => [ $self->{lxs}->locals ],
78         }, 'PublicInbox::SolverGit';
79         $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
80         local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
81         $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
82 }
83
84 sub lei_blob {
85         my ($lei, $blob) = @_;
86         $lei->start_pager if -t $lei->{1};
87         my $opt = $lei->{opt};
88         my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)});
89
90         # first, see if it's a blob returned by "lei q" JSON output:k
91         if ($opt->{mail} // ($has_hints ? 0 : 1)) {
92                 my $rdr = { 1 => $lei->{1} };
93                 open $rdr->{2}, '>', '/dev/null' or die "open: $!";
94                 my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
95                                 'cat-file', 'blob', $blob ];
96                 waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
97                 return if $? == 0;
98         }
99
100         # maybe it's a non-email (code) blob from a coderepo
101         my $git_dirs = $opt->{'git-dir'} //= [];
102         if ($opt->{'cwd'} // 1) {
103                 my $cgd = get_git_dir('.');
104                 unshift(@$git_dirs, $cgd) if defined $cgd;
105         }
106         my $lxs = $lei->lxs_prepare or return;
107         require PublicInbox::SolverGit;
108         my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
109         my ($op_c, $ops) = $lei->workers_start($self, 'lei_solve', 1,
110                 { '' => [ \&sol_done, $lei ] });
111         $lei->{sol} = $self;
112         $self->wq_io_do('do_solve_blob', []);
113         $self->wq_close(1);
114         $op_c->op_wait_event($ops);
115 }
116
117 sub ipc_atfork_child {
118         my ($self) = @_;
119         $self->{lei}->_lei_atfork_child;
120         $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
121         $self->SUPER::ipc_atfork_child;
122 }
123
124 1;