From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 61CA11F47C for ; Wed, 11 Jan 2023 11:01:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1673434861; bh=P4xfAJVb1OFIIZoSgKB+nFdIgu/vfCSSeQjgDJB1SFQ=; h=Date:From:To:Subject:References:In-Reply-To:From; b=xFEgQGv9gY4EPq6ISc3BB9XxnQ7hlLkfHF/USN5S5XS5T42tzGRyYRV/onBGe56MO sf7zUUWpiXqVr2F4Vsb4PJTPs4XroNBgcPZLp0F5STxXmSDIdhbIYcrpFEsQ9UeNTb lBXapnXE1PnX6XkeWGiZOFFP/Bs9EYYmxpxSbWME= Date: Wed, 11 Jan 2023 11:00:49 +0000 From: Eric Wong To: meta@public-inbox.org Subject: [1/2 PATCH] hoist MailDiff and ContentDigestDbg out of lei Message-ID: <20230111110049.M302127@dcvr> References: <20230111105539.302803-1-e@80x24.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230111105539.302803-1-e@80x24.org> List-Id: These will be reused in the web UI, too. --- <20230111105539.302803-1-e@80x24.org> was actually [2/2] of this series. My mind drifted and I thought it was just one patch :x MANIFEST | 3 ++ lib/PublicInbox/ContentDigestDbg.pm | 17 +++++++ lib/PublicInbox/LeiMailDiff.pm | 71 +++-------------------------- lib/PublicInbox/MailDiff.pm | 50 ++++++++++++++++++++ t/lei-mail-diff.t | 14 ++++++ 5 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 lib/PublicInbox/ContentDigestDbg.pm create mode 100644 lib/PublicInbox/MailDiff.pm create mode 100644 t/lei-mail-diff.t diff --git a/MANIFEST b/MANIFEST index 565317ce..3626e4d2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -163,6 +163,7 @@ lib/PublicInbox/CmdIPC4.pm lib/PublicInbox/CompressNoop.pm lib/PublicInbox/Config.pm lib/PublicInbox/ConfigIter.pm +lib/PublicInbox/ContentDigestDbg.pm lib/PublicInbox/ContentHash.pm lib/PublicInbox/DS.pm lib/PublicInbox/DSKQXS.pm @@ -280,6 +281,7 @@ lib/PublicInbox/Lock.pm lib/PublicInbox/MDA.pm lib/PublicInbox/MID.pm lib/PublicInbox/MIME.pm +lib/PublicInbox/MailDiff.pm lib/PublicInbox/ManifestJsGz.pm lib/PublicInbox/Mbox.pm lib/PublicInbox/MboxGz.pm @@ -478,6 +480,7 @@ t/lei-import.t t/lei-index.t t/lei-inspect.t t/lei-lcat.t +t/lei-mail-diff.t t/lei-mirror.psgi t/lei-mirror.t t/lei-p2q.t diff --git a/lib/PublicInbox/ContentDigestDbg.pm b/lib/PublicInbox/ContentDigestDbg.pm new file mode 100644 index 00000000..425e8589 --- /dev/null +++ b/lib/PublicInbox/ContentDigestDbg.pm @@ -0,0 +1,17 @@ +# Copyright (C) all contributors +# License: AGPL-3.0+ +package PublicInbox::ContentDigestDbg; # cf. PublicInbox::ContentDigest +use v5.12; +use Data::Dumper; +use Digest::SHA; + +sub new { bless { dig => Digest::SHA->new(256), fh => $_[1] }, __PACKAGE__ } + +sub add { + $_[0]->{dig}->add($_[1]); + print { $_[0]->{fh} } Dumper([split(/^/sm, $_[1])]) or die "print $!"; +} + +sub hexdigest { $_[0]->{dig}->hexdigest; } + +1; diff --git a/lib/PublicInbox/LeiMailDiff.pm b/lib/PublicInbox/LeiMailDiff.pm index 2b4cfd9e..c813144f 100644 --- a/lib/PublicInbox/LeiMailDiff.pm +++ b/lib/PublicInbox/LeiMailDiff.pm @@ -4,59 +4,16 @@ # The "lei mail-diff" sub-command, diffs input contents against # the first message of input package PublicInbox::LeiMailDiff; -use strict; -use v5.10.1; -use parent qw(PublicInbox::IPC PublicInbox::LeiInput); -use File::Temp 0.19 (); # 0.19 for ->newdir +use v5.12; +use parent qw(PublicInbox::IPC PublicInbox::LeiInput PublicInbox::MailDiff); use PublicInbox::Spawn qw(spawn which); -use PublicInbox::MsgIter qw(msg_part_text); -use File::Path qw(remove_tree); -use PublicInbox::ContentHash qw(content_digest); +use File::Path (); require PublicInbox::LeiRediff; -use Data::Dumper (); - -sub write_part { # Eml->each_part callback - my ($ary, $self) = @_; - my ($part, $depth, $idx) = @$ary; - if ($idx ne '1' || $self->{lei}->{opt}->{'raw-header'}) { - open my $fh, '>', "$self->{curdir}/$idx.hdr" or die "open: $!"; - print $fh ${$part->{hdr}} or die "print $!"; - close $fh or die "close $!"; - } - my $ct = $part->content_type || 'text/plain'; - my ($s, $err) = msg_part_text($part, $ct); - my $sfx = defined($s) ? 'txt' : 'bin'; - open my $fh, '>', "$self->{curdir}/$idx.$sfx" or die "open: $!"; - print $fh ($s // $part->body) or die "print $!"; - close $fh or die "close $!"; -} - -sub dump_eml ($$$) { - my ($self, $dir, $eml) = @_; - local $self->{curdir} = $dir; - mkdir $dir or die "mkdir($dir): $!"; - $eml->each_part(\&write_part, $self); - - open my $fh, '>', "$dir/content_digest" or die "open: $!"; - my $dig = PublicInbox::ContentDigestDbg->new($fh); - local $Data::Dumper::Useqq = 1; - local $Data::Dumper::Terse = 1; - content_digest($eml, $dig); - print $fh "\n", $dig->hexdigest, "\n" or die "print $!"; - close $fh or die "close: $!"; -} - -sub prep_a ($$) { - my ($self, $eml) = @_; - $self->{tmp} = File::Temp->newdir('lei-mail-diff-XXXX', TMPDIR => 1); - dump_eml($self, "$self->{tmp}/a", $eml); -} sub diff_a ($$) { my ($self, $eml) = @_; - ++$self->{nr}; - my $dir = "$self->{tmp}/N$self->{nr}"; - dump_eml($self, $dir, $eml); + my $dir = "$self->{tmp}/N".(++$self->{nr}); + $self->dump_eml($dir, $eml); my $cmd = [ qw(git diff --no-index) ]; my $lei = $self->{lei}; PublicInbox::LeiRediff::_lei_diff_prepare($lei, $cmd); @@ -71,7 +28,7 @@ sub diff_a ($$) { sub input_eml_cb { # used by PublicInbox::LeiInput::input_fh my ($self, $eml) = @_; - $self->{tmp} ? diff_a($self, $eml) : prep_a($self, $eml); + $self->{tmp} ? diff_a($self, $eml) : $self->prep_a($eml); } sub lei_mail_diff { @@ -82,24 +39,10 @@ sub lei_mail_diff { $lei->{opt}->{color} //= $isatty; $lei->start_pager if $isatty; $lei->{-err_type} = 'non-fatal'; + $self->{-raw_hdr} = $lei->{opt}->{'raw-header'}; $lei->wq1_start($self); } no warnings 'once'; *net_merge_all_done = \&PublicInbox::LeiInput::input_only_net_merge_all_done; - -package PublicInbox::ContentDigestDbg; # cf. PublicInbox::ContentDigest -use strict; -use v5.10.1; -use Data::Dumper; - -sub new { bless { dig => Digest::SHA->new(256), fh => $_[1] }, __PACKAGE__ } - -sub add { - $_[0]->{dig}->add($_[1]); - print { $_[0]->{fh} } Dumper([split(/^/sm, $_[1])]) or die "print $!"; -} - -sub hexdigest { $_[0]->{dig}->hexdigest; } - 1; diff --git a/lib/PublicInbox/MailDiff.pm b/lib/PublicInbox/MailDiff.pm new file mode 100644 index 00000000..06eb3a0d --- /dev/null +++ b/lib/PublicInbox/MailDiff.pm @@ -0,0 +1,50 @@ +# Copyright (C) all contributors +# License: AGPL-3.0+ +package PublicInbox::MailDiff; +use v5.12; +use File::Temp 0.19 (); # 0.19 for ->newdir +use PublicInbox::ContentHash qw(content_digest); +use PublicInbox::ContentDigestDbg; +use Data::Dumper (); +use PublicInbox::MsgIter qw(msg_part_text); + +sub write_part { # Eml->each_part callback + my ($ary, $self) = @_; + my ($part, $depth, $idx) = @$ary; + if ($idx ne '1' || $self->{-raw_hdr}) { + open my $fh, '>', "$self->{curdir}/$idx.hdr" or die "open: $!"; + print $fh ${$part->{hdr}} or die "print $!"; + close $fh or die "close $!"; + } + my $ct = $part->content_type || 'text/plain'; + my ($s, $err) = msg_part_text($part, $ct); + my $sfx = defined($s) ? 'txt' : 'bin'; + open my $fh, '>', "$self->{curdir}/$idx.$sfx" or die "open: $!"; + print $fh ($s // $part->body) or die "print $!"; + close $fh or die "close $!"; +} + +# public +sub dump_eml ($$$) { + my ($self, $dir, $eml) = @_; + local $self->{curdir} = $dir; + mkdir $dir or die "mkdir($dir): $!"; + $eml->each_part(\&write_part, $self); + + open my $fh, '>', "$dir/content_digest" or die "open: $!"; + my $dig = PublicInbox::ContentDigestDbg->new($fh); + local $Data::Dumper::Useqq = 1; + local $Data::Dumper::Terse = 1; + content_digest($eml, $dig); + print $fh "\n", $dig->hexdigest, "\n" or die "print $!"; + close $fh or die "close: $!"; +} + +# public +sub prep_a ($$) { + my ($self, $eml) = @_; + $self->{tmp} = File::Temp->newdir('mail-diff-XXXX', TMPDIR => 1); + dump_eml($self, "$self->{tmp}/a", $eml); +} + +1; diff --git a/t/lei-mail-diff.t b/t/lei-mail-diff.t new file mode 100644 index 00000000..9398596a --- /dev/null +++ b/t/lei-mail-diff.t @@ -0,0 +1,14 @@ +#!perl -w +# Copyright (C) all contributors +# License: AGPL-3.0+ +use v5.12; use PublicInbox::TestCommon; + +test_lei(sub { + ok(!lei('mail-diff', 't/data/0001.patch', 't/data/binary.patch'), + 'different messages are different'); + like($lei_out, qr/^\+/m, 'diff shown'); + lei_ok('mail-diff', 't/data/0001.patch', 't/data/0001.patch'); + is($lei_out, '', 'no output if identical'); +}); + +done_testing;