send-email: clear the $message_id after validation

Recently git-send-email started parsing the same message twice, once to validate _all_ the message before sending even the first one, and then after the validation hook is happy and each message gets sent, to read the contents to find out where to send to etc. Unfortunately, the effect of reading the messages for validation lingered even after the validation is done. Namely $message_id gets assigned if exists in the input files but the variable is global, and it is not cleared before pre_process_file runs. This causes reading a message without a message-id followed by reading a message with a message-id to misbehave---the sub reports as if the message had the same id as the previously written one. Clear the variable before starting to read the headers in pre_process_file. Tested-by: Douglas Anderson <dianders@chromium.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 17, 2023 at 14:10 UTC 3ece9bf0f9e24909b090cf348d89e8920bd4f82f
2 files changed +18 -1
git-send-email.perl
+2
@@ -1768,6 +1768,8 @@ sub pre_process_file {
1768 $subject = $initial_subject;
1769 $message = "";
1770 $message_num++;
1771 + undef $message_id;
1772 +
1773 # First unfold multiline header fields
1774 while(<$fh>) {
1775 last if /^\s*$/;
t/t9001-send-email.sh
+16 -1
@@ -47,7 +47,7 @@ clean_fake_sendmail () {
47
48 test_expect_success $PREREQ 'Extract patches' '
49 patches=$(git format-patch -s --cc="One <one@example.com>" --cc=two@example.com -n HEAD^1) &&
50 - threaded_patches=$(git format-patch -o threaded -s --in-reply-to="format" HEAD^1)
50 + threaded_patches=$(git format-patch -o threaded --thread=shallow -s --in-reply-to="format" HEAD^1)
51 '
52
53 # Test no confirm early to ensure remaining tests will not hang
@@ -588,6 +588,21 @@ test_expect_success $PREREQ "--validate hook supports header argument" '
588 outdir/000?-*.patch
589 '
590
591 +test_expect_success $PREREQ 'clear message-id before parsing a new message' '
592 + clean_fake_sendmail &&
593 + echo true | write_script my-hooks/sendemail-validate &&
594 + test_config core.hooksPath my-hooks &&
595 + GIT_SEND_EMAIL_NOTTY=1 \
596 + git send-email --validate --to=recipient@example.com \
597 + --smtp-server="$(pwd)/fake.sendmail" \
598 + $patches $threaded_patches &&
599 + id0=$(grep "^Message-ID: " $threaded_patches) &&
600 + id1=$(grep "^Message-ID: " msgtxt1) &&
601 + id2=$(grep "^Message-ID: " msgtxt2) &&
602 + test "z$id0" = "z$id2" &&
603 + test "z$id1" != "z$id2"
604 +'
605 +
606 for enc in 7bit 8bit quoted-printable base64
607 do
608 test_expect_success $PREREQ "--transfer-encoding=$enc produces correct header" '