]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiBlob.pm
imap+nntp: share COMPRESS implementation
[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(0, $$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                 warn "# $oid is a $type of $size bytes in:\n#\t$gd\n";
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         local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
77         $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
78 }
79
80 sub cat_attach_i { # Eml->each_part callback
81         my ($part, $depth, $idx) = @{$_[0]};
82         my $lei = $_[1];
83         my $want = $lei->{-attach_idx} // return;
84         return if $idx ne $want; # [0-9]+(?:\.[0-9]+)+
85         delete $lei->{-attach_idx};
86         $lei->out($part->body);
87 }
88
89 sub extract_attach ($$$) {
90         my ($lei, $blob, $bref) = @_;
91         my $eml = PublicInbox::Eml->new($bref);
92         $eml->each_part(\&cat_attach_i, $lei, 1);
93         my $idx = delete $lei->{-attach_idx};
94         defined($idx) and return $lei->fail(<<EOM);
95 E: attachment $idx not found in $blob
96 EOM
97 }
98
99 sub lei_blob {
100         my ($lei, $blob) = @_;
101         $lei->start_pager if -t $lei->{1};
102         my $opt = $lei->{opt};
103         my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)});
104         my $lxs;
105         if ($blob =~ s/:([0-9\.]+)\z//) {
106                 $lei->{-attach_idx} = $1;
107                 $opt->{mail} = 1;
108         }
109
110         # first, see if it's a blob returned by "lei q" JSON output:k
111         if ($opt->{mail} // ($has_hints ? 0 : 1)) {
112                 if (grep(defined, @$opt{qw(include only)})) {
113                         $lxs = $lei->lxs_prepare;
114                         $lei->ale->refresh_externals($lxs, $lei);
115                 }
116                 my $rdr = {};
117                 if ($opt->{mail}) {
118                         open $rdr->{2}, '+>', undef or die "open: $!";
119                 } else {
120                         open $rdr->{2}, '>', '/dev/null' or die "open: $!";
121                 }
122                 my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
123                                 'cat-file', 'blob', $blob ];
124                 if (defined $lei->{-attach_idx}) {
125                         my $fh = popen_rd($cmd, $lei->{env}, $rdr);
126                         require PublicInbox::Eml;
127                         my $buf = do { local $/; <$fh> };
128                         return extract_attach($lei, $blob, \$buf) if close($fh);
129                 } else {
130                         $rdr->{1} = $lei->{1};
131                         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
132                 }
133                 my $ce = $?;
134                 return if $ce == 0;
135                 my $lms = $lei->lms;
136                 if (my $bref = $lms ? $lms->local_blob($blob, 1) : undef) {
137                         defined($lei->{-attach_idx}) and
138                                 return extract_attach($lei, $blob, $bref);
139                         return $lei->out($$bref);
140                 } elsif ($opt->{mail}) {
141                         my $eh = $rdr->{2};
142                         seek($eh, 0, 0);
143                         return $lei->child_error($ce, do { local $/; <$eh> });
144                 } # else: fall through to solver below
145         }
146
147         # maybe it's a non-email (code) blob from a coderepo
148         my $git_dirs = $opt->{'git-dir'} //= [];
149         if ($opt->{'cwd'} // 1) {
150                 my $cgd = get_git_dir($lei, '.');
151                 unshift(@$git_dirs, $cgd) if defined $cgd;
152         }
153         return $lei->fail('no --git-dir to try') unless @$git_dirs;
154         unless ($lxs) {
155                 $lxs = $lei->lxs_prepare or return;
156                 $lei->ale->refresh_externals($lxs, $lei);
157         }
158         if ($lxs->remotes) {
159                 require PublicInbox::LeiRemote;
160                 $lei->{curl} //= which('curl') or return
161                         $lei->fail('curl needed for', $lxs->remotes);
162                 $lei->_lei_store(1)->write_prepare($lei);
163         }
164         require PublicInbox::SolverGit;
165         my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
166         my ($op_c, $ops) = $lei->workers_start($self, 1);
167         $lei->{wq1} = $self;
168         $self->wq_io_do('do_solve_blob', []);
169         $self->wq_close;
170         $lei->wait_wq_events($op_c, $ops);
171 }
172
173 sub ipc_atfork_child {
174         my ($self) = @_;
175         $self->{lei}->_lei_atfork_child;
176         $self->SUPER::ipc_atfork_child;
177 }
178
179 1;