t3311: use test_commit_bulk
[gitweb.git] / git-send-email.perl
index 46a5242d253a8ef38635897f156499a5b23a6ffe..5f92c89c1c1be5ba2fadedd1d7a8d013fb7577fd 100755 (executable)
@@ -82,8 +82,11 @@ sub usage {
                                      Pass an empty string to disable certificate
                                      verification.
     --smtp-domain           <str>  * The domain name sent to HELO/EHLO handshake
-    --smtp-auth             <str>  * Space-separated list of allowed AUTH mechanisms.
+    --smtp-auth             <str>  * Space-separated list of allowed AUTH mechanisms, or
+                                     "none" to disable authentication.
                                      This setting forces to use one of the listed mechanisms.
+    --no-smtp-auth                   Disable SMTP authentication. Shorthand for
+                                     `--smtp-auth=none`
     --smtp-debug            <0|1>  * Disable, enable Net::SMTP debug.
 
     --batch-size            <int>  * send max <int> message per connection.
@@ -94,7 +97,7 @@ sub usage {
     --identity              <str>  * Use the sendemail.<id> options.
     --to-cmd                <str>  * Email To: via `<str> \$patch_path`
     --cc-cmd                <str>  * Email Cc: via `<str> \$patch_path`
-    --suppress-cc           <str>  * author, self, sob, cc, cccmd, body, bodycc, all.
+    --suppress-cc           <str>  * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
     --[no-]cc-cover                * Email Cc: addresses in the cover letter.
     --[no-]to-cover                * Email To: addresses in the cover letter.
     --[no-]signed-off-by-cc        * Send to Signed-off-by: addresses. Default on.
@@ -119,6 +122,11 @@ sub usage {
        exit(1);
 }
 
+sub completion_helper {
+    print Git::command('format-patch', '--git-completion-helper');
+    exit(0);
+}
+
 # most mail servers generate the Date: header, but not all...
 sub format_2822_time {
        my ($time) = @_;
@@ -174,7 +182,7 @@ sub format_2822_time {
        $author,$sender,$smtp_authpass,$annotate,$compose,$time);
 # Things we either get from config, *or* are overridden on the
 # command-line.
-my ($no_cc, $no_to, $no_bcc);
+my ($no_cc, $no_to, $no_bcc, $no_identity);
 my (@config_to, @getopt_to);
 my (@config_cc, @getopt_cc);
 my (@config_bcc, @getopt_bcc);
@@ -317,44 +325,53 @@ sub signal_handler {
 
 # Read our sendemail.* config
 sub read_config {
-       my ($prefix) = @_;
+       my ($configured, $prefix) = @_;
 
        foreach my $setting (keys %config_bool_settings) {
                my $target = $config_bool_settings{$setting};
                my $v = Git::config_bool(@repo, "$prefix.$setting");
-               $$target = $v if defined $v;
+               next unless defined $v;
+               next if $configured->{$setting}++;
+               $$target = $v;
        }
 
        foreach my $setting (keys %config_path_settings) {
                my $target = $config_path_settings{$setting};
                if (ref($target) eq "ARRAY") {
-                       unless (@$target) {
-                               my @values = Git::config_path(@repo, "$prefix.$setting");
-                               @$target = @values if (@values && defined $values[0]);
-                       }
+                       my @values = Git::config_path(@repo, "$prefix.$setting");
+                       next unless @values;
+                       next if $configured->{$setting}++;
+                       @$target = @values;
                }
                else {
                        my $v = Git::config_path(@repo, "$prefix.$setting");
-                       $$target = $v if defined $v;
+                       next unless defined $v;
+                       next if $configured->{$setting}++;
+                       $$target = $v;
                }
        }
 
        foreach my $setting (keys %config_settings) {
                my $target = $config_settings{$setting};
                if (ref($target) eq "ARRAY") {
-                       unless (@$target) {
-                               my @values = Git::config(@repo, "$prefix.$setting");
-                               @$target = @values if (@values && defined $values[0]);
-                       }
+                       my @values = Git::config(@repo, "$prefix.$setting");
+                       next unless @values;
+                       next if $configured->{$setting}++;
+                       @$target = @values;
                }
                else {
                        my $v = Git::config(@repo, "$prefix.$setting");
-                       $$target = $v if defined $v;
+                       next unless defined $v;
+                       next if $configured->{$setting}++;
+                       $$target = $v;
                }
        }
 
        if (!defined $smtp_encryption) {
-               my $enc = Git::config(@repo, "$prefix.smtpencryption");
+               my $setting = "$prefix.smtpencryption";
+               my $enc = Git::config(@repo, $setting);
+               return unless defined $enc;
+               return if $configured->{$setting}++;
                if (defined $enc) {
                        $smtp_encryption = $enc;
                } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) {
@@ -363,16 +380,30 @@ sub read_config {
        }
 }
 
+# sendemail.identity yields to --identity. We must parse this
+# special-case first before the rest of the config is read.
 $identity = Git::config(@repo, "sendemail.identity");
-read_config("sendemail.$identity") if defined $identity;
-read_config("sendemail");
+my $rc = GetOptions(
+       "identity=s" => \$identity,
+       "no-identity" => \$no_identity,
+);
+usage() unless $rc;
+undef $identity if $no_identity;
+
+# Now we know enough to read the config
+{
+    my %configured;
+    read_config(\%configured, "sendemail.$identity") if defined $identity;
+    read_config(\%configured, "sendemail");
+}
 
 # Begin by accumulating all the variables (defined above), that we will end up
 # needing, first, from the command line:
 
 my $help;
-my $rc = GetOptions("h" => \$help,
-                    "dump-aliases" => \$dump_aliases);
+my $git_completion_helper;
+$rc = GetOptions("h" => \$help,
+                 "dump-aliases" => \$dump_aliases);
 usage() unless $rc;
 die __("--dump-aliases incompatible with other options\n")
     if !$help and $dump_aliases and @ARGV;
@@ -401,7 +432,7 @@ sub read_config {
                    "smtp-debug:i" => \$debug_net_smtp,
                    "smtp-domain:s" => \$smtp_domain,
                    "smtp-auth=s" => \$smtp_auth,
-                   "identity=s" => \$identity,
+                   "no-smtp-auth" => sub {$smtp_auth = 'none'},
                    "annotate!" => \$annotate,
                    "no-annotate" => sub {$annotate = 0},
                    "compose" => \$compose,
@@ -433,6 +464,7 @@ sub read_config {
                    "no-xmailer" => sub {$use_xmailer = 0},
                    "batch-size=i" => \$batch_size,
                    "relogin-delay=i" => \$relogin_delay,
+                   "git-completion-helper" => \$git_completion_helper,
         );
 
 # Munge any "either config or getopt, not both" variables
@@ -441,6 +473,7 @@ sub read_config {
 my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
 
 usage() if $help;
+completion_helper() if $git_completion_helper;
 unless ($rc) {
     usage();
 }
@@ -459,14 +492,16 @@ sub read_config {
 my(%suppress_cc);
 if (@suppress_cc) {
        foreach my $entry (@suppress_cc) {
+               # Please update $__git_send_email_suppresscc_options
+               # in git-completion.bash when you add new options.
                die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
-                       unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/;
+                       unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
                $suppress_cc{$entry} = 1;
        }
 }
 
 if ($suppress_cc{'all'}) {
-       foreach my $entry (qw (cccmd cc author self sob body bodycc)) {
+       foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
                $suppress_cc{$entry} = 1;
        }
        delete $suppress_cc{'all'};
@@ -477,7 +512,7 @@ sub read_config {
 $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
 
 if ($suppress_cc{'body'}) {
-       foreach my $entry (qw (sob bodycc)) {
+       foreach my $entry (qw (sob bodycc misc-by)) {
                $suppress_cc{$entry} = 1;
        }
        delete $suppress_cc{'body'};
@@ -488,6 +523,8 @@ sub read_config {
 if ($confirm_unconfigured) {
        $confirm = scalar %suppress_cc ? 'compose' : 'auto';
 };
+# Please update $__git_send_email_confirm_options in
+# git-completion.bash when you add new options.
 die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
        unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
 
@@ -581,6 +618,8 @@ sub parse_sendmail_aliases {
                if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
                        $aliases{$1} = [ $2 ];
                }}}
+       # Please update _git_config() in git-completion.bash when you
+       # add new MUAs.
 );
 
 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
@@ -1247,7 +1286,7 @@ sub smtp_host_string {
 # (smtp_user was not specified), and 0 otherwise.
 
 sub smtp_auth_maybe {
-       if (!defined $smtp_authuser || $auth) {
+       if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
                return 1;
        }
 
@@ -1485,7 +1524,7 @@ sub send_message {
                                                         SSL => 1);
                        }
                }
-               else {
+               elsif (!$smtp) {
                        $smtp_server_port ||= 25;
                        $smtp ||= Net::SMTP->new($smtp_server,
                                                 Hello => $smtp_domain,
@@ -1507,7 +1546,6 @@ sub send_message {
                                        $smtp->starttls(ssl_verify_params())
                                                or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
                                }
-                               $smtp_encryption = '';
                                # Send EHLO again to receive fresh
                                # supported commands
                                $smtp->hello($smtp_domain);
@@ -1688,7 +1726,7 @@ sub process_file {
        # Now parse the message body
        while(<$fh>) {
                $message .=  $_;
-               if (/^(Signed-off-by|Cc): (.*)/i) {
+               if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
                        chomp;
                        my ($what, $c) = ($1, $2);
                        # strip garbage for the address we'll use:
@@ -1698,8 +1736,18 @@ sub process_file {
                        if ($sc eq $sender) {
                                next if ($suppress_cc{'self'});
                        } else {
-                               next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i;
-                               next if $suppress_cc{'bodycc'} and $what =~ /Cc/i;
+                               if ($what =~ /^Signed-off-by$/i) {
+                                       next if $suppress_cc{'sob'};
+                               } elsif ($what =~ /-by$/i) {
+                                       next if $suppress_cc{'misc-by'};
+                               } elsif ($what =~ /Cc/i) {
+                                       next if $suppress_cc{'bodycc'};
+                               }
+                       }
+                       if ($c !~ /.+@.+|<.+>/) {
+                               printf("(body) Ignoring %s from line '%s'\n",
+                                       $what, $_) unless $quiet;
+                               next;
                        }
                        push @cc, $c;
                        printf(__("(body) Adding cc: %s from line '%s'\n"),
@@ -1841,7 +1889,7 @@ sub apply_transfer_encoding {
        my $from = shift;
        my $to = shift;
 
-       return $message if ($from eq $to and $from ne '7bit');
+       return ($message, $to) if ($from eq $to and $from ne '7bit');
 
        require MIME::QuotedPrint;
        require MIME::Base64;
@@ -1851,7 +1899,7 @@ sub apply_transfer_encoding {
        $message = MIME::Base64::decode($message)
                if ($from eq 'base64');
 
-       $to = ($message =~ /.{999,}/) ? 'quoted-printable' : '8bit'
+       $to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
                if $to eq 'auto';
 
        die __("cannot send message as 7bit")