]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ContentHash.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / ContentHash.pm
index 420dc5e7c92d28c05cfa183cf4059d5258afd786..d3ff146aa96e91ef3ada23c29689de0a33f6b2e0 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # Unstable internal API.
@@ -8,18 +8,20 @@
 # See L<public-inbox-v2-format(5)> manpage for more details.
 package PublicInbox::ContentHash;
 use strict;
-use warnings;
-use base qw/Exporter/;
-our @EXPORT_OK = qw/content_hash content_digest/;
+use v5.10.1;
+use parent qw(Exporter);
+our @EXPORT_OK = qw(content_hash content_digest git_sha);
 use PublicInbox::MID qw(mids references);
 use PublicInbox::MsgIter;
 
 # not sure if less-widely supported hash families are worth bothering with
-use Digest::SHA;
+use PublicInbox::SHA; # faster, but no ->clone
+use Digest::SHA; # we still need this for ->clone
 
 sub digest_addr ($$$) {
        my ($dig, $h, $v) = @_;
        $v =~ tr/"//d;
+       $v =~ tr/\r\n\t / /s;
        $v =~ s/@([a-z0-9\_\.\-\(\)]*([A-Z])\S*)/'@'.lc($1)/ge;
        utf8::encode($v);
        $dig->add("$h\0$v\0");
@@ -52,30 +54,29 @@ sub content_dig_i {
        $dig->add($s);
 }
 
-sub content_digest ($) {
-       my ($mime) = @_;
-       my $dig = Digest::SHA->new(256);
-       my $hdr = $mime->header_obj;
+sub content_digest ($;$) {
+       my ($eml, $dig) = @_;
+       $dig //= Digest::SHA->new(256);
 
        # References: and In-Reply-To: get used interchangeably
        # in some "duplicates" in LKML.  We treat them the same
        # in SearchIdx, so treat them the same for this:
        # do NOT consider the Message-ID as part of the content_hash
        # if we got here, we've already got Message-ID reuse
-       my %seen = map { $_ => 1 } @{mids($hdr)};
-       foreach my $mid (@{references($hdr)}) {
-               $dig->add("ref\0$mid\0") unless $seen{$mid}++;
+       my %seen = map { $_ => 1 } @{mids($eml)};
+       for (grep { !$seen{$_}++ } @{references($eml)}) {
+               utf8::encode($_);
+               $dig->add("ref\0$_\0");
        }
 
        # Only use Sender: if From is not present
        foreach my $h (qw(From Sender)) {
-               my @v = $hdr->header($h);
-               if (@v) {
-                       digest_addr($dig, $h, $_) foreach @v;
-               }
+               my @v = $eml->header($h) or next;
+               digest_addr($dig, $h, $_) foreach @v;
+               last;
        }
        foreach my $h (qw(Subject Date)) {
-               my @v = $hdr->header($h);
+               my @v = $eml->header($h);
                foreach my $v (@v) {
                        utf8::encode($v);
                        $dig->add("$h\0$v\0");
@@ -85,15 +86,24 @@ sub content_digest ($) {
        # not in the original message.  For the purposes of deduplication,
        # do not take it into account:
        foreach my $h (qw(To Cc)) {
-               my @v = $hdr->header($h);
+               my @v = $eml->header($h);
                digest_addr($dig, $h, $_) foreach @v;
        }
-       msg_iter($mime, \&content_dig_i, $dig);
+       msg_iter($eml, \&content_dig_i, $dig);
        $dig;
 }
 
 sub content_hash ($) {
-       content_digest($_[0])->digest;
+       content_digest($_[0], PublicInbox::SHA->new(256))->digest;
+}
+
+# don't clone the result of this
+sub git_sha ($$) {
+       my ($n, $eml) = @_;
+       my $dig = PublicInbox::SHA->new($n);
+       my $bref = ref($eml) eq 'SCALAR' ? $eml : \($eml->as_string);
+       $dig->add('blob '.length($$bref)."\0", $$bref);
+       $dig;
 }
 
 1;