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 # The "lei rediff" sub-command, regenerates diffs with new options
5 package PublicInbox::LeiRediff;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
9 use File::Temp 0.19 (); # 0.19 for ->newdir
10 use PublicInbox::Spawn qw(spawn which);
11 use PublicInbox::MsgIter qw(msg_part_text);
12 use PublicInbox::ViewDiff;
13 use PublicInbox::LeiBlob;
14 use PublicInbox::Git qw(git_quote git_unquote);
15 use PublicInbox::Import;
17 use PublicInbox::SolverGit;
19 sub rediff_user_cb { # called by solver when done
20 my ($res, $self) = @_;
21 my $lei = $self->{lei};
22 my $log_buf = delete $lei->{log_buf};
23 $$log_buf =~ s/^/# /sgm;
24 ref($res) eq 'ARRAY' or return $lei->child_error(1 << 8, $$log_buf);
25 $lei->qerr($$log_buf);
26 my ($git, $oid, $type, $size, $di) = @$res;
27 my $oid_want = delete $self->{cur_oid_want};
29 # don't try to support all the git-show(1) options for non-blob,
30 # this is just a convenience:
31 $type ne 'blob' and return $lei->err(<<EOF);
32 # $oid is a $type of $size bytes in:
33 # $git->{git_dir} (wanted: $oid_want)
35 $self->{blob}->{$oid_want} = $oid;
36 push @{$self->{gits}}, $git if $git->{-tmp};
39 # returns a full blob for oid_want
41 my ($self, $oid_want, $hints) = @_;
42 return if $oid_want =~ /\A0+\z/;
43 $self->{cur_oid_want} = $oid_want;
45 gits => $self->{gits},
46 user_cb => \&rediff_user_cb,
48 inboxes => [ $self->{lxs}->locals, @{$self->{rmt}} ],
49 }, 'PublicInbox::SolverGit';
50 open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
51 $self->{lei}->{log_buf} = \$log_buf;
52 local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
53 $solver->solve($self->{lei}->{env}, $log, $oid_want, $hints);
54 $self->{blob}->{$oid_want}; # full OID
58 my ($self, $ctxq) = @_;
60 my $blob = $self->{blob};
65 committer <c@s> 0 +0000
70 my $lei = $self->{lei};
71 my $wait = delete($self->{-do_done}) ? $lei->{sto}->ipc_do('done') : 0;
72 while (my ($oid_a, $oid_b, $pa, $pb) = splice(@$ctxq, 0, 4)) {
73 my $xa = $blob->{$oid_a} //= solve_1($self, $oid_a,
75 my $xb = $blob->{$oid_b} //= solve_1($self, $oid_b, {
80 $ta .= "M 100644 $xa ".git_quote($pa)."\n" if $xa;
81 $tb .= "M 100644 $xb ".git_quote($pb)."\n" if $xb;
83 my $rw = $self->{gits}->[-1]; # has all known alternates
85 my $d = "$self->{rdtmp}/for_tree.git";
86 -d $d or PublicInbox::Import::init_bare($d);
87 my $f = "$d/objects/info/alternates"; # always overwrite
88 open my $fh, '>', $f or die "open $f: $!";
89 for my $git (@{$self->{gits}}) {
90 print $fh $git->git_path('objects'),"\n";
92 close $fh or die "close $f: $!";
93 $rw = PublicInbox::Git->new($d);
95 pipe(my ($r, $w)) or die "pipe: $!";
96 my $pid = spawn(['git', "--git-dir=$rw->{git_dir}",
97 qw(fast-import --quiet --done --date-format=raw)],
98 $lei->{env}, { 2 => $lei->{2}, 0 => $r });
99 close $r or die "close r fast-import: $!";
100 print $w $ta, "\n", $tb, "\ndone\n" or die "print fast-import: $!";
101 close $w or die "close w fast-import: $!";
103 die "fast-import failed: \$?=$?" if $?;
106 my $opt = $lei->{opt};
107 push @cmd, '--'.($opt->{color} && !$opt->{'no-color'} ? '' : 'no-').
109 for my $o (@PublicInbox::LEI::diff_opt) {
110 $o =~ s/\|([a-z0-9])\b//i; # remove single char short option
112 if ($o =~ s/=[is]@\z//) {
113 my $v = $opt->{$o} or next;
114 push @cmd, map { $c ? "-$c$_" : "--$o=$_" } @$v;
115 } elsif ($o =~ s/=[is]\z//) {
116 my $v = $opt->{$o} // next;
117 push @cmd, $c ? "-$c$v" : "--$o=$v";
118 } elsif ($o =~ s/:[is]\z//) {
119 my $v = $opt->{$o} // next;
120 push @cmd, $c ? "-$c$v" :
121 ($v eq '' ? "--$o" : "--$o=$v");
122 } elsif ($o =~ s/!\z//) {
123 my $v = $opt->{$o} // next;
124 push @cmd, $v ? "--$o" : "--no-$o";
125 } elsif ($opt->{$o}) {
126 push @cmd, $c ? "-$c" : "--$o";
129 $lei->qerr("# git @cmd");
131 unshift @cmd, 'git', "--git-dir=$rw->{git_dir}";
132 $pid = spawn(\@cmd, $lei->{env}, { 2 => $lei->{2}, 1 => $lei->{1} });
134 $lei->child_error($?) if $?; # for git diff --exit-code
137 sub extract_oids { # Eml each_part callback
138 my ($ary, $self) = @_;
139 my ($p, undef, $idx) = @$ary;
140 $self->{lei}->out($p->header_obj->as_string, "\n");
141 my ($s, undef) = msg_part_text($p, $p->content_type || 'text/plain');
142 defined $s or return;
143 my @top = split($PublicInbox::ViewDiff::EXTRACT_DIFFS, $s);
145 my $blobs = $self->{blobs}; # blobs to resolve
147 while (defined(my $x = shift @top)) {
148 if (scalar(@top) >= 4 &&
149 $top[1] =~ $PublicInbox::ViewDiff::IS_OID &&
150 $top[0] =~ $PublicInbox::ViewDiff::IS_OID) {
151 my ($oid_a, $oid_b, $pa, $pb) = splice(@top, 0, 4);
152 $pa eq '/dev/null' or
153 $pa = (split(m'/', git_unquote($pa), 2))[1];
154 $pb eq '/dev/null' or
155 $pb = (split(m'/', git_unquote($pb), 2))[1];
156 $blobs->{$oid_a} //= undef;
157 $blobs->{$oid_b} //= undef;
158 push @$ctxq, $oid_a, $oid_b, $pa, $pb;
161 for (split(/^/sm, $x)) {
162 if (/\A-- \r?\n/s) { # email sig starts
164 $ctxq = diff_ctxq($self, $ctxq);
165 } elsif ($ctxq && (/\A[\+\- ]/ || /\A@@ / ||
166 # allow totally blank lines w/o leading
167 # SP, git-apply does:
174 $self->{lei}->out(@out) if @out;
176 $ctxq = diff_ctxq($self, $ctxq);
177 $self->{lei}->out($x);
180 $ctxq = diff_ctxq($self, $ctxq);
183 sub input_eml_cb { # callback for all emails
184 my ($self, $eml) = @_;
185 $self->{lei}->{sto}->ipc_do('add_eml', $eml);
186 $self->{-do_done} = 1;
187 $eml->each_part(\&extract_oids, $self, 1);
191 my ($lei, @inputs) = @_;
192 $lei->_lei_store(1)->write_prepare($lei);
193 $lei->{opt}->{stdin} = 1 if !@inputs;
194 $lei->{opt}->{'in-format'} //= 'eml';
195 # maybe it's a non-email (code) blob from a coderepo
196 my $git_dirs = $lei->{opt}->{'git-dir'} //= [];
197 if ($lei->{opt}->{cwd} // 1) {
198 my $cgd = PublicInbox::LeiBlob::get_git_dir($lei, '.');
199 unshift(@$git_dirs, $cgd) if defined $cgd;
201 return $lei->fail('no --git-dir to try') unless @$git_dirs;
202 my $lxs = $lei->lxs_prepare;
204 require PublicInbox::LeiRemote;
205 $lei->{curl} //= which('curl') or return
206 $lei->fail('curl needed for', $lxs->remotes);
208 $lei->ale->refresh_externals($lxs);
210 -force_eml => 1, # for LeiInput->input_fh
213 $self->prepare_inputs($lei, \@inputs) or return;
214 my $isatty = -t $lei->{1};
215 $lei->{opt}->{color} //= $isatty;
216 $lei->start_pager if $isatty;
217 my ($op_c, $ops) = $lei->workers_start($self, 1);
219 net_merge_all_done($self) unless $lei->{auth};
220 $op_c->op_wait_event($ops);
223 sub ipc_atfork_child {
225 PublicInbox::LeiInput::input_only_atfork_child(@_);
226 my $lei = $self->{lei};
227 $lei->{1}->autoflush(1);
228 binmode $lei->{1}, ':utf8';
229 $self->{blobs} = {}; # oidhex => filename
230 $self->{rdtmp} = File::Temp->newdir('lei-rediff-XXXX', TMPDIR => 1);
231 $self->{rmt} = [ map {
232 PublicInbox::LeiRemote->new($lei, $_)
233 } $self->{lxs}->remotes ];
234 $self->{gits} = [ map {
235 PublicInbox::Git->new($lei->rel2abs($_))
236 } @{$self->{lei}->{opt}->{'git-dir'}} ];
237 $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
238 $lei->{env}->{TMPDIR} = $self->{rdtmp}->dirname;
243 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
244 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;