Skip to content

Commit 6e74e07

Browse files
JoePerchesgitster
authored andcommitted
git-send-email.perl: Add --to-cmd
Add the ability to use a command line --to-cmd=cmd to create the list of "To:" addresses. Used a shared routine for --cc-cmd and --to-cmd. Did not use IPC::Open2, leaving that for Ævar if ever he decides to fix the other bugs he might find. Signed-off-by: Joe Perches <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9027fa9 commit 6e74e07

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

Documentation/git-send-email.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ See the CONFIGURATION section for 'sendemail.multiedit'.
9797
Specify the primary recipient of the emails generated. Generally, this
9898
will be the upstream maintainer of the project involved. Default is the
9999
value of the 'sendemail.to' configuration value; if that is unspecified,
100-
this will be prompted for.
100+
and --to-cmd is not specified, this will be prompted for.
101101
+
102102
The --to option must be repeated for each user you want on the to list.
103103

@@ -177,6 +177,12 @@ user is prompted for a password while the input is masked for privacy.
177177
Automating
178178
~~~~~~~~~~
179179

180+
--to-cmd=<command>::
181+
Specify a command to execute once per patch file which
182+
should generate patch file specific "To:" entries.
183+
Output of this command must be single email address per line.
184+
Default is the value of 'sendemail.tocmd' configuration value.
185+
180186
--cc-cmd=<command>::
181187
Specify a command to execute once per patch file which
182188
should generate patch file specific "Cc:" entries.

git-send-email.perl

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ sub usage {
7070
7171
Automating:
7272
--identity <str> * Use the sendemail.<id> options.
73+
--to-cmd <str> * Email To: via `<str> \$patch_path`
7374
--cc-cmd <str> * Email Cc: via `<str> \$patch_path`
7475
--suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
7576
--[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
@@ -187,7 +188,8 @@ sub do_edit {
187188
}
188189

189190
# Variables with corresponding config settings
190-
my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd);
191+
my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
192+
my ($to_cmd, $cc_cmd);
191193
my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
192194
my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain);
193195
my ($validate, $confirm);
@@ -214,6 +216,7 @@ sub do_edit {
214216
"smtppass" => \$smtp_authpass,
215217
"smtpdomain" => \$smtp_domain,
216218
"to" => \@to,
219+
"tocmd" => \$to_cmd,
217220
"cc" => \@initial_cc,
218221
"cccmd" => \$cc_cmd,
219222
"aliasfiletype" => \$aliasfiletype,
@@ -272,6 +275,7 @@ sub signal_handler {
272275
"in-reply-to=s" => \$initial_reply_to,
273276
"subject=s" => \$initial_subject,
274277
"to=s" => \@to,
278+
"to-cmd=s" => \$to_cmd,
275279
"no-to" => \$no_to,
276280
"cc=s" => \@initial_cc,
277281
"no-cc" => \$no_cc,
@@ -711,7 +715,7 @@ ($)
711715
$prompting++;
712716
}
713717

714-
if (!@to) {
718+
if (!@to && !defined $to_cmd) {
715719
my $to = ask("Who should the emails be sent to? ");
716720
push @to, parse_address_line($to) if defined $to; # sanitized/validated later
717721
$prompting++;
@@ -1238,21 +1242,10 @@ sub send_message {
12381242
}
12391243
close F;
12401244

1241-
if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
1242-
open(F, "$cc_cmd \Q$t\E |")
1243-
or die "(cc-cmd) Could not execute '$cc_cmd'";
1244-
while(<F>) {
1245-
my $c = $_;
1246-
$c =~ s/^\s*//g;
1247-
$c =~ s/\n$//g;
1248-
next if ($c eq $sender and $suppress_from);
1249-
push @cc, $c;
1250-
printf("(cc-cmd) Adding cc: %s from: '%s'\n",
1251-
$c, $cc_cmd) unless $quiet;
1252-
}
1253-
close F
1254-
or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
1255-
}
1245+
push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1246+
if defined $to_cmd;
1247+
push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1248+
if defined $cc_cmd && !$suppress_cc{'cccmd'};
12561249

12571250
if ($broken_encoding{$t} && !$has_content_type) {
12581251
$has_content_type = 1;
@@ -1310,6 +1303,30 @@ sub send_message {
13101303
$message_id = undef;
13111304
}
13121305

1306+
# Execute a command (e.g. $to_cmd) to get a list of email addresses
1307+
# and return a results array
1308+
sub recipients_cmd {
1309+
my ($prefix, $what, $cmd, $file) = @_;
1310+
1311+
my $sanitized_sender = sanitize_address($sender);
1312+
my @addresses = ();
1313+
open(F, "$cmd \Q$file\E |")
1314+
or die "($prefix) Could not execute '$cmd'";
1315+
while(<F>) {
1316+
my $address = $_;
1317+
$address =~ s/^\s*//g;
1318+
$address =~ s/\s*$//g;
1319+
$address = sanitize_address($address);
1320+
next if ($address eq $sanitized_sender and $suppress_from);
1321+
push @addresses, $address;
1322+
printf("($prefix) Adding %s: %s from: '%s'\n",
1323+
$what, $address, $cmd) unless $quiet;
1324+
}
1325+
close F
1326+
or die "($prefix) failed to close pipe to '$cmd'";
1327+
return @addresses;
1328+
}
1329+
13131330
cleanup_compose_files();
13141331

13151332
sub cleanup_compose_files() {

t/t9001-send-email.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ test_expect_success $PREREQ 'Prompting works' '
201201
grep "^To: [email protected]\$" msgtxt1
202202
'
203203

204+
test_expect_success $PREREQ 'tocmd works' '
205+
clean_fake_sendmail &&
206+
cp $patches tocmd.patch &&
207+
echo [email protected] >>tocmd.patch &&
208+
{
209+
echo "#!$SHELL_PATH"
210+
echo sed -n -e s/^tocmd--//p \"\$1\"
211+
} > tocmd-sed &&
212+
chmod +x tocmd-sed &&
213+
git send-email \
214+
--from="Example <[email protected]>" \
215+
--to-cmd=./tocmd-sed \
216+
--smtp-server="$(pwd)/fake.sendmail" \
217+
tocmd.patch \
218+
&&
219+
grep "^To: [email protected]" msgtxt1
220+
'
221+
204222
test_expect_success $PREREQ 'cccmd works' '
205223
clean_fake_sendmail &&
206224
cp $patches cccmd.patch &&

0 commit comments

Comments
 (0)