]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ContentHash.pm
www: drop --subject from "git send-email" instructions
[public-inbox.git] / lib / PublicInbox / ContentHash.pm
index 838fdd6fcf654907af59ee1c451aebdaaa9b89b2..bacc9cdda12498abbb0ada5d2a2e2faec10190f2 100644 (file)
@@ -8,9 +8,9 @@
 # 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;
 
@@ -20,6 +20,7 @@ use Digest::SHA;
 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 +53,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
@@ -68,10 +69,9 @@ sub content_digest ($) {
 
        # Only use Sender: if From is not present
        foreach my $h (qw(From Sender)) {
-               my @v = $eml->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 = $eml->header($h);
@@ -95,4 +95,13 @@ sub content_hash ($) {
        content_digest($_[0])->digest;
 }
 
+sub git_sha ($$) {
+       my ($n, $eml) = @_;
+       my $dig = Digest::SHA->new($n);
+       my $bref = ref($eml) eq 'SCALAR' ? $eml : \($eml->as_string);
+       $dig->add('blob '.length($$bref)."\0");
+       $dig->add($$bref);
+       $dig;
+}
+
 1;