Merge branch 'db/send-email-omit-cc'
[gitweb.git] / git-send-email.perl
index 76baa8e431e082c1e36eb9c78125094de0b8a85e..8de5789f669aeefa1d5eba938e8b41442ede9ffa 100755 (executable)
@@ -88,6 +88,12 @@ sub usage {
 
    --smtp-ssl     If set, connects to the SMTP server using SSL.
 
+   --suppress-cc  Suppress the specified category of auto-CC.  The category
+                 can be one of 'author' for the patch author, 'self' to
+                 avoid copying yourself, 'sob' for Signed-off-by lines,
+                 'cccmd' for the output of the cccmd, or 'all' to suppress
+                 all of these.
+
    --suppress-from Suppress sending emails to yourself. Defaults to off.
 
    --thread       Specify that the "In-Reply-To:" header should be set on all
@@ -100,6 +106,8 @@ sub usage {
 
    --envelope-sender   Specify the envelope sender used to send the emails.
 
+   --no-validate       Don't perform any sanity checks on patches.
+
 EOT
        exit(1);
 }
@@ -177,12 +185,14 @@ sub format_2822_time {
 my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
 my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_authpass, $smtp_ssl);
 my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts);
+my ($no_validate);
+my (@suppress_cc);
 
 my %config_bool_settings = (
     "thread" => [\$thread, 1],
     "chainreplyto" => [\$chain_reply_to, 1],
-    "suppressfrom" => [\$suppress_from, 0],
-    "signedoffcc" => [\$signed_off_cc, 1],
+    "suppressfrom" => [\$suppress_from, undef],
+    "signedoffcc" => [\$signed_off_cc, undef],
     "smtpssl" => [\$smtp_ssl, 0],
 );
 
@@ -196,6 +206,7 @@ sub format_2822_time {
     "aliasfiletype" => \$aliasfiletype,
     "bcc" => \@bcclist,
     "aliasesfile" => \@alias_files,
+    "suppresscc" => \@suppress_cc,
 );
 
 # Begin by accumulating all the variables (defined above), that we will end up
@@ -218,10 +229,12 @@ sub format_2822_time {
                    "quiet" => \$quiet,
                    "cc-cmd=s" => \$cc_cmd,
                    "suppress-from!" => \$suppress_from,
+                   "suppress-cc=s" => \@suppress_cc,
                    "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
                    "dry-run" => \$dry_run,
                    "envelope-sender=s" => \$envelope_sender,
                    "thread!" => \$thread,
+                   "no-validate" => \$no_validate,
         );
 
 unless ($rc) {
@@ -262,6 +275,35 @@ sub read_config {
        ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]}));
 }
 
+# Set CC suppressions
+my(%suppress_cc);
+if (@suppress_cc) {
+       foreach my $entry (@suppress_cc) {
+               die "Unknown --suppress-cc field: '$entry'\n"
+                       unless $entry =~ /^(all|cccmd|cc|author|self|sob)$/;
+               $suppress_cc{$entry} = 1;
+       }
+}
+
+if ($suppress_cc{'all'}) {
+       foreach my $entry (qw (ccmd cc author self sob)) {
+               $suppress_cc{$entry} = 1;
+       }
+       delete $suppress_cc{'all'};
+}
+
+# If explicit old-style ones are specified, they trump --suppress-cc.
+$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
+$suppress_cc{'sob'} = $signed_off_cc if defined $signed_off_cc;
+
+# Debugging, print out the suppressions.
+if (0) {
+       print "suppressions:\n";
+       foreach my $entry (keys %suppress_cc) {
+               printf "  %-5s -> $suppress_cc{$entry}\n", $entry;
+       }
+}
+
 my ($repoauthor) = $repo->ident_person('author');
 my ($repocommitter) = $repo->ident_person('committer');
 
@@ -314,6 +356,40 @@ sub read_config {
 
 ($sender) = expand_aliases($sender) if defined $sender;
 
+# Now that all the defaults are set, process the rest of the command line
+# arguments and collect up the files that need to be processed.
+for my $f (@ARGV) {
+       if (-d $f) {
+               opendir(DH,$f)
+                       or die "Failed to opendir $f: $!";
+
+               push @files, grep { -f $_ } map { +$f . "/" . $_ }
+                               sort readdir(DH);
+
+       } elsif (-f $f) {
+               push @files, $f;
+
+       } else {
+               print STDERR "Skipping $f - not found.\n";
+       }
+}
+
+if (!$no_validate) {
+       foreach my $f (@files) {
+               my $error = validate_patch($f);
+               $error and die "fatal: $f: $error\nwarning: no patches were sent\n";
+       }
+}
+
+if (@files) {
+       unless ($quiet) {
+               print $_,"\n" for (@files);
+       }
+} else {
+       print STDERR "\nNo patch files specified!\n\n";
+       usage();
+}
+
 my $prompting = 0;
 if (!defined $sender) {
        $sender = $repoauthor || $repocommitter;
@@ -367,8 +443,10 @@ sub expand_aliases {
        } while (!defined $_);
 
        $initial_reply_to = $_;
-       $initial_reply_to =~ s/^\s+<?/</;
-       $initial_reply_to =~ s/>?\s+$/>/;
+}
+if (defined $initial_reply_to && $_ ne "") {
+       $initial_reply_to =~ s/^\s*<?/</;
+       $initial_reply_to =~ s/>?\s*$/>/;
 }
 
 if (!defined $smtp_server) {
@@ -398,7 +476,7 @@ sub expand_aliases {
        close(C);
 
        my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
-       system($editor, $compose_filename);
+       system('sh', '-c', '$0 $@', $editor, $compose_filename);
 
        open(C2,">",$compose_filename . ".final")
                or die "Failed to open $compose_filename.final : " . $!;
@@ -422,35 +500,7 @@ sub expand_aliases {
                exit(0);
        }
 
-       @files = ($compose_filename . ".final");
-}
-
-
-# Now that all the defaults are set, process the rest of the command line
-# arguments and collect up the files that need to be processed.
-for my $f (@ARGV) {
-       if (-d $f) {
-               opendir(DH,$f)
-                       or die "Failed to opendir $f: $!";
-
-               push @files, grep { -f $_ } map { +$f . "/" . $_ }
-                               sort readdir(DH);
-
-       } elsif (-f $f) {
-               push @files, $f;
-
-       } else {
-               print STDERR "Skipping $f - not found.\n";
-       }
-}
-
-if (@files) {
-       unless ($quiet) {
-               print $_,"\n" for (@files);
-       }
-} else {
-       print STDERR "\nNo patch files specified!\n\n";
-       usage();
+       @files = ($compose_filename . ".final", @files);
 }
 
 # Variables we set as part of the loop over files
@@ -578,7 +628,7 @@ sub send_message
                $ccline = "\nCc: $cc";
        }
        my $sanitized_sender = sanitize_address($sender);
-       make_message_id();
+       make_message_id() unless defined($message_id);
 
        my $header = "From: $sanitized_sender
 To: $to${ccline}
@@ -699,11 +749,14 @@ sub send_message
 
                                } elsif (/^(Cc|From):\s+(.*)$/) {
                                        if (unquote_rfc2047($2) eq $sender) {
-                                               next if ($suppress_from);
+                                               next if ($suppress_cc{'self'});
                                        }
                                        elsif ($1 eq 'From') {
                                                ($author, $author_encoding)
                                                  = unquote_rfc2047($2);
+                                               next if ($suppress_cc{'author'});
+                                       } else {
+                                               next if ($suppress_cc{'cc'});
                                        }
                                        printf("(mbox) Adding cc: %s from line '%s'\n",
                                                $2, $_) unless $quiet;
@@ -716,6 +769,9 @@ sub send_message
                                        }
                                        push @xh, $_;
                                }
+                               elsif (/^Message-Id: (.*)/i) {
+                                       $message_id = $1;
+                               }
                                elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
                                        push @xh, $_;
                                }
@@ -727,7 +783,7 @@ sub send_message
                                # line 2 = subject
                                # So let's support that, too.
                                $input_format = 'lots';
-                               if (@cc == 0) {
+                               if (@cc == 0 && !$suppress_cc{'cc'}) {
                                        printf("(non-mbox) Adding cc: %s from line '%s'\n",
                                                $_, $_) unless $quiet;
 
@@ -744,10 +800,11 @@ sub send_message
                        }
                } else {
                        $message .=  $_;
-                       if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
+                       if (/^(Signed-off-by|Cc): (.*)$/i) {
+                               next if ($suppress_cc{'sob'});
                                my $c = $2;
                                chomp $c;
-                               next if ($c eq $sender and $suppress_from);
+                               next if ($c eq $sender and $suppress_cc{'self'});
                                push @cc, $c;
                                printf("(sob) Adding cc: %s from line '%s'\n",
                                        $c, $_) unless $quiet;
@@ -756,7 +813,7 @@ sub send_message
        }
        close F;
 
-       if (defined $cc_cmd) {
+       if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
                open(F, "$cc_cmd $t |")
                        or die "(cc-cmd) Could not execute '$cc_cmd'";
                while(<F>) {
@@ -803,6 +860,7 @@ sub send_message
                        $references = "$message_id";
                }
        }
+       $message_id = undef;
 }
 
 if ($compose) {
@@ -832,3 +890,15 @@ (@)
        }
        return @emails;
 }
+
+sub validate_patch {
+       my $fn = shift;
+       open(my $fh, '<', $fn)
+               or die "unable to open $fn: $!\n";
+       while (my $line = <$fh>) {
+               if (length($line) > 998) {
+                       return "$.: patch contains a line longer than 998 characters";
+               }
+       }
+       return undef;
+}