]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiRediff.pm
lei rediff: regenerate diffs from stdin
[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 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};
28
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)
34 EOF
35         $self->{blob}->{$oid_want} = $oid;
36         push @{$self->{gits}}, $git if $git->{-tmp};
37 }
38
39 # returns a full blob for oid_want
40 sub solve_1 ($$$) {
41         my ($self, $oid_want, $hints) = @_;
42         return if $oid_want =~ /\A0+\z/;
43         $self->{cur_oid_want} = $oid_want;
44         my $solver = bless {
45                 gits => $self->{gits},
46                 user_cb => \&rediff_user_cb,
47                 uarg => $self,
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
55 }
56
57 sub diff_ctxq ($$) {
58         my ($self, $ctxq) = @_;
59         return unless $ctxq;
60         my $blob = $self->{blob};
61         my $ta = <<'EOM';
62 reset refs/heads/A
63 commit refs/heads/A
64 author <a@s> 0 +0000
65 committer <c@s> 0 +0000
66 data 0
67 EOM
68         my $tb = $ta;
69         $tb =~ tr!A!B!;
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,
74                                                         { path_b => $pa });
75                 my $xb = $blob->{$oid_b} //= solve_1($self, $oid_b, {
76                                                 oid_a => $oid_a,
77                                                 path_a => $pa,
78                                                 path_b => $pb
79                                         });
80                 $ta .= "M 100644 $xa ".git_quote($pa)."\n" if $xa;
81                 $tb .= "M 100644 $xb ".git_quote($pb)."\n" if $xb;
82         }
83         my $rw = $self->{gits}->[-1]; # has all known alternates
84         if (!$rw->{-tmp}) {
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";
91                 }
92                 close $fh or die "close $f: $!";
93                 $rw = PublicInbox::Git->new($d);
94         }
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: $!";
102         waitpid($pid, 0);
103         die "fast-import failed: \$?=$?" if $?;
104
105         my @cmd = qw(diff);
106         my $opt = $lei->{opt};
107         push @cmd, '--'.($opt->{color} && !$opt->{'no-color'} ? '' : 'no-').
108                         'color';
109         for my $o (@PublicInbox::LEI::diff_opt) {
110                 $o =~ s/\|([a-z0-9])\b//i; # remove single char short option
111                 my $c = $1;
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";
127                 }
128         }
129         $lei->qerr("# git @cmd");
130         push @cmd, qw(A B);
131         unshift @cmd, 'git', "--git-dir=$rw->{git_dir}";
132         $pid = spawn(\@cmd, $lei->{env}, { 2 => $lei->{2}, 1 => $lei->{1} });
133         waitpid($pid, 0);
134         $lei->child_error($?) if $?; # for git diff --exit-code
135 }
136
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);
144         undef $s;
145         my $blobs = $self->{blobs}; # blobs to resolve
146         my $ctxq;
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;
159                 } elsif ($ctxq) {
160                         my @out;
161                         for (split(/^/sm, $x)) {
162                                 if (/\A-- \r?\n/s) { # email sig starts
163                                         push @out, $_;
164                                         $ctxq = diff_ctxq($self, $ctxq);
165                                 } elsif ($ctxq && (/\A[\+\- ]/ || /\A@@ / ||
166                                         # allow totally blank lines w/o leading
167                                         # SP, git-apply does:
168                                                         /\A\r?\n/s)) {
169                                         next;
170                                 } else {
171                                         push @out, $_;
172                                 }
173                         }
174                         $self->{lei}->out(@out) if @out;
175                 } else {
176                         $ctxq = diff_ctxq($self, $ctxq);
177                         $self->{lei}->out($x);
178                 }
179         }
180         $ctxq = diff_ctxq($self, $ctxq);
181 }
182
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);
188 }
189
190 sub lei_rediff {
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;
200         }
201         return $lei->fail('no --git-dir to try') unless @$git_dirs;
202         my $lxs = $lei->lxs_prepare;
203         if ($lxs->remotes) {
204                 require PublicInbox::LeiRemote;
205                 $lei->{curl} //= which('curl') or return
206                         $lei->fail('curl needed for', $lxs->remotes);
207         }
208         $lei->ale->refresh_externals($lxs);
209         my $self = bless {
210                 -force_eml => 1, # for LeiInput->input_fh
211                 lxs => $lxs,
212         }, __PACKAGE__;
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);
218         $lei->{wq1} = $self;
219         net_merge_all_done($self) unless $lei->{auth};
220         $op_c->op_wait_event($ops);
221 }
222
223 sub ipc_atfork_child {
224         my ($self) = @_;
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;
239         undef;
240 }
241
242 no warnings 'once';
243 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
244 *net_merge_all = \&PublicInbox::LeiAuth::net_merge_all;
245 1;