]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiBlob.pm
lei index: new command to index mail w/o git storage
[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 # TODO: this doesn't scan submodules, but maybe it should
6 package PublicInbox::LeiBlob;
7 use strict;
8 use v5.10.1;
9 use parent qw(PublicInbox::IPC);
10 use PublicInbox::Spawn qw(spawn popen_rd which);
11 use PublicInbox::DS;
12
13 sub get_git_dir ($$) {
14         my ($lei, $d) = @_;
15         return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
16
17         my $cmd = [ qw(git rev-parse --git-dir) ];
18         my $opt = { '-C' => $d };
19         if (defined($lei->{opt}->{cwd})) { # --cwd used, report errors
20                 $opt->{2} = $lei->{2};
21         } else { # implicit --cwd, quiet errors
22                 open $opt->{2}, '>', '/dev/null' or die "open /dev/null: $!";
23         }
24         my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, $opt);
25         chomp(my $gd = do { local $/; <$r> });
26         waitpid($pid, 0) == $pid or die "BUG: waitpid @$cmd ($!)";
27         $? == 0 ? $gd : undef;
28 }
29
30 sub solver_user_cb { # called by solver when done
31         my ($res, $self) = @_;
32         my $lei = $self->{lei};
33         my $log_buf = delete $lei->{'log_buf'};
34         $$log_buf =~ s/^/# /sgm;
35         ref($res) eq 'ARRAY' or return $lei->child_error(1 << 8, $$log_buf);
36         $lei->qerr($$log_buf);
37         my ($git, $oid, $type, $size, $di) = @$res;
38         my $gd = $git->{git_dir};
39
40         # don't try to support all the git-show(1) options for non-blob,
41         # this is just a convenience:
42         $type ne 'blob' and
43                 $lei->err("# $oid is a $type of $size bytes in:\n#\t$gd");
44
45         my $cmd = [ 'git', "--git-dir=$gd", 'show', $oid ];
46         my $rdr = { 1 => $lei->{1}, 2 => $lei->{2} };
47         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
48         $lei->child_error($?) if $?;
49 }
50
51 sub do_solve_blob { # via wq_do
52         my ($self) = @_;
53         my $lei = $self->{lei};
54         my $git_dirs = $lei->{opt}->{'git-dir'};
55         my $hints = {};
56         for my $x (qw(oid-a path-a path-b)) {
57                 my $v = $lei->{opt}->{$x} // next;
58                 $x =~ tr/-/_/;
59                 $hints->{$x} = $v;
60         }
61         open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
62         $lei->{log_buf} = \$log_buf;
63         my $git = $lei->{ale}->git;
64         my @rmt = map {
65                 PublicInbox::LeiRemote->new($lei, $_)
66         } $self->{lxs}->remotes;
67         my $solver = bless {
68                 gits => [ map {
69                                 PublicInbox::Git->new($lei->rel2abs($_))
70                         } @$git_dirs ],
71                 user_cb => \&solver_user_cb,
72                 uarg => $self,
73                 # -cur_di, -qsp, -msg => temporary fields for Qspawn callbacks
74                 inboxes => [ $self->{lxs}->locals, @rmt ],
75         }, 'PublicInbox::SolverGit';
76         $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
77         local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
78         $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
79 }
80
81 sub cat_attach_i { # Eml->each_part callback
82         my ($part, $depth, $idx) = @{$_[0]};
83         my $lei = $_[1];
84         my $want = $lei->{-attach_idx} // return;
85         return if $idx ne $want; # [0-9]+(?:\.[0-9]+)+
86         delete $lei->{-attach_idx};
87         $lei->out($part->body);
88 }
89
90 sub extract_attach ($$$) {
91         my ($lei, $blob, $bref) = @_;
92         my $eml = PublicInbox::Eml->new($bref);
93         $eml->each_part(\&cat_attach_i, $lei, 1);
94         my $idx = delete $lei->{-attach_idx};
95         defined($idx) and return $lei->fail(<<EOM);
96 E: attachment $idx not found in $blob
97 EOM
98 }
99
100 sub lei_blob {
101         my ($lei, $blob) = @_;
102         $lei->start_pager if -t $lei->{1};
103         my $opt = $lei->{opt};
104         my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)});
105         my $lxs;
106         if ($blob =~ s/:([0-9\.]+)\z//) {
107                 $lei->{-attach_idx} = $1;
108                 $opt->{mail} = 1;
109         }
110
111         # first, see if it's a blob returned by "lei q" JSON output:k
112         if ($opt->{mail} // ($has_hints ? 0 : 1)) {
113                 if (grep(defined, @$opt{qw(include only)})) {
114                         $lxs = $lei->lxs_prepare;
115                         $lei->ale->refresh_externals($lxs);
116                 }
117                 my $rdr = {};
118                 if ($opt->{mail}) {
119                         open $rdr->{2}, '+>', undef or die "open: $!";
120                 } else {
121                         open $rdr->{2}, '>', '/dev/null' or die "open: $!";
122                 }
123                 my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
124                                 'cat-file', 'blob', $blob ];
125                 if (defined $lei->{-attach_idx}) {
126                         my $fh = popen_rd($cmd, $lei->{env}, $rdr);
127                         require PublicInbox::Eml;
128                         my $buf = do { local $/; <$fh> };
129                         return extract_attach($lei, $blob, \$buf) if close($fh);
130                 } else {
131                         $rdr->{1} = $lei->{1};
132                         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
133                 }
134                 my $ce = $?;
135                 return if $ce == 0;
136                 my $sto = $lei->_lei_store;
137                 my $lms = $sto ? $sto->search->lms : undef;
138                 if (my $bref = $lms ? $lms->local_blob($blob, 1) : undef) {
139                         defined($lei->{-attach_idx}) and
140                                 return extract_attach($lei, $blob, $bref);
141                         return $lei->out($$bref);
142                 } elsif ($opt->{mail}) {
143                         my $eh = $rdr->{2};
144                         seek($eh, 0, 0);
145                         return $lei->child_error($ce, do { local $/; <$eh> });
146                 } # else: fall through to solver below
147         }
148
149         # maybe it's a non-email (code) blob from a coderepo
150         my $git_dirs = $opt->{'git-dir'} //= [];
151         if ($opt->{'cwd'} // 1) {
152                 my $cgd = get_git_dir($lei, '.');
153                 unshift(@$git_dirs, $cgd) if defined $cgd;
154         }
155         return $lei->fail('no --git-dir to try') unless @$git_dirs;
156         unless ($lxs) {
157                 $lxs = $lei->lxs_prepare or return;
158                 $lei->ale->refresh_externals($lxs);
159         }
160         if ($lxs->remotes) {
161                 require PublicInbox::LeiRemote;
162                 $lei->{curl} //= which('curl') or return
163                         $lei->fail('curl needed for', $lxs->remotes);
164                 $lei->_lei_store(1)->write_prepare($lei);
165         }
166         require PublicInbox::SolverGit;
167         my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
168         my ($op_c, $ops) = $lei->workers_start($self, 1);
169         $lei->{wq1} = $self;
170         $self->wq_io_do('do_solve_blob', []);
171         $self->wq_close(1);
172         $op_c->op_wait_event($ops);
173 }
174
175 sub ipc_atfork_child {
176         my ($self) = @_;
177         $self->{lei}->_lei_atfork_child;
178         $self->SUPER::ipc_atfork_child;
179 }
180
181 1;