send-email: handle to/cc/bcc from --compose message

If the user writes a message via --compose, send-email will pick up various headers like "From", "Subject", etc and use them for other patches as if they were specified on the command-line. But we don't handle "To", "Cc", or "Bcc" this way; we just tell the user "those aren't interpeted yet" and ignore them. But it seems like an obvious thing to want, especially as the same feature exists when the cover letter is generated separately by format-patch. There it is gated behind the --to-cover option, but I don't think we'd need the same control here; since we generate the --compose template ourselves based on the existing input, if the user leaves the lines unchanged then the behavior remains the same. So let's fill in the implementation; like those other headers we already handle, we just need to assign to the initial_* variables. The only difference in this case is that they are arrays, so we'll feed them through parse_address_line() to split them (just like we would when reading a single string via prompting). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 20, 2023 at 06:15 UTC 3ec6167567d0e1e03a728a64efa9848310d172ab
3 files changed +31 -12
Documentation/git-send-email.txt
+6 -5
@@ -68,11 +68,12 @@ This option may be specified multiple times.
68 Invoke a text editor (see GIT_EDITOR in linkgit:git-var[1])
69 to edit an introductory message for the patch series.
70 +
71 -When `--compose` is used, git send-email will use the From, Subject,
72 -Reply-To, and In-Reply-To headers specified in the message. If the body
73 -of the message (what you type after the headers and a blank line) only
74 -contains blank (or Git: prefixed) lines, the summary won't be sent, but
75 -the headers mentioned above will be used unless they are removed.
71 +When `--compose` is used, git send-email will use the From, To, Cc, Bcc,
72 +Subject, Reply-To, and In-Reply-To headers specified in the message. If
73 +the body of the message (what you type after the headers and a blank
74 +line) only contains blank (or Git: prefixed) lines, the summary won't be
75 +sent, but the headers mentioned above will be used unless they are
76 +removed.
77 +
78 Missing From or In-Reply-To headers will be prompted for.
79 +
git-send-email.perl
+14 -2
@@ -861,6 +861,9 @@ if ($compose) {
861 my $tpl_subject = $initial_subject || '';
862 my $tpl_in_reply_to = $initial_in_reply_to || '';
863 my $tpl_reply_to = $reply_to || '';
864 + my $tpl_to = join(',', @initial_to);
865 + my $tpl_cc = join(',', @initial_cc);
866 + my $tpl_bcc = join(', ', @initial_bcc);
867
868 print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3;
869 From $tpl_sender # This line is ignored.
@@ -872,6 +875,9 @@ for the patch you are writing.
875 Clear the body content if you don't wish to send a summary.
876 EOT2
877 From: $tpl_sender
878 +To: $tpl_to
879 +Cc: $tpl_cc
880 +Bcc: $tpl_bcc
881 Reply-To: $tpl_reply_to
882 Subject: $tpl_subject
883 In-Reply-To: $tpl_in_reply_to
@@ -928,8 +934,14 @@ EOT3
934 } elsif (/^From:\s*(.+)\s*$/i) {
935 $sender = $1;
936 next;
931 - } elsif (/^(?:To|Cc|Bcc):/i) {
932 - print __("To/Cc/Bcc fields are not interpreted yet, they have been ignored\n");
937 + } elsif (/^To:\s*(.+)\s*$/i) {
938 + @initial_to = parse_address_line($1);
939 + next;
940 + } elsif (/^Cc:\s*(.+)\s*$/i) {
941 + @initial_cc = parse_address_line($1);
942 + next;
943 + } elsif (/^Bcc:/i) {
944 + @initial_bcc = parse_address_line($1);
945 next;
946 }
947 print $c2 $_;
t/t9001-send-email.sh
+11 -5
@@ -2522,7 +2522,7 @@ test_expect_success $PREREQ '--compose handles lowercase headers' '
2522
2523 test_expect_success $PREREQ '--compose handles to headers' '
2524 write_script fake-editor <<-\EOF &&
2525 - sed "s/^$/To: edited-to@example.com\n/" <"$1" >"$1.tmp" &&
2525 + sed "s/^To: .*/&, edited-to@example.com/" <"$1" >"$1.tmp" &&
2526 echo this is the body >>"$1.tmp" &&
2527 mv "$1.tmp" "$1"
2528 EOF
@@ -2534,10 +2534,16 @@ test_expect_success $PREREQ '--compose handles to headers' '
2534 --to=nobody@example.com \
2535 --smtp-server="$(pwd)/fake.sendmail" \
2536 HEAD^ &&
2537 - # Ideally the "to" header we specified would be used,
2538 - # but the program explicitly warns that these are
2539 - # ignored. For now, just make sure we did not abort.
2540 - grep "To:" msgtxt1
2537 + # Check both that the cover letter used our modified "to" line,
2538 + # but also that it was picked up for the patch.
2539 + q_to_tab >expect <<-\EOF &&
2540 + To: nobody@example.com,
2541 + Qedited-to@example.com
2542 + EOF
2543 + grep -A1 "^To:" msgtxt1 >msgtxt1.to &&
2544 + test_cmp expect msgtxt1.to &&
2545 + grep -A1 "^To:" msgtxt2 >msgtxt2.to &&
2546 + test_cmp expect msgtxt2.to
2547 '
2548
2549 test_done