Skip to content

Commit 7ebee44

Browse files
committed
Merge branch 'ab/send-email-perl'
* ab/send-email-perl: send-email: extract_valid_address use qr// regexes send-email: is_rfc2047_quoted use qr// regexes send-email: use Perl idioms in while loop send-email: make_message_id use "require" instead of "use" send-email: send_message die on $!, not $? send-email: use (?:) instead of () if no match variables are needed send-email: sanitize_address use qq["foo"], not "\"foo\"" send-email: sanitize_address use $foo, not "$foo" send-email: use \E***\Q instead of \*\*\* send-email: cleanup_compose_files doesn't need a prototype send-email: unique_email_list doesn't need a prototype send-email: file_declares_8bit_cte doesn't need a prototype send-email: get_patch_subject doesn't need a prototype send-email: use lexical filehandles during sending send-email: use lexical filehandles for $compose send-email: use lexical filehandle for opendir Conflicts: git-send-email.perl
2 parents 8796ff7 + 35b6ab9 commit 7ebee44

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

git-send-email.perl

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ sub format_2822_time {
139139
my $smtp;
140140
my $auth;
141141

142-
sub unique_email_list(@);
143-
sub cleanup_compose_files();
144-
145142
# Variables we fill in automatically, or via prompting:
146143
my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
147144
$initial_reply_to,$initial_subject,@files,
@@ -377,7 +374,7 @@ sub read_config {
377374
if (@suppress_cc) {
378375
foreach my $entry (@suppress_cc) {
379376
die "Unknown --suppress-cc field: '$entry'\n"
380-
unless $entry =~ /^(all|cccmd|cc|author|self|sob|body|bodycc)$/;
377+
unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
381378
$suppress_cc{$entry} = 1;
382379
}
383380
}
@@ -521,12 +518,12 @@ ($)
521518
push @rev_list_opts, "--", @ARGV;
522519
@ARGV = ();
523520
} elsif (-d $f and !check_file_rev_conflict($f)) {
524-
opendir(DH,$f)
521+
opendir my $dh, $f
525522
or die "Failed to opendir $f: $!";
526523

527524
push @files, grep { -f $_ } map { catfile($f, $_) }
528-
sort readdir(DH);
529-
closedir(DH);
525+
sort readdir $dh;
526+
closedir $dh;
530527
} elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) {
531528
push @files, $f;
532529
} else {
@@ -558,7 +555,7 @@ ($)
558555
usage();
559556
}
560557

561-
sub get_patch_subject($) {
558+
sub get_patch_subject {
562559
my $fn = shift;
563560
open (my $fh, '<', $fn);
564561
while (my $line = <$fh>) {
@@ -576,15 +573,15 @@ ($)
576573
$compose_filename = ($repo ?
577574
tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
578575
tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
579-
open(C,">",$compose_filename)
576+
open my $c, ">", $compose_filename
580577
or die "Failed to open for writing $compose_filename: $!";
581578

582579

583580
my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
584581
my $tpl_subject = $initial_subject || '';
585582
my $tpl_reply_to = $initial_reply_to || '';
586583

587-
print C <<EOT;
584+
print $c <<EOT;
588585
From $tpl_sender # This line is ignored.
589586
GIT: Lines beginning in "GIT:" will be removed.
590587
GIT: Consider including an overall diffstat or table of contents
@@ -597,33 +594,33 @@ ($)
597594
598595
EOT
599596
for my $f (@files) {
600-
print C get_patch_subject($f);
597+
print $c get_patch_subject($f);
601598
}
602-
close(C);
599+
close $c;
603600

604601
if ($annotate) {
605602
do_edit($compose_filename, @files);
606603
} else {
607604
do_edit($compose_filename);
608605
}
609606

610-
open(C2,">",$compose_filename . ".final")
607+
open my $c2, ">", $compose_filename . ".final"
611608
or die "Failed to open $compose_filename.final : " . $!;
612609

613-
open(C,"<",$compose_filename)
610+
open $c, "<", $compose_filename
614611
or die "Failed to open $compose_filename : " . $!;
615612

616613
my $need_8bit_cte = file_has_nonascii($compose_filename);
617614
my $in_body = 0;
618615
my $summary_empty = 1;
619-
while(<C>) {
616+
while(<$c>) {
620617
next if m/^GIT:/;
621618
if ($in_body) {
622619
$summary_empty = 0 unless (/^\n$/);
623620
} elsif (/^\n$/) {
624621
$in_body = 1;
625622
if ($need_8bit_cte) {
626-
print C2 "MIME-Version: 1.0\n",
623+
print $c2 "MIME-Version: 1.0\n",
627624
"Content-Type: text/plain; ",
628625
"charset=UTF-8\n",
629626
"Content-Transfer-Encoding: 8bit\n";
@@ -648,10 +645,10 @@ ($)
648645
print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n";
649646
next;
650647
}
651-
print C2 $_;
648+
print $c2 $_;
652649
}
653-
close(C);
654-
close(C2);
650+
close $c;
651+
close $c2;
655652

656653
if ($summary_empty) {
657654
print "Summary email is empty, skipping it\n";
@@ -688,7 +685,7 @@ sub ask {
688685

689686
my %broken_encoding;
690687

691-
sub file_declares_8bit_cte($) {
688+
sub file_declares_8bit_cte {
692689
my $fn = shift;
693690
open (my $fh, '<', $fn);
694691
while (my $line = <$fh>) {
@@ -717,7 +714,7 @@ ($)
717714

718715
if (!$force) {
719716
for my $f (@files) {
720-
if (get_patch_subject($f) =~ /\*\*\* SUBJECT HERE \*\*\*/) {
717+
if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
721718
die "Refusing to send because the patch\n\t$f\n"
722719
. "has the template subject '*** SUBJECT HERE ***'. "
723720
. "Pass --force if you really want to send.\n";
@@ -789,8 +786,8 @@ sub expand_one_alias {
789786

790787
sub extract_valid_address {
791788
my $address = shift;
792-
my $local_part_regexp = '[^<>"\s@]+';
793-
my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
789+
my $local_part_regexp = qr/[^<>"\s@]+/;
790+
my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
794791

795792
# check for a local address:
796793
return $address if ($address =~ /^($local_part_regexp)$/);
@@ -831,7 +828,7 @@ sub make_message_id {
831828
last if (defined $du_part and $du_part ne '');
832829
}
833830
if (not defined $du_part or $du_part eq '') {
834-
use Sys::Hostname qw();
831+
require Sys::Hostname;
835832
$du_part = 'user@' . Sys::Hostname::hostname();
836833
}
837834
my $message_id_template = "<%s-git-send-email-%s>";
@@ -864,8 +861,8 @@ sub quote_rfc2047 {
864861

865862
sub is_rfc2047_quoted {
866863
my $s = shift;
867-
my $token = '[^][()<>@,;:"\/?.= \000-\037\177-\377]+';
868-
my $encoded_text = '[!->@-~]+';
864+
my $token = qr/[^][()<>@,;:"\/?.= \000-\037\177-\377]+/;
865+
my $encoded_text = qr/[!->@-~]+/;
869866
length($s) <= 75 &&
870867
$s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o;
871868
}
@@ -876,7 +873,7 @@ sub sanitize_address {
876873
my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
877874

878875
if (not $recipient_name) {
879-
return "$recipient";
876+
return $recipient;
880877
}
881878

882879
# if recipient_name is already quoted, do nothing
@@ -893,7 +890,7 @@ sub sanitize_address {
893890
# double quotes are needed if specials or CTLs are included
894891
elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
895892
$recipient_name =~ s/(["\\\r])/\\$1/g;
896-
$recipient_name = "\"$recipient_name\"";
893+
$recipient_name = qq["$recipient_name"];
897894
}
898895

899896
return "$recipient_name $recipient_addr";
@@ -1049,7 +1046,7 @@ sub send_message {
10491046
exec($smtp_server, @sendmail_parameters) or die $!;
10501047
}
10511048
print $sm "$header\n$message";
1052-
close $sm or die $?;
1049+
close $sm or die $!;
10531050
} else {
10541051

10551052
if (!defined $smtp_server) {
@@ -1155,7 +1152,7 @@ sub send_message {
11551152
$message_num = 0;
11561153

11571154
foreach my $t (@files) {
1158-
open(F,"<",$t) or die "can't open file $t";
1155+
open my $fh, "<", $t or die "can't open file $t";
11591156

11601157
my $author = undef;
11611158
my $author_encoding;
@@ -1169,7 +1166,7 @@ sub send_message {
11691166
$message = "";
11701167
$message_num++;
11711168
# First unfold multiline header fields
1172-
while(<F>) {
1169+
while(<$fh>) {
11731170
last if /^\s*$/;
11741171
if (/^\s+\S/ and @header) {
11751172
chomp($header[$#header]);
@@ -1252,7 +1249,7 @@ sub send_message {
12521249
}
12531250
}
12541251
# Now parse the message body
1255-
while(<F>) {
1252+
while(<$fh>) {
12561253
$message .= $_;
12571254
if (/^(Signed-off-by|Cc): (.*)$/i) {
12581255
chomp;
@@ -1269,7 +1266,7 @@ sub send_message {
12691266
$c, $_) unless $quiet;
12701267
}
12711268
}
1272-
close F;
1269+
close $fh;
12731270

12741271
push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
12751272
if defined $to_cmd;
@@ -1340,10 +1337,9 @@ sub recipients_cmd {
13401337

13411338
my $sanitized_sender = sanitize_address($sender);
13421339
my @addresses = ();
1343-
open(F, "$cmd \Q$file\E |")
1340+
open my $fh, "$cmd \Q$file\E |"
13441341
or die "($prefix) Could not execute '$cmd'";
1345-
while(<F>) {
1346-
my $address = $_;
1342+
while (my $address = <$fh>) {
13471343
$address =~ s/^\s*//g;
13481344
$address =~ s/\s*$//g;
13491345
$address = sanitize_address($address);
@@ -1352,20 +1348,20 @@ sub recipients_cmd {
13521348
printf("($prefix) Adding %s: %s from: '%s'\n",
13531349
$what, $address, $cmd) unless $quiet;
13541350
}
1355-
close F
1351+
close $fh
13561352
or die "($prefix) failed to close pipe to '$cmd'";
13571353
return @addresses;
13581354
}
13591355

13601356
cleanup_compose_files();
13611357

1362-
sub cleanup_compose_files() {
1358+
sub cleanup_compose_files {
13631359
unlink($compose_filename, $compose_filename . ".final") if $compose;
13641360
}
13651361

13661362
$smtp->quit if $smtp;
13671363

1368-
sub unique_email_list(@) {
1364+
sub unique_email_list {
13691365
my %seen;
13701366
my @emails;
13711367

t/t9001-send-email.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ test_expect_success $PREREQ 'tocmd works' '
222222
test_expect_success $PREREQ 'cccmd works' '
223223
clean_fake_sendmail &&
224224
cp $patches cccmd.patch &&
225-
echo [email protected] >>cccmd.patch &&
225+
echo "cccmd-- [email protected]" >>cccmd.patch &&
226226
{
227227
echo "#!$SHELL_PATH"
228228
echo sed -n -e s/^cccmd--//p \"\$1\"

0 commit comments

Comments
 (0)