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 $lei->err("# $oid is a $type of $size bytes in:\n#\t$gd");
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 $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);
81 sub cat_attach_i { # Eml->each_part callback
82 my ($part, $depth, $idx) = @{$_[0]};
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);
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
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)});
106 if ($blob =~ s/:([0-9\.]+)\z//) {
107 $lei->{-attach_idx} = $1;
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, $lei);
119 open $rdr->{2}, '+>', undef or die "open: $!";
121 open $rdr->{2}, '>', '/dev/null' or die "open: $!";
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);
131 $rdr->{1} = $lei->{1};
132 waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
137 if (my $bref = $lms ? $lms->local_blob($blob, 1) : undef) {
138 defined($lei->{-attach_idx}) and
139 return extract_attach($lei, $blob, $bref);
140 return $lei->out($$bref);
141 } elsif ($opt->{mail}) {
144 return $lei->child_error($ce, do { local $/; <$eh> });
145 } # else: fall through to solver below
148 # maybe it's a non-email (code) blob from a coderepo
149 my $git_dirs = $opt->{'git-dir'} //= [];
150 if ($opt->{'cwd'} // 1) {
151 my $cgd = get_git_dir($lei, '.');
152 unshift(@$git_dirs, $cgd) if defined $cgd;
154 return $lei->fail('no --git-dir to try') unless @$git_dirs;
156 $lxs = $lei->lxs_prepare or return;
157 $lei->ale->refresh_externals($lxs, $lei);
160 require PublicInbox::LeiRemote;
161 $lei->{curl} //= which('curl') or return
162 $lei->fail('curl needed for', $lxs->remotes);
163 $lei->_lei_store(1)->write_prepare($lei);
165 require PublicInbox::SolverGit;
166 my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
167 my ($op_c, $ops) = $lei->workers_start($self, 1);
169 $self->wq_io_do('do_solve_blob', []);
171 $lei->wait_wq_events($op_c, $ops);
174 sub ipc_atfork_child {
176 $self->{lei}->_lei_atfork_child;
177 $self->SUPER::ipc_atfork_child;