1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # The "lei mail-diff" sub-command, diffs input contents against
5 # the first message of input
6 package PublicInbox::LeiMailDiff;
9 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
10 use File::Temp 0.19 (); # 0.19 for ->newdir
11 use PublicInbox::Spawn qw(spawn which);
12 use PublicInbox::MsgIter qw(msg_part_text);
13 use File::Path qw(remove_tree);
14 use PublicInbox::ContentHash qw(content_digest);
15 require PublicInbox::LeiRediff;
18 sub write_part { # Eml->each_part callback
19 my ($ary, $self) = @_;
20 my ($part, $depth, $idx) = @$ary;
21 if ($idx ne '1' || $self->{lei}->{opt}->{'raw-header'}) {
22 open my $fh, '>', "$self->{curdir}/$idx.hdr" or die "open: $!";
23 print $fh ${$part->{hdr}} or die "print $!";
24 close $fh or die "close $!";
26 my $ct = $part->content_type || 'text/plain';
27 my ($s, $err) = msg_part_text($part, $ct);
28 my $sfx = defined($s) ? 'txt' : 'bin';
29 open my $fh, '>', "$self->{curdir}/$idx.$sfx" or die "open: $!";
30 print $fh ($s // $part->body) or die "print $!";
31 close $fh or die "close $!";
35 my ($self, $dir, $eml) = @_;
36 local $self->{curdir} = $dir;
37 mkdir $dir or die "mkdir($dir): $!";
38 $eml->each_part(\&write_part, $self);
40 open my $fh, '>', "$dir/content_digest" or die "open: $!";
41 my $dig = PublicInbox::ContentDigestDbg->new($fh);
42 local $Data::Dumper::Useqq = 1;
43 local $Data::Dumper::Terse = 1;
44 content_digest($eml, $dig);
45 print $fh "\n", $dig->hexdigest, "\n" or die "print $!";
46 close $fh or die "close: $!";
50 my ($self, $eml) = @_;
51 $self->{tmp} = File::Temp->newdir('lei-mail-diff-XXXX', TMPDIR => 1);
52 dump_eml($self, "$self->{tmp}/a", $eml);
56 my ($self, $eml) = @_;
58 my $dir = "$self->{tmp}/N$self->{nr}";
59 dump_eml($self, $dir, $eml);
60 my $cmd = [ qw(git diff --no-index) ];
61 my $lei = $self->{lei};
62 PublicInbox::LeiRediff::_lei_diff_prepare($lei, $cmd);
63 push @$cmd, qw(-- a), "N$self->{nr}";
64 my $rdr = { -C => "$self->{tmp}" };
65 @$rdr{1, 2} = @$lei{1, 2};
66 my $pid = spawn($cmd, $lei->{env}, $rdr);
68 $lei->child_error($?) if $?; # for git diff --exit-code
69 File::Path::remove_tree($self->{curdir});
72 sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh
73 my ($self, $eml) = @_;
74 $self->{tmp} ? diff_a($self, $eml) : prep_a($self, $eml);
78 my ($lei, @argv) = @_;
79 $lei->{opt}->{'in-format'} //= 'eml';
80 my $self = bless {}, __PACKAGE__;
81 $self->prepare_inputs($lei, \@argv) or return;
82 my $isatty = -t $lei->{1};
83 $lei->{opt}->{color} //= $isatty;
84 $lei->start_pager if $isatty;
86 $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
87 (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
89 $lei->{-err_type} = 'non-fatal';
90 net_merge_all_done($self) unless $lei->{auth};
91 $lei->wait_wq_events($op_c, $ops);
95 *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done;
97 package PublicInbox::ContentDigestDbg;
102 sub new { bless { dig => Digest::SHA->new(256), fh => $_[1] }, __PACKAGE__ }
105 $_[0]->{dig}->add($_[1]);
106 print { $_[0]->{fh} } Dumper($_[1]) or die "print $!";
109 sub hexdigest { $_[0]->{dig}->hexdigest; }