Skip to content

Commit dd84e52

Browse files
ddevaultgitster
authored andcommitted
git-send-email: die if sendmail.* config is set
I've seen several people mis-configure git send-email on their first attempt because they set the sendmail.* config options - not sendemail.*. This patch detects this mistake and bails out with a friendly warning. Signed-off-by: Drew DeVault <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3d20111 commit dd84e52

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

Documentation/config/sendemail.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@ sendemail.smtpBatchSize::
6161
sendemail.smtpReloginDelay::
6262
Seconds wait before reconnecting to smtp server.
6363
See also the `--relogin-delay` option of linkgit:git-send-email[1].
64+
65+
sendemail.forbidSendmailVariables::
66+
To avoid common misconfiguration mistakes, linkgit:git-send-email[1]
67+
will abort with a warning if any configuration options for "sendmail"
68+
exist. Set this variable to bypass the check.

git-send-email.perl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ sub do_edit {
250250
my $use_xmailer = 1;
251251
my $validate = 1;
252252
my $target_xfer_encoding = 'auto';
253+
my $forbid_sendmail_variables = 1;
253254

254255
my %config_bool_settings = (
255256
"thread" => \$thread,
@@ -263,6 +264,7 @@ sub do_edit {
263264
"multiedit" => \$multiedit,
264265
"annotate" => \$annotate,
265266
"xmailer" => \$use_xmailer,
267+
"forbidsendmailvariables" => \$forbid_sendmail_variables,
266268
);
267269

268270
my %config_settings = (
@@ -478,6 +480,12 @@ sub read_config {
478480
usage();
479481
}
480482

483+
if ($forbid_sendmail_variables && (scalar Git::config_regexp("^sendmail[.]")) != 0) {
484+
die __("fatal: found configuration options for 'sendmail'\n" .
485+
"git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
486+
"Set sendemail.forbidSendmailVariables to false to disable this check.\n");
487+
}
488+
481489
die __("Cannot run git format-patch from outside a repository\n")
482490
if $format_patch and not $repo;
483491

perl/Git.pm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,32 @@ sub config_int {
723723
return scalar _config_common({'kind' => '--int'}, @_);
724724
}
725725

726+
=item config_regexp ( RE )
727+
728+
Retrieve the list of configuration key names matching the regular
729+
expression C<RE>. The return value is a list of strings matching
730+
this regex.
731+
732+
=cut
733+
734+
sub config_regexp {
735+
my ($self, $regex) = _maybe_self(@_);
736+
try {
737+
my @cmd = ('config', '--name-only', '--get-regexp', $regex);
738+
unshift @cmd, $self if $self;
739+
my @matches = command(@cmd);
740+
return @matches;
741+
} catch Git::Error::Command with {
742+
my $E = shift;
743+
if ($E->value() == 1) {
744+
my @matches = ();
745+
return @matches;
746+
} else {
747+
throw $E;
748+
}
749+
};
750+
}
751+
726752
# Common subroutine to implement bulk of what the config* family of methods
727753
# do. This currently wraps command('config') so it is not so fast.
728754
sub _config_common {

t/t9001-send-email.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,4 +2142,33 @@ test_expect_success $PREREQ 'test that send-email works outside a repo' '
21422142
"$(pwd)/0001-add-master.patch"
21432143
'
21442144

2145+
test_expect_success $PREREQ 'test that sendmail config is rejected' '
2146+
test_config sendmail.program sendmail &&
2147+
test_must_fail git send-email \
2148+
--from="Example <[email protected]>" \
2149+
2150+
--smtp-server="$(pwd)/fake.sendmail" \
2151+
HEAD^ 2>err &&
2152+
test_i18ngrep "found configuration options for '"'"sendmail"'"'" err
2153+
'
2154+
2155+
test_expect_success $PREREQ 'test that sendmail config rejection is specific' '
2156+
test_config resendmail.program sendmail &&
2157+
git send-email \
2158+
--from="Example <[email protected]>" \
2159+
2160+
--smtp-server="$(pwd)/fake.sendmail" \
2161+
HEAD^
2162+
'
2163+
2164+
test_expect_success $PREREQ 'test forbidSendmailVariables behavior override' '
2165+
test_config sendmail.program sendmail &&
2166+
test_config sendemail.forbidSendmailVariables false &&
2167+
git send-email \
2168+
--from="Example <[email protected]>" \
2169+
2170+
--smtp-server="$(pwd)/fake.sendmail" \
2171+
HEAD^
2172+
'
2173+
21452174
test_done

0 commit comments

Comments
 (0)