1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # "lei blob $OID" command
5 # TODO: this doesn't scan submodules, but maybe it should
6 package PublicInbox::LeiBlob;
9 use parent qw(PublicInbox::IPC);
10 use PublicInbox::Spawn qw(spawn popen_rd which);
13 sub get_git_dir ($$) {
15 return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
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: $!";
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;
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};
40 # don't try to support all the git-show(1) options for non-blob,
41 # this is just a convenience:
43 warn "# $oid is a $type of $size bytes in:\n#\t$gd\n";
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 $?;
51 sub do_solve_blob { # via wq_do
53 my $lei = $self->{lei};
54 my $git_dirs = $lei->{opt}->{'git-dir'};
56 for my $x (qw(oid-a path-a path-b)) {
57 my $v = $lei->{opt}->{$x} // next;
61 open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
62 $lei->{log_buf} = \$log_buf;
63 my $git = $lei->{ale}->git;
65 PublicInbox::LeiRemote->new($lei, $_)
66 } $self->{lxs}->remotes;
69 PublicInbox::Git->new($lei->rel2abs($_))
71 user_cb => \&solver_user_cb,
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);
80 sub cat_attach_i { # Eml->each_part callback
81 my ($part, $depth, $idx) = @{$_[0]};
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);
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
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)});
105 if ($blob =~ s/:([0-9\.]+)\z//) {
106 $lei->{-attach_idx} = $1;
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);
118 open $rdr->{2}, '+>', undef or die "open: $!";
120 open $rdr->{2}, '>', '/dev/null' or die "open: $!";
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);
130 $rdr->{1} = $lei->{1};
131 waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
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}) {
143 return $lei->child_error($ce, do { local $/; <$eh> });
144 } # else: fall through to solver below
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;
153 return $lei->fail('no --git-dir to try') unless @$git_dirs;
155 $lxs = $lei->lxs_prepare or return;
156 $lei->ale->refresh_externals($lxs, $lei);
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);
164 require PublicInbox::SolverGit;
165 my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
166 my ($op_c, $ops) = $lei->workers_start($self, 1);
168 $self->wq_io_do('do_solve_blob', []);
170 $lei->wait_wq_events($op_c, $ops);
173 sub ipc_atfork_child {
175 $self->{lei}->_lei_atfork_child;
176 $self->SUPER::ipc_atfork_child;