]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiBlob.pm
ff079e6584413d7b54aefba64cc2ca793f7ff2e1
[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 _lei_wq_eof { # EOF callback for main daemon
14         my ($lei) = @_;
15         my $sol = delete $lei->{sol} // return $lei->dclose; # already failed
16         $sol->wq_wait_old($lei->can('wq_done_wait'), $lei);
17 }
18
19 sub get_git_dir ($$) {
20         my ($lei, $d) = @_;
21         return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
22
23         my $cmd = [ qw(git rev-parse --git-dir) ];
24         my $opt = { '-C' => $d };
25         if (defined($lei->{opt}->{cwd})) { # --cwd used, report errors
26                 $opt->{2} = $lei->{2};
27         } else { # implicit --cwd, quiet errors
28                 open $opt->{2}, '>', '/dev/null' or die "open /dev/null: $!";
29         }
30         my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, $opt);
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->child_error(1 << 8, $$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 @rmt = map {
71                 PublicInbox::LeiRemote->new($lei, $_)
72         } $self->{lxs}->remotes;
73         my $solver = bless {
74                 gits => [ map {
75                                 PublicInbox::Git->new($lei->rel2abs($_))
76                         } @$git_dirs ],
77                 user_cb => \&solver_user_cb,
78                 uarg => $self,
79                 # -cur_di, -qsp, -msg => temporary fields for Qspawn callbacks
80                 inboxes => [ $self->{lxs}->locals, @rmt ],
81         }, 'PublicInbox::SolverGit';
82         $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
83         local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
84         $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
85 }
86
87 sub cat_attach_i { # Eml->each_part callback
88         my ($part, $depth, $idx) = @{$_[0]};
89         my $lei = $_[1];
90         my $want = $lei->{-attach_idx} // return;
91         return if $idx ne $want; # [0-9]+(?:\.[0-9]+)+
92         delete $lei->{-attach_idx};
93         $lei->out($part->body);
94 }
95
96 sub lei_blob {
97         my ($lei, $blob) = @_;
98         $lei->start_pager if -t $lei->{1};
99         my $opt = $lei->{opt};
100         my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)});
101         my $lxs;
102         if ($blob =~ s/:([0-9\.]+)\z//) {
103                 $lei->{-attach_idx} = $1;
104                 $opt->{mail} = 1;
105         }
106
107         # first, see if it's a blob returned by "lei q" JSON output:k
108         if ($opt->{mail} // ($has_hints ? 0 : 1)) {
109                 if (grep(defined, @$opt{qw(include only)})) {
110                         $lxs = $lei->lxs_prepare;
111                         $lei->ale->refresh_externals($lxs);
112                 }
113                 my $rdr = {};
114                 if ($opt->{mail}) {
115                         $rdr->{2} = $lei->{2};
116                 } else {
117                         open $rdr->{2}, '>', '/dev/null' or die "open: $!";
118                 }
119                 my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
120                                 'cat-file', 'blob', $blob ];
121                 if (defined $lei->{-attach_idx}) {
122                         my $fh = popen_rd($cmd, $lei->{env}, $rdr);
123                         require PublicInbox::Eml;
124                         my $str = do { local $/; <$fh> };
125                         if (close $fh) {
126                                 my $eml = PublicInbox::Eml->new(\$str);
127                                 $eml->each_part(\&cat_attach_i, $lei, 1);
128                                 my $idx = delete $lei->{-attach_idx};
129                                 defined($idx) and return $lei->fail(<<EOM);
130 E: attachment $idx not found in $blob
131 EOM
132                         }
133                 } else {
134                         $rdr->{1} = $lei->{1};
135                         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
136                 }
137                 return if $? == 0;
138                 return $lei->child_error($?) if $opt->{mail};
139         }
140
141         # maybe it's a non-email (code) blob from a coderepo
142         my $git_dirs = $opt->{'git-dir'} //= [];
143         if ($opt->{'cwd'} // 1) {
144                 my $cgd = get_git_dir($lei, '.');
145                 unshift(@$git_dirs, $cgd) if defined $cgd;
146         }
147         return $lei->fail('no --git-dir to try') unless @$git_dirs;
148         unless ($lxs) {
149                 $lxs = $lei->lxs_prepare or return;
150                 $lei->ale->refresh_externals($lxs);
151         }
152         if ($lxs->remotes) {
153                 require PublicInbox::LeiRemote;
154                 $lei->{curl} //= which('curl') or return
155                         $lei->fail('curl needed for', $lxs->remotes);
156                 $lei->_lei_store(1)->write_prepare($lei);
157         }
158         require PublicInbox::SolverGit;
159         my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
160         my ($op_c, $ops) = $lei->workers_start($self, 'lei-blob', 1);
161         $lei->{sol} = $self;
162         $self->wq_io_do('do_solve_blob', []);
163         $self->wq_close(1);
164         $op_c->op_wait_event($ops);
165 }
166
167 sub ipc_atfork_child {
168         my ($self) = @_;
169         $self->{lei}->_lei_atfork_child;
170         $self->SUPER::ipc_atfork_child;
171 }
172
173 1;