Skip to content

Commit 0d86470

Browse files
Michael Strawbridgegitster
authored andcommitted
send-email: move validation code below process_address_list
Move validation logic below processing of email address lists so that email validation gets the proper email addresses. As a side effect, some initialization needed to be moved down. In order for validation and the actual email sending to have the same initial state, the initialized variables that get modified by pre_process_file are encapsulated in a new function. This fixes email address validation errors when the optional perl module Email::Valid is installed and multiple addresses are passed in on a single to/cc argument like [email protected],[email protected]. A new test was added to t9001 to expose failures with this case in the future. Reported-by: Bagas Sanjaya <[email protected]> Signed-off-by: Michael Strawbridge <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb7d80e commit 0d86470

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

git-send-email.perl

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -811,30 +811,6 @@ sub is_format_patch_arg {
811811

812812
$time = time - scalar $#files;
813813

814-
if ($validate) {
815-
# FIFOs can only be read once, exclude them from validation.
816-
my @real_files = ();
817-
foreach my $f (@files) {
818-
unless (-p $f) {
819-
push(@real_files, $f);
820-
}
821-
}
822-
823-
# Run the loop once again to avoid gaps in the counter due to FIFO
824-
# arguments provided by the user.
825-
my $num = 1;
826-
my $num_files = scalar @real_files;
827-
$ENV{GIT_SENDEMAIL_FILE_TOTAL} = "$num_files";
828-
foreach my $r (@real_files) {
829-
$ENV{GIT_SENDEMAIL_FILE_COUNTER} = "$num";
830-
pre_process_file($r, 1);
831-
validate_patch($r, $target_xfer_encoding);
832-
$num += 1;
833-
}
834-
delete $ENV{GIT_SENDEMAIL_FILE_COUNTER};
835-
delete $ENV{GIT_SENDEMAIL_FILE_TOTAL};
836-
}
837-
838814
@files = handle_backup_files(@files);
839815

840816
if (@files) {
@@ -1764,10 +1740,6 @@ sub send_message {
17641740
return 1;
17651741
}
17661742

1767-
$in_reply_to = $initial_in_reply_to;
1768-
$references = $initial_in_reply_to || '';
1769-
$message_num = 0;
1770-
17711743
sub pre_process_file {
17721744
my ($t, $quiet) = @_;
17731745

@@ -2033,6 +2005,38 @@ sub process_file {
20332005
return 1;
20342006
}
20352007

2008+
sub initialize_modified_loop_vars {
2009+
$in_reply_to = $initial_in_reply_to;
2010+
$references = $initial_in_reply_to || '';
2011+
$message_num = 0;
2012+
}
2013+
2014+
if ($validate) {
2015+
# FIFOs can only be read once, exclude them from validation.
2016+
my @real_files = ();
2017+
foreach my $f (@files) {
2018+
unless (-p $f) {
2019+
push(@real_files, $f);
2020+
}
2021+
}
2022+
2023+
# Run the loop once again to avoid gaps in the counter due to FIFO
2024+
# arguments provided by the user.
2025+
my $num = 1;
2026+
my $num_files = scalar @real_files;
2027+
$ENV{GIT_SENDEMAIL_FILE_TOTAL} = "$num_files";
2028+
initialize_modified_loop_vars();
2029+
foreach my $r (@real_files) {
2030+
$ENV{GIT_SENDEMAIL_FILE_COUNTER} = "$num";
2031+
pre_process_file($r, 1);
2032+
validate_patch($r, $target_xfer_encoding);
2033+
$num += 1;
2034+
}
2035+
delete $ENV{GIT_SENDEMAIL_FILE_COUNTER};
2036+
delete $ENV{GIT_SENDEMAIL_FILE_TOTAL};
2037+
}
2038+
2039+
initialize_modified_loop_vars();
20362040
foreach my $t (@files) {
20372041
while (!process_file($t)) {
20382042
# user edited the file

t/t9001-send-email.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,25 @@ test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" '
632632
test_cmp expect actual
633633
'
634634

635+
test_expect_success $PREREQ "--validate hook supports multiple addresses in arguments" '
636+
hooks_path="$(pwd)/my-hooks" &&
637+
test_config core.hooksPath "$hooks_path" &&
638+
test_when_finished "rm my-hooks.ran" &&
639+
test_must_fail git send-email \
640+
--from="Example <[email protected]>" \
641+
642+
--smtp-server="$(pwd)/fake.sendmail" \
643+
--validate \
644+
longline.patch 2>actual &&
645+
test_path_is_file my-hooks.ran &&
646+
cat >expect <<-EOF &&
647+
fatal: longline.patch: rejected by sendemail-validate hook
648+
fatal: command '"'"'git hook run --ignore-missing sendemail-validate -- <patch> <header>'"'"' died with exit code 1
649+
warning: no patches were sent
650+
EOF
651+
test_cmp expect actual
652+
'
653+
635654
test_expect_success $PREREQ "--validate hook supports header argument" '
636655
write_script my-hooks/sendemail-validate <<-\EOF &&
637656
if test "$#" -ge 2

0 commit comments

Comments
 (0)