git-send-email: add sendmailCommand option
PATCH 1/1 git-send-email: add sendmailCommand option
The sendemail.smtpServer option currently supports using a sendmail-like program to send emails by specifying an absolute file path. However, this is not ideal for the following reasons:
1. It overloads the meaning of smtpServer (now a program is being used for the server?)
2. It doesn't allow for non-absolute paths, arguments, or arbitrary scripting.
Requiring an absolute path is bad for portability, as the same program may be in different locations on different systems. If I wish to pass arguments to my program, I have to use the smtpServerOption option, which is cumbersome (as it must be repeated for each option) and doesn't adhere to normal git conventions.
This patch attempts to solve these problems by introducing a new configuration option sendemail.sendmailCommand as well as a command line option --sendmail-cmd. The value of this option is invoked with the standard sendmail options passed as arguments.
sendmailCommand has precedence over smtpServer. If both options are unspecified, the default is to search for 'sendmail' in /usr/sbin, /usr/lib, and $PATH. If not found, smtpServer is set to localhost. This mimics the current behavior when smtpServer is unspecified.
The option is passed to Perl's exec() function, which automatically determines whether or not to invoke a shell. If shell metacharacters are detected, then a shell is used; otherwise, the command is invoked directly. This means that e.g.
[sendemail]
sendmailCommand = f() { if [ "$(uname -s) = Darwin ]; then sendmail "$@"; else postfix "$@"; fi; };fwill be executed in a shell (as expected), while
[sendemail]
sendmailCommand = msmtp
will be executed directly.
This change deprecates the use of absolute paths in sendemail.smtpServer, although support is kept for backward compatibility.
---
Note that this patch is incompatible with (and supersedes) the patch discussed here:
https://public-inbox.org/git/[email protected]/T/#t
Documentation/git-send-email.txt | 39 ++++--- git-send-email.perl | 30 ++++-- t/t9001-send-email.sh | 169 ++++++++++++++++++------------- 3 files changed, 140 insertions(+), 98 deletions(-)
Documentation/git-send-email.txt +25 -14
@@ -159,13 +159,23 @@ Sending ~~~~~~~ --envelope-sender=<address>:: - Specify the envelope sender used to send the emails. - This is useful if your default address is not the address that is - subscribed to a list. In order to use the 'From' address, set the - value to "auto". If you use the sendmail binary, you must have - suitable privileges for the -f parameter. Default is the value of the - `sendemail.envelopeSender` configuration variable; if that is - unspecified, choosing the envelope sender is left to your MTA. + Specify the envelope sender used to send the emails. This is + useful if your default address is not the address that is + subscribed to a list. In order to use the 'From' address, set + the value to "auto". If you use the sendmail binary, you must + have suitable privileges for the -f parameter. Default is the + value of the `sendemail.envelopeSender` configuration variable; + if that is unspecified, choosing the envelope sender is left to + your MTA. + +--sendmail-cmd=<command>:: + Specify a command to run to send the email. The command should + be compatible with `sendmail` as the arguments are passed + directly. The command will be executed in the shell if + necessary. Default is the value of `sendemail.sendmailCommand`. + If unspecified, and if --smtp-server is also unspecified, + git-send-email will search for `sendmail` in `/usr/sbin`, + `/usr/lib` and $PATH if such a program is available. --smtp-encryption=<encryption>:: Specify the encryption to use, either 'ssl' or 'tls'. Any other
@@ -211,13 +221,14 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in - `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise. + `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. + + For backward compatibility, this option can also specify a full + pathname of a sendmail-like program instead; the program must + support the `-i` option. Prefer using `--sendmail-cmd` instead. --smtp-server-port=<port>:: Specifies a port different from the default port (SMTP
git-send-email.perl +19 -11
@@ -70,6 +70,7 @@ sub usage { Sending: --envelope-sender <str> * Email envelope sender. + --sendmail-cmd <str> * Shell command to run to send email. --smtp-server <str:int> * Outgoing SMTP server to use. The port is optional. Default 'localhost'. --smtp-server-option <str> * Outgoing SMTP server option to use.
@@ -252,6 +253,7 @@ sub do_edit { my (@suppress_cc); my ($auto_8bit_encoding); my ($compose_encoding); +my ($sendmail_command); # Variables with corresponding config settings & hardcoded defaults my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() my $thread = 1;
@@ -299,6 +301,7 @@ sub do_edit { "assume8bitencoding" => \$auto_8bit_encoding, "composeencoding" => \$compose_encoding, "transferencoding" => \$target_xfer_encoding, + "sendmailcommand" => \$sendmail_command, ); my %config_path_settings = (
@@ -432,6 +435,7 @@ sub read_config { "no-bcc" => \$no_bcc, "chain-reply-to!" => \$chain_reply_to, "no-chain-reply-to" => sub {$chain_reply_to = 0}, + "sendmail-cmd=s" => \$sendmail_command, "smtp-server=s" => \$smtp_server, "smtp-server-option=s" => \@smtp_server_options, "smtp-server-port=s" => \$smtp_server_port,
@@ -1008,11 +1012,14 @@ sub expand_one_alias { push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH}; foreach (@sendmail_paths) { if (-x $_) { - $smtp_server = $_; + $sendmail_command ||= $_; last; } } - $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* + + if (!defined $sendmail_command) { + $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* + } } if ($compose && $compose > 0) {
@@ -1490,14 +1497,15 @@ sub send_message { unshift (@sendmail_parameters, @smtp_server_options); + if (file_name_is_absolute($smtp_server)) { + # Preserved for backward compatibility + $sendmail_command ||= $smtp_server; + } + if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { - my $pid = open my $sm, '|-'; - defined $pid or die $!; - if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!; - } + } elsif (defined $sendmail_command) { + open my $sm, '|-', "$sendmail_command @sendmail_parameters"; print $sm "$header\n$message"; close $sm or die $!; } else {
@@ -1592,14 +1600,14 @@ sub send_message { printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); } else { print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); - if (!file_name_is_absolute($smtp_server)) { + if (defined $sendmail_command) { + print "Sendmail: $sendmail_command ".join(' ',@sendmail_parameters)."\n"; + } else { print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; foreach my $entry (@recipients) { print "RCPT TO:<$entry>\n"; } - } else { - print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; } print $header, "\n"; if ($smtp) {
t/t9001-send-email.sh +96 -73
613 lines not on this page · raw message
Signature · 1 line
2.31.1.576.g12e560b5fe
Quoted 2 lines · Gregory Anders
The sendemail.smtpServer option currently supports using a sendmail-like program to send emails by specifying an absolute file path. However,
That is not wrong per-se, but it is not limited to the configuration variable, but is a shared trait with --smtp-server command line option. It is easier on the readers to mention both.
Our problem description talks about the status quo in the present tense. No noiseword "currently " necessary. I.e. something along this line:
The sendemail.smtpServer configuration variable (and the "--smtp-server" command line option of "git send-email" command) allows to name a command to run to send emails by specifying an absolute path name. However,
Quoted 7 lines
this is not ideal for the following reasons:
1. It overloads the meaning of smtpServer (now a program is being used for the server?)
2. It doesn't allow for non-absolute paths, arguments, or arbitrary scripting.
Quoted 5 lines
Requiring an absolute path is bad for portability, as the same program may be in different locations on different systems. If I wish to pass arguments to my program, I have to use the smtpServerOption option, which is cumbersome (as it must be repeated for each option) and doesn't adhere to normal git conventions.
Up to here, nice explanation of the background and description of the problem being solved.
Quoted 4 lines
This patch attempts to solve these problems by introducing a new configuration option sendemail.sendmailCommand as well as a command line option --sendmail-cmd. The value of this option is invoked with the standard sendmail options passed as arguments.
When presenting a potential solution, in the history of this project, we'd talk as if we are giving an order to the codebase to "be like so".
Introduce a command line option '--sendmail-cmd' and a configuration variable sendemail.sendmailCommand that can be used to specify the command line (possibly including its command line options) to send pieces of e-mail. This is invoked while honoring $PATH, so it does not have to be named with an absolute path to the command.
Give it a higher precedence over --smtp-server (and sendemail.smtpServer), as the new interface is more flexible.
Quoted 4 lines
sendmailCommand has precedence over smtpServer. If both options are unspecified, the default is to search for 'sendmail' in
/usr/sbin,/usr/lib, and $PATH. If not found, smtpServer is set to localhost. This mimics the current behavior when smtpServer is unspecified.
I do not think "If both options are unspecified" and everything after it is needed.
Quoted 4 lines
The option is passed to Perl's
exec()function, which automatically determines whether or not to invoke a shell. If shell metacharacters are detected, then a shell is used; otherwise, the command is invoked directly.
I do not think this, and the two examples below (omitted), are relevant, either.
The "metacharacters make the command diverted to shell" is a mere optimization and not of interest to the end users. Even if sendemail.sendmailcommand is set to just a single word 'msmtp', which does not have any metacharacter, we _could_ spawn it via the shell and the observable end result would be the same as if the single word was directly executed without the shell.
Quoted 3 lines
This change deprecates the use of absolute paths in sendemail.smtpServer, although support is kept for backward compatibility.
I am on the fence about saying this. We may eventually want to deprecate, but until we start issuing a warning when the absolute-path form is used, I'd rather not to call it "deprecated" in either the proposed log message or in the documenation.
Quoted 1 line
---
Missing sign-off.
Quoted 5 lines
Note that this patch is incompatible with (and supersedes) the patch discussed here:
Thanks---such a note is very valuable.
Quoted 13 lines
Documentation/git-send-email.txt +0
-5@@ -159,13 +159,23 @@ Sending ~~~~~~~ --envelope-sender=<address>:: - Specify the envelope sender used to send the emails. - This is useful if your default address is not the address that is - subscribed to a list. In order to use the 'From' address, set the - value to "auto". If you use the sendmail binary, you must have - suitable privileges for the -f parameter. Default is the value of the
Documentation/git-send-email.txt Sending · line 169
Quoted 10 lines
Documentation/git-send-email.txt +8
-2- `sendemail.envelopeSender` configuration variable; if that is - unspecified, choosing the envelope sender is left to your MTA. + Specify the envelope sender used to send the emails. This is + useful if your default address is not the address that is + subscribed to a list. In order to use the 'From' address, set + the value to "auto". If you use the sendmail binary, you must + have suitable privileges for the -f parameter. Default is the + value of the `sendemail.envelopeSender` configuration variable; + if that is unspecified, choosing the envelope sender is left to + your MTA.
Is this a totally unwarranted rewrapping of an unrelated part of the same document, or was there some words or phrases in this description of the envelope-sender option that needed to be adjusted for the introduction of sendmail-cmd option?
Documentation/git-send-email.txt Sending · line 178
Quoted 8 lines
Documentation/git-send-email.txt +8
-0+--sendmail-cmd=<command>:: + Specify a command to run to send the email. The command should + be compatible with `sendmail` as the arguments are passed + directly. The command will be executed in the shell if + necessary. Default is the value of `sendemail.sendmailCommand`. + If unspecified, and if --smtp-server is also unspecified, + git-send-email will search for `sendmail` in `/usr/sbin`, + `/usr/lib` and $PATH if such a program is available.
OK, but doesn't this also need to support '-i'?
Quoted 9 lines
Documentation/git-send-email.txt +0
-5@@ -211,13 +221,14 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in
Documentation/git-send-email.txt a password is obtained using 'git-credential'. · line 231
Quoted 10 lines
Documentation/git-send-email.txt +8
-2- `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise. + `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. + + For backward compatibility, this option can also specify a full + pathname of a sendmail-like program instead; the program must + support the `-i` option. Prefer using `--sendmail-cmd` instead.
Drop the last sentence, if we are not going to explain why. Or perhaps:
... an absolute path to a program that behaves like sendmail (among other things, it must support the -i option). As you only can specify the path to the program and cannot give any leading arguments to it, consider using --sendmail-cmd instead.
Documentation/git-send-email.txt sub send_message { · line 1503
Quoted 8 lines
Documentation/git-send-email.txt +4
-0@@ -1490,14 +1497,15 @@ sub send_message { unshift (@sendmail_parameters, @smtp_server_options); + if (file_name_is_absolute($smtp_server)) { + # Preserved for backward compatibility + $sendmail_command ||= $smtp_server; + }
Hmph, I wonder if this makes the intent more clear.
if (!defined $sendmail_command && file_name_is_absolute($smtp_server)) {
$sendmail_command = $smtp_server;
}That is, if the user gave us the command in newer form, we do not even have to bother checking if the server is given as an absolute pathname.
Documentation/git-send-email.txt test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' ' · line 1075
Quoted 9 lines
Documentation/git-send-email.txt +1
-1@@ -1069,7 +1069,7 @@ test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' ' git send-email \ --from="Example <[email protected]>" \ [email protected] \ - --smtp-server="$(pwd)/fake.sendmail" \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\"" \ outdir/*.patch && grep "^ " msgtxt1 | grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <[email protected]>"
You seem to have replaced every smtp-server="$(pwd)/ mechanically with sendmai-cmd=\"$(pwd)/, but please make sure that we have at least one test left that passes an absolute path to --smtp-server to ensure that the old mechanism keeps working. A bonus point for marking such a test that needs to be adjusted when the actual deprecation happens (i.e. we'd likely to detect the use of absolute path and throw a warning, so the test should notice the warning message).
Also you would want to tweak some of the --sendmail-cmd variants to use just the command name, with and without args, to ensure that (1) discovery on $PATH works, and (2) passing initial args works.
Thanks.
Quoted 11 lines · Gregory Anders
Documentation/git-send-email.txt +0
-5@@ -159,13 +159,23 @@ Sending ~~~~~~~ --envelope-sender=<address>:: - Specify the envelope sender used to send the emails. - This is useful if your default address is not the address that is - subscribed to a list. In order to use the 'From' address, set the - value to "auto". If you use the sendmail binary, you must have - suitable privileges for the -f parameter. Default is the value of the
Documentation/git-send-email.txt Sending · line 169
Quoted 10 lines
Documentation/git-send-email.txt +8
-2- `sendemail.envelopeSender` configuration variable; if that is - unspecified, choosing the envelope sender is left to your MTA. + Specify the envelope sender used to send the emails. This is + useful if your default address is not the address that is + subscribed to a list. In order to use the 'From' address, set + the value to "auto". If you use the sendmail binary, you must + have suitable privileges for the -f parameter. Default is the + value of the `sendemail.envelopeSender` configuration variable; + if that is unspecified, choosing the envelope sender is left to + your MTA.
I'm not against these kinds of changes but it took me one minute to figure out all you did was change the format.
This belongs in a separate patch.
Documentation/git-send-email.txt Sending · line 171
Quoted 1 line
Documentation/git-send-email.txt +1
-0+--sendmail-cmd=<command>::
Oh no no no. Don't do shortcuts.
If you think --sendmail-command is too long, then address that problem head on, don't try to hide it.
I do think it's too long, which is why I suggested --command (especially since it's obvious which command we are talking about), but I wouldn't suggest --sdm-command, or something of that sort. We have to own our decisions.
1. --command
2. --sendmail
3. --sendmail-command
We have to pick one. I suggest #1.
To try to make #3 shorter is just shoving the problem under the rug.
Quoted 27 lines
git-send-email.perl +3
-0@@ -70,6 +70,7 @@ sub usage { Sending: --envelope-sender <str> * Email envelope sender. + --sendmail-cmd <str> * Shell command to run to send email. --smtp-server <str:int> * Outgoing SMTP server to use. The port is optional. Default 'localhost'. --smtp-server-option <str> * Outgoing SMTP server option to use.@@ -252,6 +253,7 @@ sub do_edit { my (@suppress_cc); my ($auto_8bit_encoding); my ($compose_encoding); +my ($sendmail_command); # Variables with corresponding config settings & hardcoded defaults my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() my $thread = 1;@@ -299,6 +301,7 @@ sub do_edit { "assume8bitencoding" => \$auto_8bit_encoding, "composeencoding" => \$compose_encoding, "transferencoding" => \$target_xfer_encoding, + "sendmailcommand" => \$sendmail_command, ); my %config_path_settings = (
git-send-email.perl sub read_config { · line 438
Quoted 4 lines
git-send-email.perl +1
-0@@ -432,6 +435,7 @@ sub read_config { "no-bcc" => \$no_bcc, "chain-reply-to!" => \$chain_reply_to, "no-chain-reply-to" => sub {$chain_reply_to = 0}, + "sendmail-cmd=s" => \$sendmail_command,
Isn't it interesting that to make the code readable you picked $sendmail_command, but you don't want users to type so much, even if it's more readable?
Once again: "$command=s" -> \$command,
t/t9001-send-email.sh test_no_confirm () { · line 60
Quoted 8 lines
t/t9001-send-email.sh +1
-1@@ -57,7 +57,7 @@ test_no_confirm () { git send-email \ --from="Example <[email protected]>" \ [email protected] \ - --smtp-server="$(pwd)/fake.sendmail" \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\"" \
People are already using --smpt-server=$cmd, we need to keep testing that.
Yes, eventually we would want them to move to --sendmail-cmd (or --command, or whatever), but that won't happen tomorrow. Therefore our primary tests need to be focused on --smtp-server.
We need new *additional* tests for --sendmail-cmd, but those should not override the current tests. At least not right now.
Cheers.
Signature · 1 line
Felipe Contreras
Quoted 13 lines · Gregory Anders
Documentation/git-send-email.txt +0
-5@@ -159,13 +159,23 @@ Sending ~~~~~~~ --envelope-sender=<address>:: - Specify the envelope sender used to send the emails. - This is useful if your default address is not the address that is - subscribed to a list. In order to use the 'From' address, set the - value to "auto". If you use the sendmail binary, you must have - suitable privileges for the -f parameter. Default is the value of the
Documentation/git-send-email.txt Sending · line 169
Quoted 10 lines
Documentation/git-send-email.txt +8
-2- `sendemail.envelopeSender` configuration variable; if that is - unspecified, choosing the envelope sender is left to your MTA. + Specify the envelope sender used to send the emails. This is + useful if your default address is not the address that is + subscribed to a list. In order to use the 'From' address, set + the value to "auto". If you use the sendmail binary, you must + have suitable privileges for the -f parameter. Default is the + value of the `sendemail.envelopeSender` configuration variable; + if that is unspecified, choosing the envelope sender is left to + your MTA.
Please don't include word-wrapping for unrelated changes in the main patch.
Documentation/git-send-email.txt Sending · line 174
Quoted 6 lines
Documentation/git-send-email.txt +4
-1- $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* + + if (!defined $sendmail_command) { + $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* + } }
This "let's not accept a 0" change seems unrelated & should be split into a prep cleanup / refactoring patch. On the one hand it's sensible, on the other nobody cares about having a command named "0" in their path (or a hostname), so I think it's fine to have the ||= Perl idiom leak out here.
But also, this just seems like confusing logic. Per your docs "your sendmailCommand has precedence over smtpServer.".
Why not make this "if not $sendmail_command" part of the top-level check here (the if this one is nested under), which is only done if $smtp_sever is not defined, if $sendmail_command is defined we don't care about $smtp_server later on, no?
Quoted 10 lines
Documentation/git-send-email.txt +5
-0if ($compose && $compose > 0) {@@ -1490,14 +1497,15 @@ sub send_message { unshift (@sendmail_parameters, @smtp_server_options); + if (file_name_is_absolute($smtp_server)) { + # Preserved for backward compatibility + $sendmail_command ||= $smtp_server; + } +
Documentation/git-send-email.txt sub send_message { · line 1508
Quoted 10 lines
Documentation/git-send-email.txt +2
-6if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { - my $pid = open my $sm, '|-'; - defined $pid or die $!; - if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!; - } + } elsif (defined $sendmail_command) { + open my $sm, '|-', "$sendmail_command @sendmail_parameters";
Can we really not avoid moving from exec-as-list so Perl quotes everything, to doing our own interpolation here? It looks like the tests don't check arguments with whitespace (which should fail with this change).
Documentation/git-send-email.txt sub send_message { · line 1511
Quoted 3 lines
Documentation/git-send-email.txt +0
-0print $sm "$header\n$message"; close $sm or die $!; } else {
I've just skimmed the previous thread, so forgive me if this was brought up.
I for one would be fine with just using --smtp-server and not adding an --sendmail-command, and doing this by simply doing an exec() on whatever the user specifies.
If it's an absolute path and an executable command, OK. If it's a command name we find in $PATH, OK, or other valid shell whatever. You can use $? to distinguish between a failed and nonexisting command.
If not exec() will return and we continue resolving it as a hostname/IP address/whatever. We'll have a conflict if someone has a command in their $PATH called gmail.com or whatever, but really. Who does that?
Maybe it's way too nasty. This design is also fine, just a suggestion.
Quoted 8 lines
Documentation/git-send-email.txt +3
-1@@ -1592,14 +1600,14 @@ sub send_message { printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); } else { print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); - if (!file_name_is_absolute($smtp_server)) { + if (defined $sendmail_command) { + print "Sendmail: $sendmail_command ".join(' ',@sendmail_parameters)."\n"; + } else {
Documentation/git-send-email.txt sub send_message { · line 1613
Quoted 10 lines
Documentation/git-send-email.txt +0
-2print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; foreach my $entry (@recipients) { print "RCPT TO:<$entry>\n"; } - } else { - print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; } print $header, "\n"; if ($smtp) {
Minor nit: Let's just continue to use "if (!" here to keep the diff minimal or split up such refactoring into another change...
Quoted 4 lines · Junio C Hamano
Is this a totally unwarranted rewrapping of an unrelated part of the same document, or was there some words or phrases in this description of the envelope-sender option that needed to be adjusted for the introduction of sendmail-cmd option?
Yes it's just a rewrapping that I did while adding my new paragraph. The other reviewers pointed this out as well. My mistake, I will remove this from the next patch revision.
Quoted 10 lines
Review
Quoted 8 lines
+--sendmail-cmd=<command>::
(quoted diff) +7
-0+ Specify a command to run to send the email. The command should + be compatible with `sendmail` as the arguments are passed + directly. The command will be executed in the shell if + necessary. Default is the value of `sendemail.sendmailCommand`. + If unspecified, and if --smtp-server is also unspecified, + git-send-email will search for `sendmail` in `/usr/sbin`, + `/usr/lib` and $PATH if such a program is available.OK, but doesn't this also need to support '-i'?
'The command should be compatible with sendmail' was meant to imply this, though I can make this more explicit.
Quoted 13 lines
Quoted 13 lines
(quoted diff) +2
-7@@ -211,13 +221,14 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in - `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise. + `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search
Quoted 8 lines
Review a password is obtained using 'git-credential'. · line 231
Quoted 6 lines
(quoted diff) +6
-0+ for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. + + For backward compatibility, this option can also specify a full + pathname of a sendmail-like program instead; the program must + support the `-i` option. Prefer using `--sendmail-cmd` instead.Drop the last sentence, if we are not going to explain why.
I do think nudging users to use the "correct" option is valuable, so I will add some why text. Though I think adhering to the "--smtp-server should specify a host and --sendmail-cmd specifies a command" dichotomy is a good reason in and of itself.
Quoted 11 lines
Review sub send_message { · line 1503
Quoted 8 lines
(quoted diff) +4
-0@@ -1490,14 +1497,15 @@ sub send_message { unshift (@sendmail_parameters, @smtp_server_options); + if (file_name_is_absolute($smtp_server)) { + # Preserved for backward compatibility + $sendmail_command ||= $smtp_server; + }Hmph, I wonder if this makes the intent more clear.
Quoted 7 lines
if (!defined $sendmail_command && file_name_is_absolute($smtp_server)) { $sendmail_command = $smtp_server; }That is, if the user gave us the command in newer form, we do not even have to bother checking if the server is given as an absolute pathname.
Yes I think you're right, I'll make this change.
Quoted 8 lines
You seem to have replaced every smtp-server="$(pwd)/ mechanically with sendmai-cmd=\"$(pwd)/, but please make sure that we have at least one test left that passes an absolute path to --smtp-server to ensure that the old mechanism keeps working. A bonus point for marking such a test that needs to be adjusted when the actual deprecation happens (i.e. we'd likely to detect the use of absolute path and throw a warning, so the test should notice the warning message).
Noted, I'll add a test for this case.
Quoted 3 lines
Also you would want to tweak some of the --sendmail-cmd variants to use just the command name, with and without args, to ensure that (1) discovery on $PATH works, and (2) passing initial args works.
I did add two such tests:
t/t9001-send-email.sh +23 -0
@@ -2148,6 +2148,29 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' test_cmp expected-list actual-list ' +test_expect_success $PREREQ 'test using relative path with sendmailCommand' ' + clean_fake_sendmail && + PATH="$(pwd):$PATH" \ + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="fake.sendmail" \ + HEAD~2 && + test_path_is_file commandline1 && + test_path_is_file commandline2 +' + +test_expect_success $PREREQ 'test using shell with sendmailCommand' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="[ 1 -eq 1 ] && \"$(pwd)/fake.sendmail\"" \ + HEAD~2 && + test_path_is_file commandline1 && + test_path_is_file commandline2 +' + test_expect_success $PREREQ 'invoke hook' ' mkdir -p .git/hooks &&
Granted, the second test tests for some generic shell expression, not passing arguments, but I think if the former works the latter ought to as well.
Thanks for your feedback.
Quoted 5 lines · Felipe Contreras
Gregory Anders wrote:
I'm not against these kinds of changes but it took me one minute to figure out all you did was change the format.This belongs in a separate patch.
Yes this was pointed out by the other reviewers as well, I'll omit it in subsequent revisions.
Quoted 13 lines
Quoted 2 lines
+--sendmail-cmd=<command>::
Oh no no no. Don't do shortcuts.
If you think --sendmail-command is too long, then address that problem head on, don't try to hide it.
I do think it's too long, which is why I suggested --command (especially since it's obvious which command we are talking about), but I wouldn't suggest --sdm-command, or something of that sort. We have to own our decisions.
Quoted 7 lines
1. --command
2. --sendmail
3. --sendmail-commandWe have to pick one. I suggest #1.
To try to make #3 shorter is just shoving the problem under the rug.
The intention behind --sendmail-cmd was consistency with --to-cmd and --cc-cmd. Though both of those options also use the condensed 'cmd' form in their configuration options as well, so I suppose I should also change this one to 'sendemail.sendmailcmd'.
I'm not opposed to just '--sendmail' and 'sendemail.sendmail' either. I personally believe --sendmail-cmd is the clearest, even if it's verbose, but I'll concede to whatever consensus we arrive at.
Quoted 26 lines
Quoted 26 lines
git-send-email.perl +3
-0@@ -70,6 +70,7 @@ sub usage { Sending: --envelope-sender <str> * Email envelope sender. + --sendmail-cmd <str> * Shell command to run to send email. --smtp-server <str:int> * Outgoing SMTP server to use. The port is optional. Default 'localhost'. --smtp-server-option <str> * Outgoing SMTP server option to use.@@ -252,6 +253,7 @@ sub do_edit { my (@suppress_cc); my ($auto_8bit_encoding); my ($compose_encoding); +my ($sendmail_command); # Variables with corresponding config settings & hardcoded defaults my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() my $thread = 1;@@ -299,6 +301,7 @@ sub do_edit { "assume8bitencoding" => \$auto_8bit_encoding, "composeencoding" => \$compose_encoding, "transferencoding" => \$target_xfer_encoding, + "sendmailcommand" => \$sendmail_command, );
Quoted 10 lines
git-send-email.perl sub read_config { · line 438
Quoted 6 lines
git-send-email.perl +1
-0my %config_path_settings = (@@ -432,6 +435,7 @@ sub read_config { "no-bcc" => \$no_bcc, "chain-reply-to!" => \$chain_reply_to, "no-chain-reply-to" => sub {$chain_reply_to = 0}, + "sendmail-cmd=s" => \$sendmail_command,Isn't it interesting that to make the code readable you picked $sendmail_command, but you don't want users to type so much, even if it's more readable?
See my note above.
Quoted 13 lines
t/t9001-send-email.sh test_no_confirm () { · line 60
Quoted 9 lines
t/t9001-send-email.sh +1
-1@@ -57,7 +57,7 @@ test_no_confirm () { git send-email \ --from="Example <[email protected]>" \ [email protected] \ - --smtp-server="$(pwd)/fake.sendmail" \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\"" \People are already using --smpt-server=$cmd, we need to keep testing that.
Quoted 6 lines
Yes, eventually we would want them to move to --sendmail-cmd (or --command, or whatever), but that won't happen tomorrow. Therefore our primary tests need to be focused on --smtp-server.
We need new *additional* tests for --sendmail-cmd, but those should not override the current tests. At least not right now.
I will add a test case for the absolute path form of --smtp-server; however, if we are introducing an option for specifying a sendmail-like command, surely that is the one to use when using "fake.sendmail", no?
If we leave the test cases as-is for now, we introduce a split that someone will eventually need to come back and update anyway. Instead of kicking that can down the road, I thought it best to go ahead and do it now.
Thanks for your feedback.
Greg
Quoted 21 lines · Ævar Arnfjörð Bjarmason
Quoted 18 lines · Gregory Anders
Documentation/git-send-email.txt +3
-7@@ -159,13 +159,23 @@ Sending ~~~~~~~ --envelope-sender=<address>:: - Specify the envelope sender used to send the emails. - This is useful if your default address is not the address that is - subscribed to a list. In order to use the 'From' address, set the - value to "auto". If you use the sendmail binary, you must have - suitable privileges for the -f parameter. Default is the value of the - `sendemail.envelopeSender` configuration variable; if that is - unspecified, choosing the envelope sender is left to your MTA. + Specify the envelope sender used to send the emails. This is + useful if your default address is not the address that is + subscribed to a list. In order to use the 'From' address, set
Quoted 8 lines
Documentation/git-send-email.txt Sending · line 169
Quoted 5 lines
Documentation/git-send-email.txt +5
-0+ the value to "auto". If you use the sendmail binary, you must + have suitable privileges for the -f parameter. Default is the + value of the `sendemail.envelopeSender` configuration variable; + if that is unspecified, choosing the envelope sender is left to + your MTA.Please don't include word-wrapping for unrelated changes in the main patch.
My mistake, this has been pointed out to me multiple times now. I'll remove it in the next revision and I'll be sure to avoid this in the future.
Quoted 14 lines
Documentation/git-send-email.txt Sending · line 174
Quoted 7 lines
Documentation/git-send-email.txt +4
-1- $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* + + if (!defined $sendmail_command) { + $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* + } }This "let's not accept a 0" change seems unrelated & should be split into a prep cleanup / refactoring patch. On the one hand it's sensible, on the other nobody cares about having a command named "0" in their path (or a hostname), so I think it's fine to have the ||= Perl idiom leak out here.
Quoted 7 lines
But also, this just seems like confusing logic. Per your docs "your sendmailCommand has precedence over smtpServer.".
Why not make this "if not $sendmail_command" part of the top-level check here (the if this one is nested under), which is only done if $smtp_sever is not defined, if $sendmail_command is defined we don't care about $smtp_server later on, no?
I mostly left this the way it is to minimize the diff, as this is the style the code was already written in. I agree that explicitly checking whether sendmail_command is undefined is probably clearer to the reader.
Quoted 18 lines
Quoted 18 lines
Documentation/git-send-email.txt +5
-5if ($compose && $compose > 0) {@@ -1490,14 +1497,15 @@ sub send_message { unshift (@sendmail_parameters, @smtp_server_options); + if (file_name_is_absolute($smtp_server)) { + # Preserved for backward compatibility + $sendmail_command ||= $smtp_server; + } + if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { - my $pid = open my $sm, '|-'; - defined $pid or die $!; - if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!;
Quoted 9 lines
Documentation/git-send-email.txt sub send_message { · line 1508
Quoted 3 lines
Documentation/git-send-email.txt +2
-1- } + } elsif (defined $sendmail_command) { + open my $sm, '|-', "$sendmail_command @sendmail_parameters";Can we really not avoid moving from exec-as-list so Perl quotes everything, to doing our own interpolation here? It looks like the tests don't check arguments with whitespace (which should fail with this change).
Shell interpolation in this case is considered a feature, not a bug, i.e. we want to provide users the ability to use arbitrary shell expressions (as they do in e.g. aliases) or pass arguments. I also modeled this after the implementation for --to-cmd and --cc-cmd, which also run their respective commands in the shell just like this.
Also, this *did* cause the tests to fail since the tests write output to a path with a space in it. You'll notice that in the diff for the tests I had to wrap '$(pwd)/fake.sendmail' in additional quotes to resolve this.
Quoted 13 lines
Quoted 13 lines
Documentation/git-send-email.txt +3
-1@@ -1592,14 +1600,14 @@ sub send_message { printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); } else { print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); - if (!file_name_is_absolute($smtp_server)) { + if (defined $sendmail_command) { + print "Sendmail: $sendmail_command ".join(' ',@sendmail_parameters)."\n"; + } else { print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; foreach my $entry (@recipients) { print "RCPT TO:<$entry>\n"; }
Quoted 8 lines
Documentation/git-send-email.txt sub send_message { · line 1613
Quoted 5 lines
Documentation/git-send-email.txt +0
-2- } else { - print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; } print $header, "\n"; if ($smtp) {Minor nit: Let's just continue to use "if (!" here to keep the diff minimal or split up such refactoring into another change...
Sure, I can do that.
Thanks for your feedback.
Quoted 16 lines · Gregory Anders
Quoted 16 lines
Quoted 1 line
+--sendmail-cmd=<command>::
Oh no no no. Don't do shortcuts.
If you think --sendmail-command is too long, then address that problem head on, don't try to hide it.
I do think it's too long, which is why I suggested --command (especially since it's obvious which command we are talking about), but I wouldn't suggest --sdm-command, or something of that sort. We have to own our decisions.
1. --command
2. --sendmail
3. --sendmail-command
Quoted 8 lines
Quoted 3 lines
We have to pick one. I suggest #1.
To try to make #3 shorter is just shoving the problem under the rug.
The intention behind
--sendmail-cmdwas consistency with--to-cmdand--cc-cmd. Though both of those options also use the condensed 'cmd' form in their configuration options as well, so I suppose I should also change this one to 'sendemail.sendmailcmd'.
I see. In that case that might make sense. I still prefer #1.
Quoted 19 lines
Quoted 12 lines
t/t9001-send-email.sh test_no_confirm () { · line 60
Quoted 8 lines
t/t9001-send-email.sh +1
-1@@ -57,7 +57,7 @@ test_no_confirm () { git send-email \ --from="Example <[email protected]>" \ [email protected] \ - --smtp-server="$(pwd)/fake.sendmail" \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\"" \People are already using --smpt-server=$cmd, we need to keep testing that.
Quoted 6 lines
Yes, eventually we would want them to move to --sendmail-cmd (or --command, or whatever), but that won't happen tomorrow. Therefore our primary tests need to be focused on --smtp-server.
We need new *additional* tests for --sendmail-cmd, but those should not override the current tests. At least not right now.
Quoted 8 lines
I will add a test case for the absolute path form of --smtp-server; however, if we are introducing an option for specifying a sendmail-like command, surely that is the one to use when using "fake.sendmail", no?
If we leave the test cases as-is for now, we introduce a split that someone will eventually need to come back and update anyway. Instead of kicking that can down the road, I thought it best to go ahead and do it now.
The sole purpose of software is that it's useful to users. Software that works today but not tomorrow is bad software.
The primary purpose of the testing framework is to ensure that doesn't happen; that git keeps working in the same way today than it did yesterday.
That's why it's more important that tests excercise the options people were using yesterday.
In addition we also want to be testing new functionality, but that's *in addition*.
Maybe at some point in the future more people will be using --sendmail-cmd, but right now that's not the case. Right now we need to be testing the option people are using *today*.
Cheers.
Signature · 1 line
Felipe Contreras
Quoted 9 lines · Felipe Contreras
The sole purpose of software is that it's useful to users. Software that works today but not tomorrow is bad software.
The primary purpose of the testing framework is to ensure that doesn't happen; that git keeps working in the same way today than it did yesterday.
That's why it's more important that tests excercise the options people were using yesterday.
Quoted 6 lines
In addition we also want to be testing new functionality, but that's *in addition*.
Maybe at some point in the future more people will be using --sendmail-cmd, but right now that's not the case. Right now we need to be testing the option people are using *today*.
I agree with this completely. Is this requirement satisfied with the addition of a test that checks the usage of an absolute path with --smtp-server (the behavior that people were using yesterday), while all of the existing tests are still converted to the new option? I have such a test written (along with a few others) in the forthcoming v2 patch:
t/t9001-send-email.sh +49 -0
@@ -2148,6 +2148,55 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' test_cmp expected-list actual-list ' +test_expect_success $PREREQ 'test using absolute path for --smtp-server' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --smtp-server="$(pwd)/fake.sendmail" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'arguments not supported with --smtp-server' ' + test_expect_code 127 git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --smtp-server="$(pwd)/fake.sendmail -f [email protected]" \ + HEAD^ +' + +test_expect_success $PREREQ 'test using command name with --sendmail-cmd' ' + clean_fake_sendmail && + PATH="$(pwd):$PATH" \ + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="fake.sendmail" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\" -f [email protected]" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="[ 1 -eq 1 ] && \"$(pwd)/fake.sendmail\"" \ + HEAD^ && + test_path_is_file commandline1 +' + test_expect_success $PREREQ 'invoke hook' ' mkdir -p .git/hooks &&
Quoted 18 lines · Gregory Anders
Quoted 10 lines · Felipe Contreras
The sole purpose of software is that it's useful to users. Software that works today but not tomorrow is bad software.
The primary purpose of the testing framework is to ensure that doesn't happen; that git keeps working in the same way today than it did yesterday.
That's why it's more important that tests excercise the options people were using yesterday.
Quoted 6 lines
In addition we also want to be testing new functionality, but that's *in addition*.
Maybe at some point in the future more people will be using --sendmail-cmd, but right now that's not the case. Right now we need to be testing the option people are using *today*.
Quoted 5 lines
I agree with this completely. Is this requirement satisfied with the addition of a test that checks the usage of an absolute path with --smtp-server (the behavior that people were using yesterday), while all of the existing tests are still converted to the new option? I have such a test written (along with a few others) in the forthcoming v2 patch:
I still insist the current tests should not be converted. But ultimately it's Junio's call.
Cheers.
Signature · 1 line
Felipe Contreras
PATCH v2 1/1 git-send-email: add option to specify sendmail command
In reply to Gregory AndersThe sendemail.smtpServer configuration option and --smtp-server command line option both support using a sendmail-like program to send emails by specifying an absolute file path. However, this is not ideal for the following reasons:
1. It overloads the meaning of smtpServer (now a program is being used for the server?)
2. It doesn't allow for non-absolute paths, arguments, or arbitrary scripting
Requiring an absolute path is bad for portability, as the same program may be in different locations on different systems. If a user wishes to pass arguments to their program, they have to use the smtpServerOption option, which is cumbersome (as it must be repeated for each option) and doesn't adhere to normal git conventions.
Introduce a new configuration option sendemail.sendmailCmd as well as a command line option --sendmail-cmd that can be used to specify a command (with or without arguments) or shell expression to run to send email. The name of this option is consistent with --to-cmd and --cc-cmd. This invocation honors the user's $PATH so that absolute paths are not necessary. Arbitrary shell expressions are also supported, allowing users to do basic scripting.
Give this option a higher precedence over --smtp-server and sendemail.smtpServer, as the new interface is more flexible. For backward compatibility, continue to support absolute paths in --smtp-server and sendemail.smtpServer.
--- Documentation/git-send-email.txt | 26 +++++++++++++++++++------- git-send-email.perl | 29 ++++++++++++++++++++++------- t/t9001-send-email.sh | 31 +++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 14 deletions(-)
Documentation/git-send-email.txt +19 -7
@@ -167,6 +167,15 @@ Sending `sendemail.envelopeSender` configuration variable; if that is unspecified, choosing the envelope sender is left to your MTA. +--sendmail-cmd=<command>:: + Specify a command to run to send the email. The command should + be compatible with `sendmail` (specifically, it should support + the `-i` option). The command will be executed in the shell if + necessary. Default is the value of `sendemail.sendmailcmd`. If + unspecified, and if --smtp-server is also unspecified, + git-send-email will search for `sendmail` in `/usr/sbin`, + `/usr/lib` and $PATH. + --smtp-encryption=<encryption>:: Specify the encryption to use, either 'ssl' or 'tls'. Any other value reverts to plain SMTP. Default is the value of
@@ -211,13 +220,16 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in - `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise. + `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. + + For backward compatibility, this option can also specify a full + pathname of a sendmail-like program instead; the program must + support the `-i` option. This method does not support passing + arguments or using plain command names. For those use cases, + consider using `--sendmail-cmd` instead. --smtp-server-port=<port>:: Specifies a port different from the default port (SMTP
git-send-email.perl +22 -7
@@ -70,6 +70,7 @@ sub usage { Sending: --envelope-sender <str> * Email envelope sender. + --sendmail-cmd <str> * Command to run to send email. --smtp-server <str:int> * Outgoing SMTP server to use. The port is optional. Default 'localhost'. --smtp-server-option <str> * Outgoing SMTP server option to use.
@@ -252,6 +253,7 @@ sub do_edit { my (@suppress_cc); my ($auto_8bit_encoding); my ($compose_encoding); +my ($sendmail_cmd); # Variables with corresponding config settings & hardcoded defaults my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() my $thread = 1;
@@ -299,6 +301,7 @@ sub do_edit { "assume8bitencoding" => \$auto_8bit_encoding, "composeencoding" => \$compose_encoding, "transferencoding" => \$target_xfer_encoding, + "sendmailcmd" => \$sendmail_cmd, ); my %config_path_settings = (
@@ -432,6 +435,7 @@ sub read_config { "no-bcc" => \$no_bcc, "chain-reply-to!" => \$chain_reply_to, "no-chain-reply-to" => sub {$chain_reply_to = 0}, + "sendmail-cmd=s" => \$sendmail_cmd, "smtp-server=s" => \$smtp_server, "smtp-server-option=s" => \@smtp_server_options, "smtp-server-port=s" => \$smtp_server_port,
@@ -1003,16 +1007,19 @@ sub expand_one_alias { $reply_to = sanitize_address($reply_to); } -if (!defined $smtp_server) { +if (!defined $sendmail_cmd && !defined $smtp_server) { my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail ); push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH}; foreach (@sendmail_paths) { if (-x $_) { - $smtp_server = $_; + $sendmail_cmd = $_; last; } } - $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* + + if (!defined $sendmail_cmd) { + $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* + } } if ($compose && $compose > 0) {
@@ -1492,11 +1499,15 @@ sub send_message { if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { + } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) { my $pid = open my $sm, '|-'; defined $pid or die $!; if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!; + if (defined $sendmail_cmd) { + exec "$sendmail_cmd @sendmail_parameters" or die $!; + } else { + exec ($smtp_server, @sendmail_parameters) or die $!; + } } print $sm "$header\n$message"; close $sm or die $!;
@@ -1592,14 +1603,18 @@ sub send_message { printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); } else { print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); - if (!file_name_is_absolute($smtp_server)) { + if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) { print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; foreach my $entry (@recipients) { print "RCPT TO:<$entry>\n"; } } else { - print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; + if (!defined $sendmail_cmd) { + $sendmail_cmd = $smtp_server; + } + + print "Sendmail: $sendmail_cmd ".join(' ',@sendmail_parameters)."\n"; } print $header, "\n"; if ($smtp) {
t/t9001-send-email.sh +31 -0
@@ -2148,6 +2148,37 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' test_cmp expected-list actual-list ' +test_expect_success $PREREQ 'test using command name with --sendmail-cmd' ' + clean_fake_sendmail && + PATH="$(pwd):$PATH" \ + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="fake.sendmail" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\" -f [email protected]" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="f() { \"$(pwd)/fake.sendmail\" \"\$@\"; };f" \ + HEAD^ && + test_path_is_file commandline1 +' + test_expect_success $PREREQ 'invoke hook' ' mkdir -p .git/hooks &&
Signature · 1 line
2.31.1
Re: [PATCH v2] git-send-email: add option to specify sendmail command
Quoted 24 lines · Gregory Anders
The sendemail.smtpServer configuration option and --smtp-server command line option both support using a sendmail-like program to send emails by specifying an absolute file path. However, this is not ideal for the following reasons:
1. It overloads the meaning of smtpServer (now a program is being used for the server?)
2. It doesn't allow for non-absolute paths, arguments, or arbitrary scriptingRequiring an absolute path is bad for portability, as the same program may be in different locations on different systems. If a user wishes to pass arguments to their program, they have to use the smtpServerOption option, which is cumbersome (as it must be repeated for each option) and doesn't adhere to normal git conventions.
Introduce a new configuration option sendemail.sendmailCmd as well as a command line option --sendmail-cmd that can be used to specify a command (with or without arguments) or shell expression to run to send email. The name of this option is consistent with --to-cmd and --cc-cmd. This invocation honors the user's $PATH so that absolute paths are not necessary. Arbitrary shell expressions are also supported, allowing users to do basic scripting.
Quoted 7 lines
Give this option a higher precedence over --smtp-server and sendemail.smtpServer, as the new interface is more flexible. For backward compatibility, continue to support absolute paths in --smtp-server and sendemail.smtpServer.
Signed-off-by Gregory Anders <[email protected]>---
Quite well explained.
Quoted 6 lines
Documentation/git-send-email.txt +0
-0@@ -167,6 +167,15 @@ Sending `sendemail.envelopeSender` configuration variable; if that is
Documentation/git-send-email.txt Sending · line 177
Quoted 10 lines
Documentation/git-send-email.txt +8
-0unspecified, choosing the envelope sender is left to your MTA. +--sendmail-cmd=<command>:: + Specify a command to run to send the email. The command should + be compatible with `sendmail` (specifically, it should support + the `-i` option). The command will be executed in the shell if + necessary. Default is the value of `sendemail.sendmailcmd`. If + unspecified, and if --smtp-server is also unspecified, + git-send-email will search for `sendmail` in `/usr/sbin`, + `/usr/lib` and $PATH.
OK.
Quoted 14 lines
Documentation/git-send-email.txt +0
-7--smtp-encryption=<encryption>:: Specify the encryption to use, either 'ssl' or 'tls'. Any other value reverts to plain SMTP. Default is the value of@@ -211,13 +220,16 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in - `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise.
Documentation/git-send-email.txt a password is obtained using 'git-credential'. · line 232
Quoted 10 lines
Documentation/git-send-email.txt +10
-0+ `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. + + For backward compatibility, this option can also specify a full + pathname of a sendmail-like program instead; the program must + support the `-i` option. This method does not support passing + arguments or using plain command names. For those use cases, + consider using `--sendmail-cmd` instead.
Two comments here:
- The paragraph would probably not render well, unless you replace the blank "paragraph break" line before it with a line that consists of a sole '+', and dedent the paragraph body.
- The way the "-i" option is mentioned is different from the one we saw earlier for --sendmail-cmd and might risk puzzling the users if the requirement is subtly different.
Quoted 7 lines
git-send-email.perl +0
-0@@ -1492,11 +1499,15 @@ sub send_message {
git-send-email.perl sub send_message { · line 1507
Quoted 10 lines
git-send-email.perl +3
-2if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { + } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) { my $pid = open my $sm, '|-'; defined $pid or die $!; if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!; + if (defined $sendmail_cmd) { + exec "$sendmail_cmd @sendmail_parameters" or die $!;
This looks problematic, as @sendmail_parameters is computed like this:
my @sendmail_parameters = ('-i', @recipients);
...
$raw_from = extract_valid_address($raw_from);
unshift (@sendmail_parameters,
'-f', $raw_from) if(defined $envelope_sender);
...
unshift (@sendmail_parameters, @smtp_server_options);Notice that nothing quotes its elements for the shell, and it is natural if we think about the original use of this array---it is to be fed to the array form of exec($cmd, @args).
@recipients, and $raw_from come from extract_valid_address(), which gives 'add@ress' for "Human readable name <add@ress>", and it may be rare (but possible) to have a problematic characer in them. But the elements of @smtp_server_options can be anything, and because the values the end users already have in their configuration files are designed to be used in the original "exec ($smtp_server, @sendmail_parameters)" codepath, they would not be quoted for the shell, and they should not be treated differently in the new codepath.
In short, it is far from sufficient to just "$concatenate @variables" to form a single string. $sendmail_cmd should be left as-is (after all, we do want the shell to split it at $IFS whitespace into tokens), but each element of @sendmail_parameters should be protected from the shell (both word splitting and $interpolation rules). Perhaps something along the lines of this instead?
exec ("sh", "-c", "$sendmail_cmd \"\$\@\"", "-", @sendmail_parameters);git-send-email.perl sub send_message { · line 1510
Quoted 3 lines
git-send-email.perl +3
-0+ } else { + exec ($smtp_server, @sendmail_parameters) or die $!; + }
Other than that, looking good.
Thanks.
Re: [PATCH v2] git-send-email: add option to specify sendmail command
Quoted 8 lines · Junio C Hamano
In short, it is far from sufficient to just "$concatenate @variables" to form a single string. $sendmail_cmd should be left as-is (after all, we do want the shell to split it at $IFS whitespace into tokens), but each element of @sendmail_parameters should be protected from the shell (both word splitting and $interpolation rules). Perhaps something along the lines of this instead?
exec ("sh", "-c", "$sendmail_cmd \"\$\@\"", "-", @sendmail_parameters);
Does this pose a problem for platforms such as Windows that don't have a 'sh' (not sure if there are any others)? Is git-send-email meant to support Windows?
PATCH v3 1/1 git-send-email: add option to specify sendmail command
In reply to Gregory AndersThe sendemail.smtpServer configuration option and --smtp-server command line option both support using a sendmail-like program to send emails by specifying an absolute file path. However, this is not ideal for the following reasons:
1. It overloads the meaning of smtpServer (now a program is being used for the server?)
2. It doesn't allow for non-absolute paths, arguments, or arbitrary scripting
Requiring an absolute path is bad for portability, as the same program may be in different locations on different systems. If a user wishes to pass arguments to their program, they have to use the smtpServerOption option, which is cumbersome (as it must be repeated for each option) and doesn't adhere to normal git conventions.
Introduce a new configuration option sendemail.sendmailCmd as well as a command line option --sendmail-cmd that can be used to specify a command (with or without arguments) or shell expression to run to send email. The name of this option is consistent with --to-cmd and --cc-cmd. This invocation honors the user's $PATH so that absolute paths are not necessary. Arbitrary shell expressions are also supported, allowing users to do basic scripting.
Give this option a higher precedence over --smtp-server and sendemail.smtpServer, as the new interface is more flexible. For backward compatibility, continue to support absolute paths in --smtp-server and sendemail.smtpServer.
---
Fix and reword the documentation to hopefully be more consistent between --sendmail-cmd and --smtp-server.
Update the invocation of "sendmail_cmd" to ensure that parameters are not expanded by the shell (the parameters may contain any number of special characters that are not intended to be interpreted by the shell).
Use a block scoped variable to print the sendmail invocation at the end of the 'send_message' subroutine. Assigning directly to $sendmail_cmd (as in the v2 patch) causes some bizarre problems; namely, it seems to affect the value of $sendmail_cmd that is read at earlier points in the same subroutine, which causes test invocations of the form
git send-email --smtp-server="$(pwd)/fake.sendmail"
to fail. The value passed to --smtp-server was assigned to $sendmail_cmd at the end of the 'send_message' subprocedure, but somehow this caused the 'if (defined $sendmail_cmd)' condition earlier in the subproc to evaluate to true. This would fail because $sendmail_cmd is expanded in the shell, and since $(pwd) contains a space the command was being split, resulting in a "command not found" error. I don't have enough Perl experience to explain what's happening in this case, but using a scoped variable resolves the issue.
Documentation/git-send-email.txt | 25 ++++++++++++++++------- git-send-email.perl | 34 +++++++++++++++++++++++++------- t/t9001-send-email.sh | 31 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 14 deletions(-)
Documentation/git-send-email.txt +18 -7
@@ -167,6 +167,14 @@ Sending `sendemail.envelopeSender` configuration variable; if that is unspecified, choosing the envelope sender is left to your MTA. +--sendmail-cmd=<command>:: + Specify a command to run to send the email. The command should + be sendmail-like; specifically, it must support the `-i` option. + The command will be executed in the shell if necessary. Default + is the value of `sendemail.sendmailcmd`. If unspecified, and if + --smtp-server is also unspecified, git-send-email will search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH. + --smtp-encryption=<encryption>:: Specify the encryption to use, either 'ssl' or 'tls'. Any other value reverts to plain SMTP. Default is the value of
@@ -211,13 +219,16 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in - `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise. + `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. ++ +For backward compatibility, this option can also specify a full pathname +of a sendmail-like program instead; the program must support the `-i` +option. This method does not support passing arguments or using plain +command names. For those use cases, consider using `--sendmail-cmd` +instead. --smtp-server-port=<port>:: Specifies a port different from the default port (SMTP
git-send-email.perl +27 -7
@@ -70,6 +70,7 @@ sub usage { Sending: --envelope-sender <str> * Email envelope sender. + --sendmail-cmd <str> * Command to run to send email. --smtp-server <str:int> * Outgoing SMTP server to use. The port is optional. Default 'localhost'. --smtp-server-option <str> * Outgoing SMTP server option to use.
@@ -252,6 +253,7 @@ sub do_edit { my (@suppress_cc); my ($auto_8bit_encoding); my ($compose_encoding); +my ($sendmail_cmd); # Variables with corresponding config settings & hardcoded defaults my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() my $thread = 1;
@@ -299,6 +301,7 @@ sub do_edit { "assume8bitencoding" => \$auto_8bit_encoding, "composeencoding" => \$compose_encoding, "transferencoding" => \$target_xfer_encoding, + "sendmailcmd" => \$sendmail_cmd, ); my %config_path_settings = (
@@ -432,6 +435,7 @@ sub read_config { "no-bcc" => \$no_bcc, "chain-reply-to!" => \$chain_reply_to, "no-chain-reply-to" => sub {$chain_reply_to = 0}, + "sendmail-cmd=s" => \$sendmail_cmd, "smtp-server=s" => \$smtp_server, "smtp-server-option=s" => \@smtp_server_options, "smtp-server-port=s" => \$smtp_server_port,
@@ -1003,16 +1007,19 @@ sub expand_one_alias { $reply_to = sanitize_address($reply_to); } -if (!defined $smtp_server) { +if (!defined $sendmail_cmd && !defined $smtp_server) { my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail ); push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH}; foreach (@sendmail_paths) { if (-x $_) { - $smtp_server = $_; + $sendmail_cmd = $_; last; } } - $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* + + if (!defined $sendmail_cmd) { + $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* + } } if ($compose && $compose > 0) {
@@ -1492,11 +1499,17 @@ sub send_message { if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { + } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) { my $pid = open my $sm, '|-'; defined $pid or die $!; if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!; + if (defined $sendmail_cmd) { + exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters) + or die $!; + } else { + exec ($smtp_server, @sendmail_parameters) + or die $!; + } } print $sm "$header\n$message"; close $sm or die $!;
@@ -1592,14 +1605,21 @@ sub send_message { printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); } else { print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); - if (!file_name_is_absolute($smtp_server)) { + if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) { print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; foreach my $entry (@recipients) { print "RCPT TO:<$entry>\n"; } } else { - print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; + my $sm; + if (defined $sendmail_cmd) { + $sm = $sendmail_cmd; + } else { + $sm = $smtp_server; + } + + print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n"; } print $header, "\n"; if ($smtp) {
t/t9001-send-email.sh +31 -0
@@ -2148,6 +2148,37 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' test_cmp expected-list actual-list ' +test_expect_success $PREREQ 'test using command name with --sendmail-cmd' ' + clean_fake_sendmail && + PATH="$(pwd):$PATH" \ + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="fake.sendmail" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\" -f [email protected]" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="f() { \"$(pwd)/fake.sendmail\" \"\$@\"; };f" \ + HEAD^ && + test_path_is_file commandline1 +' + test_expect_success $PREREQ 'invoke hook' ' mkdir -p .git/hooks &&
Signature · 1 line
2.31.1
Re: [PATCH v2] git-send-email: add option to specify sendmail command
In reply to Gregory AndersQuoted 8 lines · Gregory Anders
Quoted 7 lines · Junio C Hamano
In short, it is far from sufficient to just "$concatenate @variables" to form a single string. $sendmail_cmd should be left as-is (after all, we do want the shell to split it at $IFS whitespace into tokens), but each element of @sendmail_parameters should be protected from the shell (both word splitting and $interpolation rules). Perhaps something along the lines of this instead?
Quoted 5 lines
Quoted 1 line
exec ("sh", "-c", "$sendmail_cmd \"\$\@\"", "-", @sendmail_parameters);Does this pose a problem for platforms such as Windows that don't have a 'sh' (not sure if there are any others)? Is git-send-email meant to support Windows?
Seeing what run-command.c::prepare_shell_cmd() does under GIT_WINDOWS_NATIVE (or on other platforms), I doubt the construct would be a problem. Our Windows experts would certainly chime in if it is.
Thanks.
Re: [PATCH v3] git-send-email: add option to specify sendmail command
In reply to Gregory AndersQuoted 6 lines · Gregory Anders
Use a block scoped variable to print the sendmail invocation at the end of the '
send_message' subroutine. Assigning directly to $sendmail_cmd (as in the v2 patch) causes some bizarre problems; namely, it seems to affect the value of $sendmail_cmd that is read at earlier points in the same subroutine, which causes test invocations of the form
Quoted 6 lines
git send-email --smtp-server="$(pwd)/fake.sendmail"to fail. The value passed to --smtp-server was assigned to $sendmail_cmd at the end of the '
send_message' subprocedure, but somehow this caused the 'if (defined $sendmail_cmd)' condition earlier in the subproc to evaluate to true.
Are you talking about the use of $sm that is local to the debug output? I think leaving $sendmail_cmd intact by using a separate variable is the right choice. Isn't the problem you observed a consequence of send_message() getting called once for each message, so assigning to $sendmail_cmd in the function for the first invocation of the function would change its value for the second invocation?
Also, if we have been using
--smtp-server=$(pwd)/fake.sendmail
we cannot expect to use the same value like this:
--sendmail-cmd=$(pwd)/fake.sendmail
because we deliberately add a space in the $(pwd) by choosing the name of the test directory to be "trash directory.something". We'd need to do something like
--sendmail-cmd='$(pwd)/fake.sendmail'
so that the shell sees '$(pwd)/fake.sendmail' literally and runs pwd to find out what the path to the program is, I would think.
Quoted 8 lines
t/t9001-send-email.sh +0
-0@@ -2148,6 +2148,37 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' test_cmp expected-list actual-list '
t/t9001-send-email.sh test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' · line 2160
Quoted 10 lines
t/t9001-send-email.sh +10
-0+test_expect_success $PREREQ 'test using command name with --sendmail-cmd' ' + clean_fake_sendmail && + PATH="$(pwd):$PATH" \ + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="fake.sendmail" \ + HEAD^ && + test_path_is_file commandline1 +'
Nice demonstration of the "we no longer need an absolute pathname" feature.
t/t9001-send-email.sh test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' · line 2170
Quoted 9 lines
t/t9001-send-email.sh +9
-0+test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\" -f [email protected]" \ + HEAD^ && + test_path_is_file commandline1 +'
Hmph, if $(pwd) has a double quote character in it, this may not work as expected, as the shell that is expanding the command line arguments for "git send-email" would see $(pwd), expand it and our program will see
"/path/with/d"quote/git/t/trash directory.9001/fake.sendmail" -f [email protected]
as the value of --sendmail-cmd, which would not interpolate well, no?
We want the shell that eats the command line of 'git send-email' to see
--sendmail-cmd='$(pwd)/fake.sendmail'\" -f [email protected]"
and because this is inside a sq pair, it would become
--sendmail-cmd='\''$(pwd)/fake.sendmail'\''\" -f [email protected]"
after we replace each sq with '\'', or something like that, perhaps?
t/t9001-send-email.sh test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' · line 2180
Quoted 9 lines
t/t9001-send-email.sh +9
-0+test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="f() { \"$(pwd)/fake.sendmail\" \"\$@\"; };f" \ + HEAD^ && + test_path_is_file commandline1 +'
Nice demonstration of how a bit of scripting can be used.
t/t9001-send-email.sh test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' · line 2183
Quoted 2 lines
t/t9001-send-email.sh +0
-0test_expect_success $PREREQ 'invoke hook' ' mkdir -p .git/hooks &&
Thanks.
Re: [PATCH v3] git-send-email: add option to specify sendmail command
Quoted 3 lines · Junio C Hamano
We want the shell that eats the command line of 'git send-email' to see
--sendmail-cmd='$(pwd)/fake.sendmail'\" -f [email protected]"
Eh, sorry, but this is wrong. It would have to be something like
--sendmail-cmd='"$(pwd)/fake.sendmail" -f [email protected]'
The point is that the outer shell (i.e. the one that is eval'ing the body of the test_expect_success) should just see and treat the path to the sendmail-like program as "$(pwd)/fake.sendmail" as a literal string including the surrounding double quotes and pass it down to "git send-email", and the shell started by our "exec('sh','-c',...)" thing will see what $(pwd) expands to, appends /fake.sendmail to it, and treat the whole thing as a single "path to the program", that is followed by two args, i.e. '-f' and '[email protected]'.
And inside test_expect_success whose body is surrounded by a pair of sq, we'd express a sq as '\'', so it becomes
--sendmail-cmd='\''"$(pwd)/fake.sendmail" -f [email protected]'\''
in the test script, I would think.
Re: [PATCH v3] git-send-email: add option to specify sendmail command
In reply to Junio C HamanoQuoted 7 lines · Junio C Hamano
Are you talking about the use of $sm that is local to the debug output? I think leaving $sendmail_cmd intact by using a separate variable is the right choice. Isn't the problem you observed a consequence of
send_message()getting called once for each message, so assigning to $sendmail_cmd in the function for the first invocation of the function would change its value for the second invocation?
Yes that is right. That makes sense. I didn't realize the subprocess was called twice, though that is such an obvious explanation I don't know why I didn't think of it.
Quoted 8 lines
Also, if we have been using
--smtp-server=$(pwd)/fake.sendmailwe cannot expect to use the same value like this:
--sendmail-cmd=$(pwd)/fake.sendmail
Quoted 8 lines
because we deliberately add a space in the $(pwd) by choosing the name of the test directory to be "trash directory.something". We'd need to do something like
--sendmail-cmd='$(pwd)/fake.sendmail'so that the shell sees '$(pwd)/fake.sendmail' literally and runs pwd to find out what the path to the program is, I would think.
Indeed, in prior versions of this patch I had replaced the uses of --smtp-server in the test suite with --sendmail-cmd which included those extra quotes (I reverted back to using --smtp-server after feedback from other reviewers in lieu of simply adding new test cases for --sendmail-cmd).
Quoted 23 lines
t/t9001-send-email.sh test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' · line 2170
Quoted 10 lines
t/t9001-send-email.sh +9
-1+test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="\"$(pwd)/fake.sendmail\" -f [email protected]" \ + HEAD^ && + test_path_is_file commandline1 +'Hmph, if $(pwd) has a double quote character in it, this may not work as expected, as the shell that is expanding the command line arguments for "git send-email" would see $(pwd), expand it and our program will see
"/path/with/d"quote/git/t/trash directory.9001/fake.sendmail" -f [email protected]as the value of --sendmail-cmd, which would not interpolate well, no?
We want the shell that eats the command line of 'git send-email' to see
Quoted 7 lines
--sendmail-cmd='$(pwd)/fake.sendmail'\" -f [email protected]"and because this is inside a sq pair, it would become
--sendmail-cmd='\''$(pwd)/fake.sendmail'\''\" -f [email protected]"after we replace each sq with '\'', or something like that, perhaps?
I'll take a look at this (as well as your followup email) and send a new version.
Thanks,
Greg
PATCH v4 1/1 git-send-email: add option to specify sendmail command
In reply to Gregory AndersThe sendemail.smtpServer configuration option and --smtp-server command line option both support using a sendmail-like program to send emails by specifying an absolute file path. However, this is not ideal for the following reasons:
1. It overloads the meaning of smtpServer (now a program is being used for the server?)
2. It doesn't allow for non-absolute paths, arguments, or arbitrary scripting
Requiring an absolute path is bad for portability, as the same program may be in different locations on different systems. If a user wishes to pass arguments to their program, they have to use the smtpServerOption option, which is cumbersome (as it must be repeated for each option) and doesn't adhere to normal git conventions.
Introduce a new configuration option sendemail.sendmailCmd as well as a command line option --sendmail-cmd that can be used to specify a command (with or without arguments) or shell expression to run to send email. The name of this option is consistent with --to-cmd and --cc-cmd. This invocation honors the user's $PATH so that absolute paths are not necessary. Arbitrary shell expressions are also supported, allowing users to do basic scripting.
Give this option a higher precedence over --smtp-server and sendemail.smtpServer, as the new interface is more flexible. For backward compatibility, continue to support absolute paths in --smtp-server and sendemail.smtpServer.
--- Documentation/git-send-email.txt | 25 ++++++++++++++++------- git-send-email.perl | 34 +++++++++++++++++++++++++------- t/t9001-send-email.sh | 31 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 14 deletions(-)
Documentation/git-send-email.txt +18 -7
@@ -167,6 +167,14 @@ Sending `sendemail.envelopeSender` configuration variable; if that is unspecified, choosing the envelope sender is left to your MTA. +--sendmail-cmd=<command>:: + Specify a command to run to send the email. The command should + be sendmail-like; specifically, it must support the `-i` option. + The command will be executed in the shell if necessary. Default + is the value of `sendemail.sendmailcmd`. If unspecified, and if + --smtp-server is also unspecified, git-send-email will search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH. + --smtp-encryption=<encryption>:: Specify the encryption to use, either 'ssl' or 'tls'. Any other value reverts to plain SMTP. Default is the value of
@@ -211,13 +219,16 @@ a password is obtained using 'git-credential'. --smtp-server=<host>:: If set, specifies the outgoing SMTP server to use (e.g. - `smtp.example.com` or a raw IP address). Alternatively it can - specify a full pathname of a sendmail-like program instead; - the program must support the `-i` option. Default value can - be specified by the `sendemail.smtpServer` configuration - option; the built-in default is to search for `sendmail` in - `/usr/sbin`, `/usr/lib` and $PATH if such program is - available, falling back to `localhost` otherwise. + `smtp.example.com` or a raw IP address). If unspecified, and if + `--sendmail-cmd` is also unspecified, the default is to search + for `sendmail` in `/usr/sbin`, `/usr/lib` and $PATH if such a + program is available, falling back to `localhost` otherwise. ++ +For backward compatibility, this option can also specify a full pathname +of a sendmail-like program instead; the program must support the `-i` +option. This method does not support passing arguments or using plain +command names. For those use cases, consider using `--sendmail-cmd` +instead. --smtp-server-port=<port>:: Specifies a port different from the default port (SMTP
git-send-email.perl +27 -7
@@ -70,6 +70,7 @@ sub usage { Sending: --envelope-sender <str> * Email envelope sender. + --sendmail-cmd <str> * Command to run to send email. --smtp-server <str:int> * Outgoing SMTP server to use. The port is optional. Default 'localhost'. --smtp-server-option <str> * Outgoing SMTP server option to use.
@@ -252,6 +253,7 @@ sub do_edit { my (@suppress_cc); my ($auto_8bit_encoding); my ($compose_encoding); +my ($sendmail_cmd); # Variables with corresponding config settings & hardcoded defaults my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() my $thread = 1;
@@ -299,6 +301,7 @@ sub do_edit { "assume8bitencoding" => \$auto_8bit_encoding, "composeencoding" => \$compose_encoding, "transferencoding" => \$target_xfer_encoding, + "sendmailcmd" => \$sendmail_cmd, ); my %config_path_settings = (
@@ -432,6 +435,7 @@ sub read_config { "no-bcc" => \$no_bcc, "chain-reply-to!" => \$chain_reply_to, "no-chain-reply-to" => sub {$chain_reply_to = 0}, + "sendmail-cmd=s" => \$sendmail_cmd, "smtp-server=s" => \$smtp_server, "smtp-server-option=s" => \@smtp_server_options, "smtp-server-port=s" => \$smtp_server_port,
@@ -1003,16 +1007,19 @@ sub expand_one_alias { $reply_to = sanitize_address($reply_to); } -if (!defined $smtp_server) { +if (!defined $sendmail_cmd && !defined $smtp_server) { my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail ); push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH}; foreach (@sendmail_paths) { if (-x $_) { - $smtp_server = $_; + $sendmail_cmd = $_; last; } } - $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* + + if (!defined $sendmail_cmd) { + $smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug* + } } if ($compose && $compose > 0) {
@@ -1492,11 +1499,17 @@ sub send_message { if ($dry_run) { # We don't want to send the email. - } elsif (file_name_is_absolute($smtp_server)) { + } elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) { my $pid = open my $sm, '|-'; defined $pid or die $!; if (!$pid) { - exec($smtp_server, @sendmail_parameters) or die $!; + if (defined $sendmail_cmd) { + exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters) + or die $!; + } else { + exec ($smtp_server, @sendmail_parameters) + or die $!; + } } print $sm "$header\n$message"; close $sm or die $!;
@@ -1592,14 +1605,21 @@ sub send_message { printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); } else { print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); - if (!file_name_is_absolute($smtp_server)) { + if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) { print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; foreach my $entry (@recipients) { print "RCPT TO:<$entry>\n"; } } else { - print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; + my $sm; + if (defined $sendmail_cmd) { + $sm = $sendmail_cmd; + } else { + $sm = $smtp_server; + } + + print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n"; } print $header, "\n"; if ($smtp) {
t/t9001-send-email.sh +31 -0
@@ -2148,6 +2148,37 @@ test_expect_success $PREREQ 'leading and trailing whitespaces are removed' ' test_cmp expected-list actual-list ' +test_expect_success $PREREQ 'test using command name with --sendmail-cmd' ' + clean_fake_sendmail && + PATH="$(pwd):$PATH" \ + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd="fake.sendmail" \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd='\''"$(pwd)/fake.sendmail" -f [email protected]'\'' \ + HEAD^ && + test_path_is_file commandline1 +' + +test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' ' + clean_fake_sendmail && + git send-email \ + --from="Example <[email protected]>" \ + [email protected] \ + --sendmail-cmd='\''f() { "$(pwd)/fake.sendmail" "$@"; };f'\'' \ + HEAD^ && + test_path_is_file commandline1 +' + test_expect_success $PREREQ 'invoke hook' ' mkdir -p .git/hooks &&
Signature · 1 line
2.31.1.576.g1878d50f81