Skip to content

Commit 03056ce

Browse files
apteryksgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 48d89b5 commit 03056ce

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

git-send-email.perl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,15 +2021,29 @@ sub process_file {
20212021
}
20222022
}
20232023

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+
20242039
# Execute a command (e.g. $to_cmd) to get a list of email addresses
20252040
# and return a results array
20262041
sub recipients_cmd {
20272042
my ($prefix, $what, $cmd, $file) = @_;
2028-
2043+
my @lines = ();
20292044
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) {
20332047
$address =~ s/^\s*//g;
20342048
$address =~ s/\s*$//g;
20352049
$address = sanitize_address($address);
@@ -2038,8 +2052,6 @@ sub recipients_cmd {
20382052
printf(__("(%s) Adding %s: %s from: '%s'\n"),
20392053
$prefix, $what, $address, $cmd) unless $quiet;
20402054
}
2041-
close $fh
2042-
or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
20432055
return @addresses;
20442056
}
20452057

0 commit comments

Comments
 (0)