{"thread":{"id":"53916","subject":"[PATCH v3] git-send-email: die if sendmail.* config is set","startedAt":"2020-07-24T00:44:35Z","lastAt":"2020-07-24T00:44:35Z","messageCount":1,"participants":["Drew DeVault"],"isPatch":true,"patchVersion":3,"patchTotal":null},"messages":[{"id":"401979","messageId":"20200724004432.185167-1-sir@cmpwn.com","threadId":"53916","inReplyTo":null,"subject":"[PATCH v3] git-send-email: die if sendmail.* config is set","fromName":"Drew DeVault","fromEmail":"sir@cmpwn.com","sentAt":"2020-07-24T00:44:32Z","receivedAt":"2020-07-24T00:44:35Z","isPatch":true,"sender":{"key":"sir@cmpwn.com","avatar":"https://avatars.githubusercontent.com/u/1310872?v=4"},"body":"I've seen several people mis-configure git send-email on their first\nattempt because they set the sendmail.* config options - not\nsendemail.*. This patch detects this mistake and bails out with a\nfriendly warning.\n\nSigned-off-by: Drew DeVault <sir@cmpwn.com>\n---\n Documentation/config/sendemail.txt |  5 +++++\n git-send-email.perl                |  8 ++++++++\n perl/Git.pm                        | 26 ++++++++++++++++++++++++++\n t/t9001-send-email.sh              | 29 +++++++++++++++++++++++++++++\n 4 files changed, 68 insertions(+)\n\ndiff --git a/Documentation/config/sendemail.txt b/Documentation/config/sendemail.txt\nindex 0006faf800..cbc5af42fd 100644\n--- a/Documentation/config/sendemail.txt\n+++ b/Documentation/config/sendemail.txt\n@@ -61,3 +61,8 @@ sendemail.smtpBatchSize::\n sendemail.smtpReloginDelay::\n \tSeconds wait before reconnecting to smtp server.\n \tSee also the `--relogin-delay` option of linkgit:git-send-email[1].\n+\n+sendemail.forbidSendmailVariables::\n+\tTo avoid common misconfiguration mistakes, linkgit:git-send-email[1]\n+\twill abort with a warning if any configuration options for \"sendmail\"\n+\texist. Set this variable to bypass the check.\ndiff --git a/git-send-email.perl b/git-send-email.perl\nindex 36c47bae1d..1f425c0809 100755\n--- a/git-send-email.perl\n+++ b/git-send-email.perl\n@@ -250,6 +250,7 @@ sub do_edit {\n my $use_xmailer = 1;\n my $validate = 1;\n my $target_xfer_encoding = 'auto';\n+my $forbid_sendmail_variables = 1;\n \n my %config_bool_settings = (\n     \"thread\" => \\$thread,\n@@ -263,6 +264,7 @@ sub do_edit {\n     \"multiedit\" => \\$multiedit,\n     \"annotate\" => \\$annotate,\n     \"xmailer\" => \\$use_xmailer,\n+    \"forbidsendmailvariables\" => \\$forbid_sendmail_variables,\n );\n \n my %config_settings = (\n@@ -478,6 +480,12 @@ sub read_config {\n     usage();\n }\n \n+if ($forbid_sendmail_variables && (scalar Git::config_regexp(\"^sendmail[.]\")) != 0) {\n+\tdie __(\"fatal: found configuration options for 'sendmail'\\n\" .\n+\t\t\"git-send-email is configured with the sendemail.* options - note the 'e'.\\n\" .\n+\t\t\"Set sendemail.forbidSendmailVariables to false to disable this check.\\n\");\n+}\n+\n die __(\"Cannot run git format-patch from outside a repository\\n\")\n \tif $format_patch and not $repo;\n \ndiff --git a/perl/Git.pm b/perl/Git.pm\nindex 54c9ed0dde..10df990959 100644\n--- a/perl/Git.pm\n+++ b/perl/Git.pm\n@@ -723,6 +723,32 @@ sub config_int {\n \treturn scalar _config_common({'kind' => '--int'}, @_);\n }\n \n+=item config_regexp ( RE )\n+\n+Retrieve the list of configuration key names matching the regular\n+expression C<RE>. The return value is a list of strings matching\n+this regex.\n+\n+=cut\n+\n+sub config_regexp {\n+\tmy ($self, $regex) = _maybe_self(@_);\n+\ttry {\n+\t\tmy @cmd = ('config', '--name-only', '--get-regexp', $regex);\n+\t\tunshift @cmd, $self if $self;\n+\t\tmy @matches = command(@cmd);\n+\t\treturn @matches;\n+\t} catch Git::Error::Command with {\n+\t\tmy $E = shift;\n+\t\tif ($E->value() == 1) {\n+\t\t\tmy @matches = ();\n+\t\t\treturn @matches;\n+\t\t} else {\n+\t\t\tthrow $E;\n+\t\t}\n+\t};\n+}\n+\n # Common subroutine to implement bulk of what the config* family of methods\n # do. This currently wraps command('config') so it is not so fast.\n sub _config_common {\ndiff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh\nindex ec261085ec..897bc6a631 100755\n--- a/t/t9001-send-email.sh\n+++ b/t/t9001-send-email.sh\n@@ -2142,4 +2142,33 @@ test_expect_success $PREREQ 'test that send-email works outside a repo' '\n \t\t\"$(pwd)/0001-add-master.patch\"\n '\n \n+test_expect_success $PREREQ 'test that sendmail config is rejected' '\n+\ttest_config sendmail.program sendmail &&\n+\ttest_must_fail git send-email \\\n+\t\t--from=\"Example <nobody@example.com>\" \\\n+\t\t--to=nobody@example.com \\\n+\t\t--smtp-server=\"$(pwd)/fake.sendmail\" \\\n+\t\tHEAD^ 2>err &&\n+\ttest_i18ngrep \"found configuration options for '\"'\"sendmail\"'\"'\" err\n+'\n+\n+test_expect_success $PREREQ 'test that sendmail config rejection is specific' '\n+\ttest_config resendmail.program sendmail &&\n+\tgit send-email \\\n+\t\t--from=\"Example <nobody@example.com>\" \\\n+\t\t--to=nobody@example.com \\\n+\t\t--smtp-server=\"$(pwd)/fake.sendmail\" \\\n+\t\tHEAD^\n+'\n+\n+test_expect_success $PREREQ 'test forbidSendmailVariables behavior override' '\n+\ttest_config sendmail.program sendmail &&\n+\ttest_config sendemail.forbidSendmailVariables false &&\n+\tgit send-email \\\n+\t\t--from=\"Example <nobody@example.com>\" \\\n+\t\t--to=nobody@example.com \\\n+\t\t--smtp-server=\"$(pwd)/fake.sendmail\" \\\n+\t\tHEAD^\n+'\n+\n test_done\n-- \n2.27.0\n\n"}]}