send-email: extract execute_cmd from recipients_cmd

This refactor is to pave the way for the addition of the new '--header-cmd' option to the send-email command. Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Maxim Cournoyer committed May 1, 2023 at 10:38 UTC 03056ce796e3a0cde71b8e4776d3453364cfda78
1 file changed +18 -6
git-send-email.perl
+18 -6
@@ -2021,15 +2021,29 @@ foreach my $t (@files) {
2021 }
2022 }
2023
2024 +# Execute a command and return its output lines as an array.
2025 +sub execute_cmd {
2026 + my ($prefix, $cmd, $file) = @_;
2027 + my @lines = ();
2028 + open my $fh, "-|", "$cmd \Q$file\E"
2029 + or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
2030 + while (my $line = <$fh>) {
2031 + last if $line =~ /^$/;
2032 + push @lines, $line;
2033 + }
2034 + close $fh
2035 + or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
2036 + return @lines;
2037 +}
2038 +
2039 # Execute a command (e.g. $to_cmd) to get a list of email addresses
2040 # and return a results array
2041 sub recipients_cmd {
2042 my ($prefix, $what, $cmd, $file) = @_;
2028 -
2043 + my @lines = ();
2044 my @addresses = ();
2030 - open my $fh, "-|", "$cmd \Q$file\E"
2031 - or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
2032 - while (my $address = <$fh>) {
2045 + @lines = execute_cmd($prefix, $cmd, $file);
2046 + for my $address (@lines) {
2047 $address =~ s/^\s*//g;
2048 $address =~ s/\s*$//g;
2049 $address = sanitize_address($address);
@@ -2038,8 +2052,6 @@ sub recipients_cmd {
2052 printf(__("(%s) Adding %s: %s from: '%s'\n"),
2053 $prefix, $what, $address, $cmd) unless $quiet;
2054 }
2041 - close $fh
2042 - or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
2055 return @addresses;
2056 }
2057