]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiBlob.pm
lei blob: aka "git-show-harder" for blobs
[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 package PublicInbox::LeiBlob;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::IPC);
9 use PublicInbox::Spawn qw(spawn popen_rd);
10 use PublicInbox::DS;
11 use PublicInbox::Eml;
12
13 sub sol_done_wait { # dwaitpid callback
14         my ($arg, $pid) = @_;
15         my (undef, $lei) = @$arg;
16         $lei->child_error($?) if $?;
17         $lei->dclose;
18 }
19
20 sub sol_done { # EOF callback for main daemon
21         my ($lei) = @_;
22         my $sol = delete $lei->{sol} or return;
23         $sol->wq_wait_old(\&sol_done_wait, $lei);
24 }
25
26 sub get_git_dir ($) {
27         my ($d) = @_;
28         return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD";
29
30         my $cmd = [ qw(git rev-parse --git-dir) ];
31         my ($r, $pid) = popen_rd($cmd, {GIT_DIR => undef}, { '-C' => $d });
32         chomp(my $gd = do { local $/; <$r> });
33         waitpid($pid, 0) == $pid or die "BUG: waitpid @$cmd ($!)";
34         $? == 0 ? $gd : undef;
35 }
36
37 sub solver_user_cb { # called by solver when done
38         my ($res, $self) = @_;
39         my $lei = $self->{lei};
40         my $log_buf = delete $lei->{'log_buf'};
41         $$log_buf =~ s/^/# /sgm;
42         ref($res) eq 'ARRAY' or return $lei->fail($$log_buf);
43         $lei->qerr($$log_buf);
44         my ($git, $oid, $type, $size, $di) = @$res;
45         my $gd = $git->{git_dir};
46
47         # don't try to support all the git-show(1) options for non-blob,
48         # this is just a convenience:
49         $type ne 'blob' and
50                 $lei->err("# $oid is a $type of $size bytes in:\n#\t$gd");
51
52         my $cmd = [ 'git', "--git-dir=$gd", 'show', $oid ];
53         my $rdr = { 1 => $lei->{1}, 2 => $lei->{2} };
54         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
55         $lei->child_error($?) if $?;
56 }
57
58 sub do_solve_blob { # via wq_do
59         my ($self) = @_;
60         my $lei = $self->{lei};
61         my $git_dirs = $lei->{opt}->{'git-dir'};
62         my $hints = {};
63         for my $x (qw(oid-a path-a path-b)) {
64                 my $v = $lei->{opt}->{$x} // next;
65                 $x =~ tr/-/_/;
66                 $hints->{$x} = $v;
67         }
68         open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
69         $lei->{log_buf} = \$log_buf;
70         my $git = $lei->ale->git;
71         my $solver = bless {
72                 gits => [ map {
73                                 PublicInbox::Git->new($lei->rel2abs($_))
74                         } @$git_dirs ],
75                 user_cb => \&solver_user_cb,
76                 uarg => $self,
77                 # -cur_di, -qsp, -msg => temporary fields for Qspawn callbacks
78                 inboxes => [ $self->{lxs}->locals ],
79         }, 'PublicInbox::SolverGit';
80         $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
81         local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
82         $solver->solve($lei->{env}, $log, $self->{oid_b}, $hints);
83 }
84
85 sub lei_blob {
86         my ($lei, $blob) = @_;
87         $lei->start_pager if -t $lei->{1};
88
89         # first, see if it's a blob returned by "lei q" JSON output:
90         my $rdr = { 1 => $lei->{1} };
91         open $rdr->{2}, '>', '/dev/null' or die "open: $!";
92         my $cmd = [ 'git', '--git-dir='.$lei->ale->git->{git_dir},
93                         'cat-file', 'blob', $blob ];
94         waitpid(spawn($cmd, $lei->{env}, $rdr), 0);
95         return if $? == 0;
96
97         # maybe it's a non-email (code) blob from a coderepo
98         my $git_dirs = $lei->{opt}->{'git-dir'} //= [];
99         if ($lei->{opt}->{'cwd'} //= 1) {
100                 my $cgd = get_git_dir('.');
101                 unshift(@$git_dirs, $cgd) if defined $cgd;
102         }
103         my $lxs = $lei->lxs_prepare or return;
104         require PublicInbox::SolverGit;
105         my $self = bless { lxs => $lxs, oid_b => $blob }, __PACKAGE__;
106         my $op = $lei->workers_start($self, 'lei_solve', 1,
107                 { '' => [ \&sol_done, $lei ] });
108         $lei->{sol} = $self;
109         $self->wq_io_do('do_solve_blob', []);
110         $self->wq_close(1);
111         while ($op && $op->{sock}) { $op->event_step }
112 }
113
114 sub ipc_atfork_child {
115         my ($self) = @_;
116         $self->{lei}->_lei_atfork_child;
117         $SIG{__WARN__} = PublicInbox::Eml::warn_ignore_cb();
118         $self->SUPER::ipc_atfork_child;
119 }
120
121 1;