Skip to content

Commit b604954

Browse files
Nathan Payregitster
authored andcommitted
send-email: extract email-parsing code into a subroutine
The existing code mixes parsing of email header with regular expression and actual code. Extract the parsing code into a new subroutine "parse_header_line()". This improves the code readability and make parse_header_line reusable in other place. "parsed_header_line()" and "filter_body()" could be used for refactoring the part of code which parses the header to prepare the email and send it. In contrast to the previous version it doesn't keep the header order and strip duplicate headers. Signed-off-by: Nathan Payre <[email protected]> Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Timothee Albertin <[email protected]> Signed-off-by: Daniel Bensoussan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Helped-by: Ævar Arnfjörð Bjarmason <[email protected]> Reviewed-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 52015aa commit b604954

File tree

1 file changed

+77
-38
lines changed

1 file changed

+77
-38
lines changed

git-send-email.perl

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -703,57 +703,70 @@ sub get_patch_subject {
703703
do_edit($compose_filename);
704704
}
705705
706-
open my $c2, ">", $compose_filename . ".final"
707-
or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
708-
709706
open $c, "<", $compose_filename
710707
or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
711708
712-
my $need_8bit_cte = file_has_nonascii($compose_filename);
713-
my $in_body = 0;
714-
my $summary_empty = 1;
715709
if (!defined $compose_encoding) {
716710
$compose_encoding = "UTF-8";
717711
}
718-
while(<$c>) {
719-
next if m/^GIT:/;
720-
if ($in_body) {
721-
$summary_empty = 0 unless (/^\n$/);
722-
} elsif (/^\n$/) {
723-
$in_body = 1;
724-
if ($need_8bit_cte) {
725-
print $c2 "MIME-Version: 1.0\n",
726-
"Content-Type: text/plain; ",
727-
"charset=$compose_encoding\n",
728-
"Content-Transfer-Encoding: 8bit\n";
729-
}
730-
} elsif (/^MIME-Version:/i) {
731-
$need_8bit_cte = 0;
732-
} elsif (/^Subject:\s*(.+)\s*$/i) {
733-
$initial_subject = $1;
734-
my $subject = $initial_subject;
735-
$_ = "Subject: " .
736-
quote_subject($subject, $compose_encoding) .
737-
"\n";
738-
} elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
739-
$initial_reply_to = $1;
740-
next;
741-
} elsif (/^From:\s*(.+)\s*$/i) {
742-
$sender = $1;
743-
next;
744-
} elsif (/^(?:To|Cc|Bcc):/i) {
745-
print __("To/Cc/Bcc fields are not interpreted yet, they have been ignored\n");
746-
next;
712+
713+
my %parsed_email;
714+
while (my $line = <$c>) {
715+
next if $line =~ m/^GIT:/;
716+
parse_header_line($line, \%parsed_email);
717+
if ($line =~ /^$/) {
718+
$parsed_email{'body'} = filter_body($c);
747719
}
748-
print $c2 $_;
749720
}
750721
close $c;
751-
close $c2;
752722
753-
if ($summary_empty) {
723+
open my $c2, ">", $compose_filename . ".final"
724+
or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
725+
726+
727+
if ($parsed_email{'From'}) {
728+
$sender = delete($parsed_email{'From'});
729+
}
730+
if ($parsed_email{'In-Reply-To'}) {
731+
$initial_reply_to = delete($parsed_email{'In-Reply-To'});
732+
}
733+
if ($parsed_email{'Subject'}) {
734+
$initial_subject = delete($parsed_email{'Subject'});
735+
print $c2 "Subject: " .
736+
quote_subject($initial_subject, $compose_encoding) .
737+
"\n";
738+
}
739+
740+
if ($parsed_email{'MIME-Version'}) {
741+
print $c2 "MIME-Version: $parsed_email{'MIME-Version'}\n",
742+
"Content-Type: $parsed_email{'Content-Type'};\n",
743+
"Content-Transfer-Encoding: $parsed_email{'Content-Transfer-Encoding'}\n";
744+
delete($parsed_email{'MIME-Version'});
745+
delete($parsed_email{'Content-Type'});
746+
delete($parsed_email{'Content-Transfer-Encoding'});
747+
} elsif (file_has_nonascii($compose_filename)) {
748+
my $content_type = (delete($parsed_email{'Content-Type'}) or
749+
"text/plain; charset=$compose_encoding");
750+
print $c2 "MIME-Version: 1.0\n",
751+
"Content-Type: $content_type\n",
752+
"Content-Transfer-Encoding: 8bit\n";
753+
}
754+
# Preserve unknown headers
755+
foreach my $key (keys %parsed_email) {
756+
next if $key eq 'body';
757+
print $c2 "$key: $parsed_email{$key}";
758+
}
759+
760+
if ($parsed_email{'body'}) {
761+
print $c2 "\n$parsed_email{'body'}\n";
762+
delete($parsed_email{'body'});
763+
} else {
754764
print __("Summary email is empty, skipping it\n");
755765
$compose = -1;
756766
}
767+
768+
close $c2;
769+
757770
} elsif ($annotate) {
758771
do_edit(@files);
759772
}
@@ -792,6 +805,32 @@ sub ask {
792805
return;
793806
}
794807
808+
sub parse_header_line {
809+
my $lines = shift;
810+
my $parsed_line = shift;
811+
my $addr_pat = join "|", qw(To Cc Bcc);
812+
813+
foreach (split(/\n/, $lines)) {
814+
if (/^($addr_pat):\s*(.+)$/i) {
815+
$parsed_line->{$1} = [ parse_address_line($2) ];
816+
} elsif (/^([^:]*):\s*(.+)\s*$/i) {
817+
$parsed_line->{$1} = $2;
818+
}
819+
}
820+
}
821+
822+
sub filter_body {
823+
my $c = shift;
824+
my $body = "";
825+
while (my $body_line = <$c>) {
826+
if ($body_line !~ m/^GIT:/) {
827+
$body .= $body_line;
828+
}
829+
}
830+
return $body;
831+
}
832+
833+
795834
my %broken_encoding;
796835
797836
sub file_declares_8bit_cte {

0 commit comments

Comments
 (0)