diff mbox series

[1/2] send-email: extract execute_cmd from recipients_cmd

Message ID 20230423122744.4865-2-maxim.cournoyer@gmail.com (mailing list archive)
State Superseded
Headers show
Series send-email: add --header-cmd option | expand

Commit Message

Maxim Cournoyer April 23, 2023, 12:27 p.m. UTC
This refactor is to pave the way for the addition of the new
'--header-cmd' option to the send-email command.
---
 git-send-email.perl | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

Comments

Junio C Hamano April 24, 2023, 9:46 p.m. UTC | #1
Maxim Cournoyer <maxim.cournoyer@gmail.com> writes:

> This refactor is to pave the way for the addition of the new
> '--header-cmd' option to the send-email command.
> ---
>  git-send-email.perl | 26 ++++++++++++++++++++------
>  1 file changed, 20 insertions(+), 6 deletions(-)

Missing sign-off?

> diff --git a/git-send-email.perl b/git-send-email.perl
> index fd8cd0d46f..d2febbda1f 100755
> --- a/git-send-email.perl
> +++ b/git-send-email.perl
> @@ -2,6 +2,7 @@
>  #
>  # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
>  # Copyright 2005 Ryan Anderson <ryan@michonline.com>
> +# Copyright 2023 Maxim Cournoyer <maxim.cournoyer@gmail.com>
>  #
>  # GPL v2 (See COPYING)
>  #
> @@ -2006,15 +2007,30 @@ sub process_file {
>  	}
>  }
>  
> +# Execute a command (e.g., $x_cmd) and return its output lines as an
> +# array.
> +sub execute_cmd {
> +	my ($prefix, $cmd, $file) = @_;
> +	my @lines = ();
> +	open my $fh, "-|", "$cmd \Q$file\E"
> +		or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
> +	while (my $line = <$fh>) {
> +		last if $line =~ /^$/;
> +		push @lines, $line;
> +	}
> +	close $fh
> +	    or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
> +	return @lines;
> +}
> +
>  # Execute a command (e.g. $to_cmd) to get a list of email addresses
>  # and return a results array
>  sub recipients_cmd {
>  	my ($prefix, $what, $cmd, $file) = @_;
> -
> +	my @lines = ();
>  	my @addresses = ();
> -	open my $fh, "-|", "$cmd \Q$file\E"
> -	    or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
> -	while (my $address = <$fh>) {
> +	@lines = execute_cmd($prefix, $cmd, $file);
> +	for my $address (@lines) {
>  		$address =~ s/^\s*//g;
>  		$address =~ s/\s*$//g;
>  		$address = sanitize_address($address);
> @@ -2023,8 +2039,6 @@ sub recipients_cmd {
>  		printf(__("(%s) Adding %s: %s from: '%s'\n"),
>  		       $prefix, $what, $address, $cmd) unless $quiet;
>  		}
> -	close $fh
> -	    or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
>  	return @addresses;
>  }
Maxim Cournoyer April 25, 2023, 1:55 a.m. UTC | #2
Hello,

And thanks for offering a review!

Junio C Hamano <gitster@pobox.com> writes:

> Maxim Cournoyer <maxim.cournoyer@gmail.com> writes:
>
>> This refactor is to pave the way for the addition of the new
>> '--header-cmd' option to the send-email command.
>> ---
>>  git-send-email.perl | 26 ++++++++++++++++++++------
>>  1 file changed, 20 insertions(+), 6 deletions(-)
>
> Missing sign-off?

Added locally; thanks.
diff mbox series

Patch

diff --git a/git-send-email.perl b/git-send-email.perl
index fd8cd0d46f..d2febbda1f 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -2,6 +2,7 @@ 
 #
 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
+# Copyright 2023 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 #
 # GPL v2 (See COPYING)
 #
@@ -2006,15 +2007,30 @@  sub process_file {
 	}
 }
 
+# Execute a command (e.g., $x_cmd) and return its output lines as an
+# array.
+sub execute_cmd {
+	my ($prefix, $cmd, $file) = @_;
+	my @lines = ();
+	open my $fh, "-|", "$cmd \Q$file\E"
+		or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
+	while (my $line = <$fh>) {
+		last if $line =~ /^$/;
+		push @lines, $line;
+	}
+	close $fh
+	    or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
+	return @lines;
+}
+
 # Execute a command (e.g. $to_cmd) to get a list of email addresses
 # and return a results array
 sub recipients_cmd {
 	my ($prefix, $what, $cmd, $file) = @_;
-
+	my @lines = ();
 	my @addresses = ();
-	open my $fh, "-|", "$cmd \Q$file\E"
-	    or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
-	while (my $address = <$fh>) {
+	@lines = execute_cmd($prefix, $cmd, $file);
+	for my $address (@lines) {
 		$address =~ s/^\s*//g;
 		$address =~ s/\s*$//g;
 		$address = sanitize_address($address);
@@ -2023,8 +2039,6 @@  sub recipients_cmd {
 		printf(__("(%s) Adding %s: %s from: '%s'\n"),
 		       $prefix, $what, $address, $cmd) unless $quiet;
 		}
-	close $fh
-	    or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
 	return @addresses;
 }