@matheusbsbs / matheus-bagliano / commits / dd84e528a3
Fork 0

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 <sir@cmpwn.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Drew DeVault committed Jul 23, 2020 at 20:44 UTC dd84e528a34c3822e7ab0473a95e411665b37681
4 files changed +68
Documentation/config/sendemail.txt
+5
@@ -61,3 +61,8 @@ sendemail.smtpBatchSize::
61 sendemail.smtpReloginDelay::
62 Seconds wait before reconnecting to smtp server.
63 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
+8
@@ -250,6 +250,7 @@ my $chain_reply_to = 0;
250 my $use_xmailer = 1;
251 my $validate = 1;
252 my $target_xfer_encoding = 'auto';
253 +my $forbid_sendmail_variables = 1;
254
255 my %config_bool_settings = (
256 "thread" => \$thread,
@@ -263,6 +264,7 @@ my %config_bool_settings = (
264 "multiedit" => \$multiedit,
265 "annotate" => \$annotate,
266 "xmailer" => \$use_xmailer,
267 + "forbidsendmailvariables" => \$forbid_sendmail_variables,
268 );
269
270 my %config_settings = (
@@ -478,6 +480,12 @@ unless ($rc) {
480 usage();
481 }
482
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 +
489 die __("Cannot run git format-patch from outside a repository\n")
490 if $format_patch and not $repo;
491
perl/Git.pm
+26
@@ -723,6 +723,32 @@ sub config_int {
723 return scalar _config_common({'kind' => '--int'}, @_);
724 }
725
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 +
752 # Common subroutine to implement bulk of what the config* family of methods
753 # do. This currently wraps command('config') so it is not so fast.
754 sub _config_common {
t/t9001-send-email.sh
+29
@@ -2142,4 +2142,33 @@ test_expect_success $PREREQ 'test that send-email works outside a repo' '
2142 "$(pwd)/0001-add-master.patch"
2143 '
2144
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 <nobody@example.com>" \
2149 + --to=nobody@example.com \
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 <nobody@example.com>" \
2159 + --to=nobody@example.com \
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 <nobody@example.com>" \
2169 + --to=nobody@example.com \
2170 + --smtp-server="$(pwd)/fake.sendmail" \
2171 + HEAD^
2172 +'
2173 +
2174 test_done