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 sol_done_wait { # dwaitpid callback
15 my (undef, $lei) = @$arg;
16 $lei->child_error($?) if $?;
20 sub sol_done { # EOF callback for main daemon
22 my $sol = delete $lei->{sol} // return $lei->dclose; # already failed
23 $sol->wq_wait_old(\&sol_done_wait, $lei);
26 sub get_git_dir ($$) {
28 return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
30 my $cmd = [ qw(git rev-parse --git-dir) ];
31 my $opt = { '-C' => $d };
32 if (defined($lei->{opt}->{cwd})) { # --cwd used, report errors
33 $opt->{2} = $lei->{2};
34 } else { # implicit --cwd, quiet errors
35 open $opt->{2}, '>', '/dev/null' or die "open /dev/null: $!";
37 my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, $opt);
38 chomp(my $gd = do { local $/; <$r> });
39 waitpid($pid, 0) == $pid or die "BUG: waitpid @$cmd ($!)";
40 $? == 0 ? $gd : undef;
43 sub solver_user_cb { # called by solver when done
44 my ($res, $self) = @_;
45 my $lei = $self->{lei};
46 my $log_buf = delete $lei->{'log_buf'};
47 $$log_buf =~ s/^/# /sgm;
48 ref($res) eq 'ARRAY' or return $lei->child_error(1 << 8, $$log_buf);
49 $lei->qerr($$log_buf);
50 my ($git, $oid, $type, $size, $di) = @$res;
51 my $gd = $git->{git_dir};
53 # don't try to support all the git-show(1) options for non-blob,
54 # this is just a convenience:
56 $lei->err("# $oid is a $type of $size bytes in:\n#\t$gd");
58 my $cmd = [ 'git', "--git-dir=$gd", 'show', $oid ];
59 my $rdr = { 1 => $lei->{1}, 2 => $lei->{2} };
60 waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
61 $lei->child_error($?) if $?;
64 sub do_solve_blob { # via wq_do
66 my $lei = $self->{lei};
67 my $git_dirs = $lei->{opt}->{'git-dir'};
69 for my $x (qw(oid-a path-a path-b)) {
70 my $v = $lei->{opt}->{$x} // next;
74 open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
75 $lei->{log_buf} = \$log_buf;
76 my $git = $lei->{ale}->git;
78 PublicInbox::LeiRemote->new($lei, $_)
79 } $self->{lxs}->remotes;
82 PublicInbox::Git->new($lei->rel2abs($_))
84 user_cb => \&solver_user_cb,
86 # -cur_di, -qsp, -msg => temporary fields for Qspawn callbacks
87 inboxes => [ $self->{lxs}->locals, @rmt ],
88 }, 'PublicInbox::SolverGit';
89 $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
90 local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
91 $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
95 my ($lei, $blob) = @_;
96 $lei->start_pager if -t $lei->{1};
97 my $opt = $lei->{opt};
98 my $has_hints = grep(defined, @$opt{qw(oid-a path-a path-b)});
101 # first, see if it's a blob returned by "lei q" JSON output:k
102 if ($opt->{mail} // ($has_hints ? 0 : 1)) {
103 if (grep(defined, @$opt{qw(include only)})) {
104 $lxs = $lei->lxs_prepare;
105 $lei->ale->refresh_externals($lxs);
107 my $rdr = { 1 => $lei->{1} };
109 $rdr->{2} = $lei->{2};
111 open $rdr->{2}, '>', '/dev/null' or die "open: $!";
113 my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
114 'cat-file', 'blob', $blob ];
115 waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
117 return $lei->child_error($?) if $opt->{mail};
120 # maybe it's a non-email (code) blob from a coderepo
121 my $git_dirs = $opt->{'git-dir'} //= [];
122 if ($opt->{'cwd'} // 1) {
123 my $cgd = get_git_dir($lei, '.');
124 unshift(@$git_dirs, $cgd) if defined $cgd;
126 return $lei->fail('no --git-dir to try') unless @$git_dirs;
128 $lxs = $lei->lxs_prepare or return;
129 $lei->ale->refresh_externals($lxs);
132 require PublicInbox::LeiRemote;
133 $lei->{curl} //= which('curl') or return
134 $lei->fail('curl needed for', $lxs->remotes);
135 $lei->_lei_store(1)->write_prepare($lei);
137 require PublicInbox::SolverGit;
138 my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
139 my ($op_c, $ops) = $lei->workers_start($self, 'lei_solve', 1,
140 { '' => [ \&sol_done, $lei ] });
142 $self->wq_io_do('do_solve_blob', []);
144 $op_c->op_wait_event($ops);
147 sub ipc_atfork_child {
149 $self->{lei}->_lei_atfork_child;
150 $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
151 $self->SUPER::ipc_atfork_child;