]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ContentHash.pm
www_coderepo: handle unborn/dead branches in summary
[public-inbox.git] / lib / PublicInbox / ContentHash.pm
index 112b1ea6c1e660722cf1e68982745edb93912599..d3ff146aa96e91ef3ada23c29689de0a33f6b2e0 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018-2021 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.
@@ -15,11 +15,13 @@ 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,9 +54,9 @@ sub content_dig_i {
        $dig->add($s);
 }
 
-sub content_digest ($) {
-       my ($eml) = @_;
-       my $dig = Digest::SHA->new(256);
+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
@@ -62,8 +64,9 @@ sub content_digest ($) {
        # 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($eml)};
-       foreach my $mid (@{references($eml)}) {
-               $dig->add("ref\0$mid\0") unless $seen{$mid}++;
+       for (grep { !$seen{$_}++ } @{references($eml)}) {
+               utf8::encode($_);
+               $dig->add("ref\0$_\0");
        }
 
        # Only use Sender: if From is not present
@@ -91,15 +94,15 @@ sub content_digest ($) {
 }
 
 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 = Digest::SHA->new($n);
-       my $buf = $eml->as_string;
-       $dig->add('blob '.length($buf)."\0");
-       $dig->add($buf);
+       my $dig = PublicInbox::SHA->new($n);
+       my $bref = ref($eml) eq 'SCALAR' ? $eml : \($eml->as_string);
+       $dig->add('blob '.length($$bref)."\0", $$bref);
        $dig;
 }