Merge branch 'jp/send-email-to-cmd'
authorJunio C Hamano <gitster@pobox.com>
Wed, 27 Oct 2010 04:52:26 +0000 (21:52 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Oct 2010 04:52:26 +0000 (21:52 -0700)
* jp/send-email-to-cmd:
git-send-email.perl: Add --to-cmd

Conflicts:
git-send-email.perl

1  2 
Documentation/git-send-email.txt
git-send-email.perl
t/t9001-send-email.sh
index cde404a461fc80a97d3af392eb8520b6361a5118,fff97a3dd23698673ea49d5d040cdd9c5df8ec97..05904e0e7f31896191bc10224c09d14e91372952
@@@ -97,7 -97,7 +97,7 @@@ See the CONFIGURATION section for 'send
        Specify the primary recipient of the emails generated. Generally, this
        will be the upstream maintainer of the project involved. Default is the
        value of the 'sendemail.to' configuration value; if that is unspecified,
-       this will be prompted for.
+       and --to-cmd is not specified, this will be prompted for.
  +
  The --to option must be repeated for each user you want on the to list.
  
@@@ -165,15 -165,6 +165,15 @@@ user is prompted for a password while t
        are also accepted. The port can also be set with the
        'sendemail.smtpserverport' configuration variable.
  
 +--smtp-server-option=<option>::
 +      If set, specifies the outgoing SMTP server option to use.
 +      Default value can be specified by the 'sendemail.smtpserveroption'
 +      configuration option.
 ++
 +The --smtp-server-option option must be repeated for each option you want
 +to pass to the server. Likewise, different lines in the configuration files
 +must be used for each option.
 +
  --smtp-ssl::
        Legacy alias for '--smtp-encryption ssl'.
  
  Automating
  ~~~~~~~~~~
  
+ --to-cmd=<command>::
+       Specify a command to execute once per patch file which
+       should generate patch file specific "To:" entries.
+       Output of this command must be single email address per line.
+       Default is the value of 'sendemail.tocmd' configuration value.
  --cc-cmd=<command>::
        Specify a command to execute once per patch file which
        should generate patch file specific "Cc:" entries.
diff --combined git-send-email.perl
index 458e86afdedfbad45f12ec1910735c617ffbe8c7,a73b655e417df641b8213734aa3ba19bd9914973..897bf5960a4220005f4ddc80ec1942136515298b
@@@ -24,7 -24,6 +24,7 @@@ use Text::ParseWords
  use Data::Dumper;
  use Term::ANSIColor;
  use File::Temp qw/ tempdir tempfile /;
 +use File::Spec::Functions qw(catfile);
  use Error qw(:try);
  use Git;
  
@@@ -61,7 -60,6 +61,7 @@@ git send-email [options] <file | direct
      --envelope-sender       <str>  * Email envelope sender.
      --smtp-server       <str:int>  * Outgoing SMTP server to use. The port
                                       is optional. Default 'localhost'.
 +    --smtp-server-option    <str>  * Outgoing SMTP server option to use.
      --smtp-server-port      <int>  * Outgoing SMTP server port.
      --smtp-user             <str>  * Username for SMTP-AUTH.
      --smtp-pass             <str>  * Password for SMTP-AUTH; not necessary.
@@@ -72,6 -70,7 +72,7 @@@
  
    Automating:
      --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.
      --[no-]signed-off-by-cc        * Send to Signed-off-by: addresses. Default on.
@@@ -87,7 -86,6 +88,7 @@@
      --[no-]validate                * Perform patch sanity checks. Default on.
      --[no-]format-patch            * understand any non optional arguments as
                                       `git format-patch` ones.
 +    --force                        * Send even if safety checks would prevent it.
  
  EOT
        exit(1);
@@@ -165,7 -163,6 +166,7 @@@ if ($@) 
  my ($quiet, $dry_run) = (0, 0);
  my $format_patch;
  my $compose_filename;
 +my $force = 0;
  
  # Handle interactive edition of files.
  my $multiedit;
@@@ -191,10 -188,10 +192,11 @@@ sub do_edit 
  }
  
  # Variables with corresponding config settings
- my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd);
+ my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc);
+ my ($to_cmd, $cc_cmd);
 -my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption);
 -my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain);
 +my ($smtp_server, $smtp_server_port, @smtp_server_options);
 +my ($smtp_authuser, $smtp_encryption);
 +my ($identity, $aliasfiletype, @alias_files, $smtp_domain);
  my ($validate, $confirm);
  my (@suppress_cc);
  my ($auto_8bit_encoding);
@@@ -215,11 -212,11 +217,12 @@@ my %config_bool_settings = 
  my %config_settings = (
      "smtpserver" => \$smtp_server,
      "smtpserverport" => \$smtp_server_port,
 +    "smtpserveroption" => \@smtp_server_options,
      "smtpuser" => \$smtp_authuser,
      "smtppass" => \$smtp_authpass,
 -      "smtpdomain" => \$smtp_domain,
 +    "smtpdomain" => \$smtp_domain,
      "to" => \@to,
+     "tocmd" => \$to_cmd,
      "cc" => \@initial_cc,
      "cccmd" => \$cc_cmd,
      "aliasfiletype" => \$aliasfiletype,
@@@ -278,6 -275,7 +281,7 @@@ my $rc = GetOptions("sender|from=s" => 
                      "in-reply-to=s" => \$initial_reply_to,
                    "subject=s" => \$initial_subject,
                    "to=s" => \@to,
+                   "to-cmd=s" => \$to_cmd,
                    "no-to" => \$no_to,
                    "cc=s" => \@initial_cc,
                    "no-cc" => \$no_cc,
                    "no-bcc" => \$no_bcc,
                    "chain-reply-to!" => \$chain_reply_to,
                    "smtp-server=s" => \$smtp_server,
 +                  "smtp-server-option=s" => \@smtp_server_options,
                    "smtp-server-port=s" => \$smtp_server_port,
                    "smtp-user=s" => \$smtp_authuser,
                    "smtp-pass:s" => \$smtp_authpass,
                    "validate!" => \$validate,
                    "format-patch!" => \$format_patch,
                    "8bit-encoding=s" => \$auto_8bit_encoding,
 +                  "force" => \$force,
         );
  
  unless ($rc) {
@@@ -519,7 -515,7 +523,7 @@@ while (defined(my $f = shift @ARGV)) 
                opendir(DH,$f)
                        or die "Failed to opendir $f: $!";
  
 -              push @files, grep { -f $_ } map { +$f . "/" . $_ }
 +              push @files, grep { -f $_ } map { catfile($f, $_) }
                                sort readdir(DH);
                closedir(DH);
        } elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) {
@@@ -710,16 -706,6 +714,16 @@@ if (!defined $auto_8bit_encoding && sca
                                  default => "UTF-8");
  }
  
 +if (!$force) {
 +      for my $f (@files) {
 +              if (get_patch_subject($f) =~ /\*\*\* SUBJECT HERE \*\*\*/) {
 +                      die "Refusing to send because the patch\n\t$f\n"
 +                              . "has the template subject '*** SUBJECT HERE ***'. "
 +                              . "Pass --force if you really want to send.\n";
 +              }
 +      }
 +}
 +
  my $prompting = 0;
  if (!defined $sender) {
        $sender = $repoauthor || $repocommitter || '';
        $prompting++;
  }
  
- if (!@to) {
+ if (!@to && !defined $to_cmd) {
        my $to = ask("Who should the emails be sent to? ");
        push @to, parse_address_line($to) if defined $to; # sanitized/validated later
        $prompting++;
@@@ -913,7 -899,7 +917,7 @@@ sub sanitize_address 
  
  sub valid_fqdn {
        my $domain = shift;
 -      return !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
 +      return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
  }
  
  sub maildomain_net {
@@@ -1033,8 -1019,6 +1037,8 @@@ X-Mailer: git-send-email $gitversio
                }
        }
  
 +      unshift (@sendmail_parameters, @smtp_server_options);
 +
        if ($dry_run) {
                # We don't want to send the email.
        } elsif ($smtp_server =~ m#^/#) {
@@@ -1258,21 -1242,10 +1262,10 @@@ foreach my $t (@files) 
        }
        close F;
  
-       if (defined $cc_cmd && !$suppress_cc{'cccmd'}) {
-               open(F, "$cc_cmd \Q$t\E |")
-                       or die "(cc-cmd) Could not execute '$cc_cmd'";
-               while(<F>) {
-                       my $c = $_;
-                       $c =~ s/^\s*//g;
-                       $c =~ s/\n$//g;
-                       next if ($c eq $sender and $suppress_from);
-                       push @cc, $c;
-                       printf("(cc-cmd) Adding cc: %s from: '%s'\n",
-                               $c, $cc_cmd) unless $quiet;
-               }
-               close F
-                       or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
-       }
+       push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t)
+               if defined $to_cmd;
+       push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t)
+               if defined $cc_cmd && !$suppress_cc{'cccmd'};
  
        if ($broken_encoding{$t} && !$has_content_type) {
                $has_content_type = 1;
        $message_id = undef;
  }
  
+ # Execute a command (e.g. $to_cmd) to get a list of email addresses
+ # and return a results array
+ sub recipients_cmd {
+       my ($prefix, $what, $cmd, $file) = @_;
+       my $sanitized_sender = sanitize_address($sender);
+       my @addresses = ();
+       open(F, "$cmd \Q$file\E |")
+           or die "($prefix) Could not execute '$cmd'";
+       while(<F>) {
+               my $address = $_;
+               $address =~ s/^\s*//g;
+               $address =~ s/\s*$//g;
+               $address = sanitize_address($address);
+               next if ($address eq $sanitized_sender and $suppress_from);
+               push @addresses, $address;
+               printf("($prefix) Adding %s: %s from: '%s'\n",
+                      $what, $address, $cmd) unless $quiet;
+               }
+       close F
+           or die "($prefix) failed to close pipe to '$cmd'";
+       return @addresses;
+ }
  cleanup_compose_files();
  
  sub cleanup_compose_files() {
diff --combined t/t9001-send-email.sh
index a298eb04373f622473e3d33c9a46c6734f8a8fc8,36cf421dd097f9f3838a5e5b244983aae70c1316..ba11c00c74468f996ba718b2c07dd1852c21d30d
@@@ -201,6 -201,24 +201,24 @@@ test_expect_success $PREREQ 'Prompting 
                grep "^To: to@example.com\$" msgtxt1
  '
  
+ test_expect_success $PREREQ 'tocmd works' '
+       clean_fake_sendmail &&
+       cp $patches tocmd.patch &&
+       echo tocmd--tocmd@example.com >>tocmd.patch &&
+       {
+         echo "#!$SHELL_PATH"
+         echo sed -n -e s/^tocmd--//p \"\$1\"
+       } > tocmd-sed &&
+       chmod +x tocmd-sed &&
+       git send-email \
+               --from="Example <nobody@example.com>" \
+               --to-cmd=./tocmd-sed \
+               --smtp-server="$(pwd)/fake.sendmail" \
+               tocmd.patch \
+               &&
+       grep "^To: tocmd@example.com" msgtxt1
+ '
  test_expect_success $PREREQ 'cccmd works' '
        clean_fake_sendmail &&
        cp $patches cccmd.patch &&
@@@ -279,7 -297,7 +297,7 @@@ test_expect_success $PREREQ 'Invalid In
                --to=nobody@example.com \
                --in-reply-to=" " \
                --smtp-server="$(pwd)/fake.sendmail" \
 -              $patches
 +              $patches \
                2>errors
        ! grep "^In-Reply-To: < *>" msgtxt1
  '
@@@ -1032,40 -1050,4 +1050,40 @@@ test_expect_success $PREREQ '--8bit-enc
        test_cmp expected actual
  '
  
 +# Note that the patches in this test are deliberately out of order; we
 +# want to make sure it works even if the cover-letter is not in the
 +# first mail.
 +test_expect_success 'refusing to send cover letter template' '
 +      clean_fake_sendmail &&
 +      rm -fr outdir &&
 +      git format-patch --cover-letter -2 -o outdir &&
 +      test_must_fail git send-email \
 +        --from="Example <nobody@example.com>" \
 +        --to=nobody@example.com \
 +        --smtp-server="$(pwd)/fake.sendmail" \
 +        outdir/0002-*.patch \
 +        outdir/0000-*.patch \
 +        outdir/0001-*.patch \
 +        2>errors >out &&
 +      grep "SUBJECT HERE" errors &&
 +      test -z "$(ls msgtxt*)"
 +'
 +
 +test_expect_success '--force sends cover letter template anyway' '
 +      clean_fake_sendmail &&
 +      rm -fr outdir &&
 +      git format-patch --cover-letter -2 -o outdir &&
 +      git send-email \
 +        --force \
 +        --from="Example <nobody@example.com>" \
 +        --to=nobody@example.com \
 +        --smtp-server="$(pwd)/fake.sendmail" \
 +        outdir/0002-*.patch \
 +        outdir/0000-*.patch \
 +        outdir/0001-*.patch \
 +        2>errors >out &&
 +      ! grep "SUBJECT HERE" errors &&
 +      test -n "$(ls msgtxt*)"
 +'
 +
  test_done