]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiRediff.pm
1e95e55ac1cce9c2409ac0d9e0a53dd854492e4b
[public-inbox.git] / lib / PublicInbox / LeiRediff.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 # The "lei rediff" sub-command, regenerates diffs with new options
5 package PublicInbox::LeiRediff;
6 use strict;
7 use v5.10.1;
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;
16 use PublicInbox::LEI;
17 use PublicInbox::SolverGit;
18
19 my $MODE = '(100644|120000|100755|160000)';
20
21 sub rediff_user_cb { # called by solver when done
22         my ($res, $self) = @_;
23         my $lei = $self->{lei};
24         my $log_buf = delete $lei->{log_buf};
25         $$log_buf =~ s/^/# /sgm;
26         ref($res) eq 'ARRAY' or return $lei->child_error(0, $$log_buf);
27         $lei->qerr($$log_buf);
28         my ($git, $oid, $type, $size, $di) = @$res;
29         my $oid_want = delete $self->{cur_oid_want};
30
31         # don't try to support all the git-show(1) options for non-blob,
32         # this is just a convenience:
33         $type ne 'blob' and return $lei->err(<<EOF);
34 # $oid is a $type of $size bytes in:
35 # $git->{git_dir} (wanted: $oid_want)
36 EOF
37         $self->{blob}->{$oid_want} = $oid;
38         push @{$self->{gits}}, $git if $git->{-tmp};
39 }
40
41 # returns a full blob for oid_want
42 sub solve_1 ($$$) {
43         my ($self, $oid_want, $hints) = @_;
44         return if $oid_want =~ /\A0+\z/;
45         $self->{cur_oid_want} = $oid_want;
46         my $solver = bless {
47                 gits => $self->{gits},
48                 user_cb => \&rediff_user_cb,
49                 uarg => $self,
50                 inboxes => [ $self->{lxs}->locals, @{$self->{rmt}} ],
51         }, 'PublicInbox::SolverGit';
52         open my $log, '+>', \(my $log_buf = '') or die "PerlIO::scalar: $!";
53         $self->{lei}->{log_buf} = \$log_buf;
54         local $PublicInbox::DS::in_loop = 0; # waitpid synchronously
55         $solver->solve($self->{lei}->{env}, $log, $oid_want, $hints);
56         $self->{blob}->{$oid_want}; # full OID
57 }
58
59 sub diff_ctxq ($$) {
60         my ($self, $ctxq) = @_;
61         return unless $ctxq;
62         my $blob = $self->{blob};
63         my $ta = <<'EOM';
64 reset refs/heads/A
65 commit refs/heads/A
66 author <a@s> 0 +0000
67 committer <c@s> 0 +0000
68 data 0
69 EOM
70         my $tb = $ta;
71         $tb =~ tr!A!B!;
72         my $lei = $self->{lei};
73         while (my ($oid_a, $oid_b, $pa, $pb, $ma, $mb) = splice(@$ctxq, 0, 6)) {
74                 my $xa = $blob->{$oid_a} //= solve_1($self, $oid_a,
75                                                         { path_b => $pa });
76                 my $xb = $blob->{$oid_b} //= solve_1($self, $oid_b, {
77                                                 oid_a => $oid_a,
78                                                 path_a => $pa,
79                                                 path_b => $pb
80                                         });
81                 $ta .= "M $ma $xa ".git_quote($pa)."\n" if $xa;
82                 $tb .= "M $mb $xb ".git_quote($pb)."\n" if $xb;
83         }
84         my $rw = $self->{gits}->[-1]; # has all known alternates
85         if (!$rw->{-tmp}) {
86                 my $d = "$self->{rdtmp}/for_tree.git";
87                 -d $d or PublicInbox::Import::init_bare($d);
88                 my $f = "$d/objects/info/alternates"; # always overwrite
89                 open my $fh, '>', $f or die "open $f: $!";
90                 for my $git (@{$self->{gits}}) {
91                         print $fh $git->git_path('objects'),"\n";
92                 }
93                 close $fh or die "close $f: $!";
94                 $rw = PublicInbox::Git->new($d);
95         }
96         pipe(my ($r, $w)) or die "pipe: $!";
97         my $pid = spawn(['git', "--git-dir=$rw->{git_dir}",
98                         qw(fast-import --quiet --done --date-format=raw)],
99                         $lei->{env}, { 2 => $lei->{2}, 0 => $r });
100         close $r or die "close r fast-import: $!";
101         print $w $ta, "\n", $tb, "\ndone\n" or die "print fast-import: $!";
102         close $w or die "close w fast-import: $!";
103         waitpid($pid, 0);
104         die "fast-import failed: \$?=$?" if $?;
105
106         my @cmd = qw(diff);
107         my $opt = $lei->{opt};
108         push @cmd, '--'.($opt->{color} && !$opt->{'no-color'} ? '' : 'no-').
109                         'color';
110         for my $o (@PublicInbox::LEI::diff_opt) {
111                 my $c = '';
112                 # remove single char short option
113                 $o =~ s/\|([a-z0-9])\b//i and $c = $1;
114                 if ($o =~ s/=[is]@\z//) {
115                         my $v = $opt->{$o} or next;
116                         push @cmd, map { $c ? "-$c$_" : "--$o=$_" } @$v;
117                 } elsif ($o =~ s/=[is]\z//) {
118                         my $v = $opt->{$o} // next;
119                         push @cmd, $c ? "-$c$v" : "--$o=$v";
120                 } elsif ($o =~ s/:[is]\z//) {
121                         my $v = $opt->{$o} // next;
122                         push @cmd, $c ? "-$c$v" :
123                                         ($v eq '' ? "--$o" : "--$o=$v");
124                 } elsif ($o =~ s/!\z//) {
125                         my $v = $opt->{$o} // next;
126                         push @cmd, $v ? "--$o" : "--no-$o";
127                 } elsif ($opt->{$o}) {
128                         push @cmd, $c ? "-$c" : "--$o";
129                 }
130         }
131         $lei->qerr("# git @cmd");
132         push @cmd, qw(A B);
133         unshift @cmd, 'git', "--git-dir=$rw->{git_dir}";
134         $pid = spawn(\@cmd, $lei->{env}, { 2 => $lei->{2}, 1 => $lei->{1} });
135         waitpid($pid, 0);
136         $lei->child_error($?) if $?; # for git diff --exit-code
137         undef;
138 }
139
140 sub wait_requote ($$$) { # OnDestroy callback
141         my ($lei, $pid, $old_1) = @_;
142         $lei->{1} = $old_1; # closes stdin of `perl -pE 's/^/> /'`
143         waitpid($pid, 0) == $pid or die "BUG(?) waitpid: \$!=$! \$?=$?";
144         $lei->child_error($?) if $?;
145 }
146
147 sub requote ($$) {
148         my ($lei, $pfx) = @_;
149         pipe(my($r, $w)) or die "pipe: $!";
150         my $rdr = { 0 => $r, 1 => $lei->{1}, 2 => $lei->{2} };
151         # $^X (perl) is overkill, but maybe there's a weird system w/o sed
152         my $pid = spawn([$^X, '-pE', "s/^/$pfx/"], $lei->{env}, $rdr);
153         my $old_1 = $lei->{1};
154         $w->autoflush(1);
155         binmode $w, ':utf8';
156         $lei->{1} = $w;
157         PublicInbox::OnDestroy->new(\&wait_requote, $lei, $pid, $old_1);
158 }
159
160 sub extract_oids { # Eml each_part callback
161         my ($ary, $self) = @_;
162         my ($p, undef, $idx) = @$ary;
163         $self->{lei}->out($p->header_obj->as_string, "\n");
164         my ($s, undef) = msg_part_text($p, $p->content_type || 'text/plain');
165         defined $s or return;
166         my $rq;
167         if ($self->{dqre} && $s =~ s/$self->{dqre}//g) { # '> ' prefix(es)
168                 $rq = requote($self->{lei}, $1) if $self->{lei}->{opt}->{drq};
169         }
170         my @top = split($PublicInbox::ViewDiff::EXTRACT_DIFFS, $s);
171         undef $s;
172         my $blobs = $self->{blobs}; # blobs to resolve
173         my $ctxq;
174         while (defined(my $x = shift @top)) {
175                 if (scalar(@top) >= 4 &&
176                                 $top[1] =~ $PublicInbox::ViewDiff::IS_OID &&
177                                 $top[0] =~ $PublicInbox::ViewDiff::IS_OID) {
178                         my ($ma, $mb);
179                         $x =~ /^old mode $MODE/sm and $ma = $1;
180                         $x =~ /^new mode $MODE/sm and $mb = $1;
181                         if (!defined($ma) && $x =~
182                                 /^index [a-z0-9]+\.\.[a-z0-9]+ $MODE/sm) {
183                                 $ma = $mb = $1;
184                         }
185                         $ma //= '100644';
186                         $mb //= $ma;
187                         my ($oid_a, $oid_b, $pa, $pb) = splice(@top, 0, 4);
188                         $pa eq '/dev/null' or
189                                 $pa = (split(m'/', git_unquote($pa), 2))[1];
190                         $pb eq '/dev/null' or
191                                 $pb = (split(m'/', git_unquote($pb), 2))[1];
192                         $blobs->{$oid_a} //= undef;
193                         $blobs->{$oid_b} //= undef;
194                         push @$ctxq, $oid_a, $oid_b, $pa, $pb, $ma, $mb;
195                 } elsif ($ctxq) {
196                         my @out;
197                         for (split(/^/sm, $x)) {
198                                 if (/\A-- \r?\n/s) { # email sig starts
199                                         push @out, $_;
200                                         $ctxq = diff_ctxq($self, $ctxq);
201                                 } elsif ($ctxq && (/\A[\+\- ]/ || /\A@@ / ||
202                                         # allow totally blank lines w/o leading
203                                         # SP, git-apply does:
204                                                         /\A\r?\n/s)) {
205                                         next;
206                                 } else {
207                                         push @out, $_;
208                                 }
209                         }
210                         $self->{lei}->out(@out) if @out;
211                 } else {
212                         $ctxq = diff_ctxq($self, $ctxq);
213                         $self->{lei}->out($x);
214                 }
215         }
216         $ctxq = diff_ctxq($self, $ctxq);
217 }
218
219 # ensure dequoted parts are available for rebuilding patches:
220 sub dequote_add { # Eml each_part callback
221         my ($ary, $self) = @_;
222         my ($p, undef, $idx) = @$ary;
223         my ($s, undef) = msg_part_text($p, $p->content_type || 'text/plain');
224         defined $s or return;
225         if ($s =~ s/$self->{dqre}//g) { # remove '> ' prefix(es)
226                 substr($s, 0, 0, "part-dequoted: $idx\n\n");
227                 utf8::encode($s);
228                 $self->{tmp_sto}->add_eml(PublicInbox::Eml->new(\$s));
229         }
230 }
231
232 sub input_eml_cb { # callback for all emails
233         my ($self, $eml) = @_;
234         {
235                 local $SIG{__WARN__} = sub {
236                         return if "@_" =~ /^no email in From: .*? or Sender:/;
237                         return if PublicInbox::Eml::warn_ignore(@_);
238                         warn @_;
239                 };
240                 $self->{tmp_sto}->add_eml($eml);
241                 $eml->each_part(\&dequote_add, $self) if $self->{dqre};
242                 $self->{tmp_sto}->done;
243         }
244         $eml->each_part(\&extract_oids, $self, 1);
245 }
246
247 sub lei_rediff {
248         my ($lei, @inputs) = @_;
249         ($lei->{opt}->{drq} && $lei->{opt}->{'dequote-only'}) and return
250                 $lei->fail('--drq and --dequote-only are mutually exclusive');
251         ($lei->{opt}->{drq} && !$lei->{opt}->{verbose}) and
252                 $lei->{opt}->{quiet} //= 1;
253         $lei->_lei_store(1)->write_prepare($lei);
254         $lei->{opt}->{'in-format'} //= 'eml';
255         # maybe it's a non-email (code) blob from a coderepo
256         my $git_dirs = $lei->{opt}->{'git-dir'} //= [];
257         if ($lei->{opt}->{cwd} // 1) {
258                 my $cgd = PublicInbox::LeiBlob::get_git_dir($lei, '.');
259                 unshift(@$git_dirs, $cgd) if defined $cgd;
260         }
261         return $lei->fail('no --git-dir to try') unless @$git_dirs;
262         my $lxs = $lei->lxs_prepare;
263         if ($lxs->remotes) {
264                 require PublicInbox::LeiRemote;
265                 $lei->{curl} //= which('curl') or return
266                         $lei->fail('curl needed for', $lxs->remotes);
267         }
268         $lei->ale->refresh_externals($lxs, $lei);
269         my $self = bless {
270                 -force_eml => 1, # for LeiInput->input_fh
271                 lxs => $lxs,
272         }, __PACKAGE__;
273         $self->prepare_inputs($lei, \@inputs) or return;
274         my $isatty = -t $lei->{1};
275         $lei->{opt}->{color} //= $isatty;
276         $lei->start_pager if $isatty;
277         my ($op_c, $ops) = $lei->workers_start($self, 1);
278         $lei->{wq1} = $self;
279         net_merge_all_done($self) unless $lei->{auth};
280         $lei->wait_wq_events($op_c, $ops);
281 }
282
283 sub ipc_atfork_child {
284         my ($self) = @_;
285         PublicInbox::LeiInput::input_only_atfork_child(@_);
286         my $lei = $self->{lei};
287         $lei->{1}->autoflush(1);
288         binmode $lei->{1}, ':utf8';
289         $self->{blobs} = {}; # oidhex => filename
290         $self->{rdtmp} = File::Temp->newdir('lei-rediff-XXXX', TMPDIR => 1);
291         $self->{tmp_sto} = PublicInbox::LeiStore->new(
292                         "$self->{rdtmp}/tmp.store",
293                         { creat => { nproc => 1 }, indexlevel => 'medium' });
294         $self->{tmp_sto}->{priv_eidx}->{parallel} = 0;
295         $self->{rmt} = [ $self->{tmp_sto}->search, map {
296                         PublicInbox::LeiRemote->new($lei, $_)
297                 } $self->{lxs}->remotes ];
298         $self->{gits} = [ map {
299                         PublicInbox::Git->new($lei->rel2abs($_))
300                 } @{$self->{lei}->{opt}->{'git-dir'}} ];
301         $lei->{env}->{'psgi.errors'} = $lei->{2}; # ugh...
302         $lei->{env}->{TMPDIR} = $self->{rdtmp}->dirname;
303         if (my $nr = ($lei->{opt}->{drq} || $lei->{opt}->{'dequote-only'})) {
304                 my $re = '\s*> ' x $nr;
305                 $self->{dqre} = qr/^($re)/ms;
306         }
307         undef;
308 }
309
310 no warnings 'once';
311 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
312 1;