send-email: refactor header generation functions

Split process_file and send_message into easier to use functions. Making SMTP header information widely available. Cc: Luben Tuikov <luben.tuikov@amd.com> Cc: Junio C Hamano <gitster@pobox.com> Cc: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Acked-by: Luben Tuikov <luben.tuikov@amd.com> Signed-off-by: Michael Strawbridge <michael.strawbridge@amd.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Strawbridge committed Apr 19, 2023 at 16:27 UTC 56adddaa06d376f3977ee91e8a769cd85439d21c
1 file changed +31 -18
git-send-email.perl
+31 -18
@@ -1502,16 +1502,7 @@ sub file_name_is_absolute {
1502 return File::Spec::Functions::file_name_is_absolute($path);
1503 }
1504
1505 -# Prepares the email, then asks the user what to do.
1506 -#
1507 -# If the user chooses to send the email, it's sent and 1 is returned.
1508 -# If the user chooses not to send the email, 0 is returned.
1509 -# If the user decides they want to make further edits, -1 is returned and the
1510 -# caller is expected to call send_message again after the edits are performed.
1511 -#
1512 -# If an error occurs sending the email, this just dies.
1513 -
1514 -sub send_message {
1505 +sub gen_header {
1506 my @recipients = unique_email_list(@to);
1507 @cc = (grep { my $cc = extract_valid_address_or_die($_);
1508 not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
@@ -1553,6 +1544,22 @@ Message-ID: $message_id
1544 if (@xh) {
1545 $header .= join("\n", @xh) . "\n";
1546 }
1547 + my $recipients_ref = \@recipients;
1548 + return ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header);
1549 +}
1550 +
1551 +# Prepares the email, then asks the user what to do.
1552 +#
1553 +# If the user chooses to send the email, it's sent and 1 is returned.
1554 +# If the user chooses not to send the email, 0 is returned.
1555 +# If the user decides they want to make further edits, -1 is returned and the
1556 +# caller is expected to call send_message again after the edits are performed.
1557 +#
1558 +# If an error occurs sending the email, this just dies.
1559 +
1560 +sub send_message {
1561 + my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
1562 + my @recipients = @$recipients_ref;
1563
1564 my @sendmail_parameters = ('-i', @recipients);
1565 my $raw_from = $sender;
@@ -1742,11 +1749,8 @@ $in_reply_to = $initial_in_reply_to;
1749 $references = $initial_in_reply_to || '';
1750 $message_num = 0;
1751
1745 -# Prepares the email, prompts the user, sends it out
1746 -# Returns 0 if an edit was done and the function should be called again, or 1
1747 -# otherwise.
1748 -sub process_file {
1749 - my ($t) = @_;
1752 +sub pre_process_file {
1753 + my ($t, $quiet) = @_;
1754
1755 open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
1756
@@ -1900,9 +1904,9 @@ sub process_file {
1904 }
1905 close $fh;
1906
1903 - push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1907 + push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t, $quiet)
1908 if defined $to_cmd;
1905 - push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1909 + push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t, $quiet)
1910 if defined $cc_cmd && !$suppress_cc{'cccmd'};
1911
1912 if ($broken_encoding{$t} && !$has_content_type) {
@@ -1961,6 +1965,15 @@ sub process_file {
1965 @initial_to = @to;
1966 }
1967 }
1968 +}
1969 +
1970 +# Prepares the email, prompts the user, and sends it out
1971 +# Returns 0 if an edit was done and the function should be called again, or 1
1972 +# on the email being successfully sent out.
1973 +sub process_file {
1974 + my ($t) = @_;
1975 +
1976 + pre_process_file($t, $quiet);
1977
1978 my $message_was_sent = send_message();
1979 if ($message_was_sent == -1) {
@@ -2009,7 +2022,7 @@ foreach my $t (@files) {
2022 # Execute a command (e.g. $to_cmd) to get a list of email addresses
2023 # and return a results array
2024 sub recipients_cmd {
2012 - my ($prefix, $what, $cmd, $file) = @_;
2025 + my ($prefix, $what, $cmd, $file, $quiet) = @_;
2026
2027 my @addresses = ();
2028 open my $fh, "-|", "$cmd \Q$file\E"