Skip to content

Commit 9b1054d

Browse files
committed
Merge branch 'jp/send-email-to-cmd'
* jp/send-email-to-cmd: git-send-email.perl: Add --to-cmd Conflicts: git-send-email.perl
2 parents e6202df + 6e74e07 commit 9b1054d

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

@@ -186,6 +186,12 @@ must be used for each option.
186186
Automating
187187
~~~~~~~~~~
188188

189+
--to-cmd=<command>::
190+
Specify a command to execute once per patch file which
191+
should generate patch file specific "To:" entries.
192+
Output of this command must be single email address per line.
193+
Default is the value of 'sendemail.tocmd' configuration value.
194+
189195
--cc-cmd=<command>::
190196
Specify a command to execute once per patch file which
191197
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
@@ -72,6 +72,7 @@ sub usage {
7272
7373
Automating:
7474
--identity <str> * Use the sendemail.<id> options.
75+
--to-cmd <str> * Email To: via `<str> \$patch_path`
7576
--cc-cmd <str> * Email Cc: via `<str> \$patch_path`
7677
--suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all.
7778
--[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
@@ -191,7 +192,8 @@ sub do_edit {
191192
}
192193

193194
# Variables with corresponding config settings
194-
my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd);
195+
my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
196+
my ($to_cmd, $cc_cmd);
195197
my ($smtp_server, $smtp_server_port, @smtp_server_options);
196198
my ($smtp_authuser, $smtp_encryption);
197199
my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
@@ -220,6 +222,7 @@ sub do_edit {
220222
"smtppass" => \$smtp_authpass,
221223
"smtpdomain" => \$smtp_domain,
222224
"to" => \@to,
225+
"tocmd" => \$to_cmd,
223226
"cc" => \@initial_cc,
224227
"cccmd" => \$cc_cmd,
225228
"aliasfiletype" => \$aliasfiletype,
@@ -278,6 +281,7 @@ sub signal_handler {
278281
"in-reply-to=s" => \$initial_reply_to,
279282
"subject=s" => \$initial_subject,
280283
"to=s" => \@to,
284+
"to-cmd=s" => \$to_cmd,
281285
"no-to" => \$no_to,
282286
"cc=s" => \@initial_cc,
283287
"no-cc" => \$no_cc,
@@ -729,7 +733,7 @@ ($)
729733
$prompting++;
730734
}
731735

732-
if (!@to) {
736+
if (!@to && !defined $to_cmd) {
733737
my $to = ask("Who should the emails be sent to? ");
734738
push @to, parse_address_line($to) if defined $to; # sanitized/validated later
735739
$prompting++;
@@ -1258,21 +1262,10 @@ sub send_message {
12581262
}
12591263
close F;
12601264

1261-
if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
1262-
open(F, "$cc_cmd \Q$t\E |")
1263-
or die "(cc-cmd) Could not execute '$cc_cmd'";
1264-
while(<F>) {
1265-
my $c = $_;
1266-
$c =~ s/^\s*//g;
1267-
$c =~ s/\n$//g;
1268-
next if ($c eq $sender and $suppress_from);
1269-
push @cc, $c;
1270-
printf("(cc-cmd) Adding cc: %s from: '%s'\n",
1271-
$c, $cc_cmd) unless $quiet;
1272-
}
1273-
close F
1274-
or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
1275-
}
1265+
push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
1266+
if defined $to_cmd;
1267+
push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
1268+
if defined $cc_cmd && !$suppress_cc{'cccmd'};
12761269

12771270
if ($broken_encoding{$t} && !$has_content_type) {
12781271
$has_content_type = 1;
@@ -1330,6 +1323,30 @@ sub send_message {
13301323
$message_id = undef;
13311324
}
13321325

1326+
# Execute a command (e.g. $to_cmd) to get a list of email addresses
1327+
# and return a results array
1328+
sub recipients_cmd {
1329+
my ($prefix, $what, $cmd, $file) = @_;
1330+
1331+
my $sanitized_sender = sanitize_address($sender);
1332+
my @addresses = ();
1333+
open(F, "$cmd \Q$file\E |")
1334+
or die "($prefix) Could not execute '$cmd'";
1335+
while(<F>) {
1336+
my $address = $_;
1337+
$address =~ s/^\s*//g;
1338+
$address =~ s/\s*$//g;
1339+
$address = sanitize_address($address);
1340+
next if ($address eq $sanitized_sender and $suppress_from);
1341+
push @addresses, $address;
1342+
printf("($prefix) Adding %s: %s from: '%s'\n",
1343+
$what, $address, $cmd) unless $quiet;
1344+
}
1345+
close F
1346+
or die "($prefix) failed to close pipe to '$cmd'";
1347+
return @addresses;
1348+
}
1349+
13331350
cleanup_compose_files();
13341351

13351352
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)