]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiMailDiff.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / LeiMailDiff.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # The "lei mail-diff" sub-command, diffs input contents against
5 # the first message of input
6 package PublicInbox::LeiMailDiff;
7 use v5.12;
8 use parent qw(PublicInbox::IPC PublicInbox::LeiInput PublicInbox::MailDiff);
9 use PublicInbox::Spawn qw(spawn which);
10 use File::Path ();
11 require PublicInbox::LeiRediff;
12
13 sub diff_a ($$) {
14         my ($self, $eml) = @_;
15         my $dir = "$self->{tmp}/N".(++$self->{nr});
16         $self->dump_eml($dir, $eml);
17         my $cmd = [ qw(git diff --no-index) ];
18         my $lei = $self->{lei};
19         PublicInbox::LeiRediff::_lei_diff_prepare($lei, $cmd);
20         push @$cmd, qw(-- a), "N$self->{nr}";
21         my $rdr = { -C => "$self->{tmp}" };
22         @$rdr{1, 2} = @$lei{1, 2};
23         my $pid = spawn($cmd, $lei->{env}, $rdr);
24         waitpid($pid, 0);
25         $lei->child_error($?) if $?; # for git diff --exit-code
26         File::Path::remove_tree($self->{curdir});
27 }
28
29 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
30         my ($self, $eml) = @_;
31         $self->{tmp} ? diff_a($self, $eml) : $self->prep_a($eml);
32 }
33
34 sub lei_mail_diff {
35         my ($lei, @argv) = @_;
36         my $self = bless {}, __PACKAGE__;
37         $self->prepare_inputs($lei, \@argv) or return;
38         my $isatty = -t $lei->{1};
39         $lei->{opt}->{color} //= $isatty;
40         $lei->start_pager if $isatty;
41         $lei->{-err_type} = 'non-fatal';
42         $self->{-raw_hdr} = $lei->{opt}->{'raw-header'};
43         $lei->wq1_start($self);
44 }
45
46 no warnings 'once';
47 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
48 1;