Code

user-manual: update references discussion
[git.git] / git-send-email.perl
index 2fd5e87934edba0c17d657fe0878c8a380202328..8dc2ee0cf7461ace62c5ef7afac6dedc7814ddfb 100755 (executable)
@@ -87,7 +87,8 @@ my (@to,@cc,@initial_cc,@bcclist,@xh,
        $initial_reply_to,$initial_subject,@files,$from,$compose,$time);
 
 # Behavior modification variables
-my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc) = (1, 0, 0, 0);
+my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
+       $dry_run) = (1, 0, 0, 0, 0);
 my $smtp_server;
 
 # Example reply to:
@@ -116,6 +117,7 @@ my $rc = GetOptions("from=s" => \$from,
                    "quiet" => \$quiet,
                    "suppress-from" => \$suppress_from,
                    "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
+                   "dry-run" => \$dry_run,
         );
 
 # Verify the user input
@@ -176,11 +178,10 @@ my $prompting = 0;
 if (!defined $from) {
        $from = $author || $committer;
        do {
-               $_ = $term->readline("Who should the emails appear to be from? ",
-                       $from);
+               $_ = $term->readline("Who should the emails appear to be from? [$from] ");
        } while (!defined $_);
 
-       $from = $_;
+       $from = $_ if ($_);
        print "Emails will be sent from: ", $from, "\n";
        $prompting++;
 }
@@ -228,6 +229,9 @@ if (!defined $initial_reply_to && $prompting) {
        $initial_reply_to =~ s/(^\s+|\s+$)//g;
 }
 
+if (!$smtp_server) {
+       $smtp_server = $repo->config('sendemail.smtpserver');
+}
 if (!$smtp_server) {
        foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
                if (-x $_) {
@@ -398,6 +402,15 @@ sub make_message_id
 $cc = "";
 $time = time - scalar $#files;
 
+sub unquote_rfc2047 {
+       local ($_) = @_;
+       if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
+               s/_/ /g;
+               s/=([0-9A-F]{2})/chr(hex($1))/eg;
+       }
+       return "$_ - unquoted";
+}
+
 sub send_message
 {
        my @recipients = unique_email_list(@to);
@@ -409,6 +422,11 @@ sub send_message
            $gitversion = Git::version();
        }
 
+       my ($author_name) = ($from =~ /^(.*?)\s+</);
+       if ($author_name && $author_name =~ /\./ && $author_name !~ /^".*"$/) {
+               my ($name, $addr) = ($from =~ /^(.*?)(\s+<.*)/);
+               $from = "\"$name\"$addr";
+       }
        my $header = "From: $from
 To: $to
 Cc: $cc
@@ -426,7 +444,9 @@ X-Mailer: git-send-email $gitversion
                $header .= join("\n", @xh) . "\n";
        }
 
-       if ($smtp_server =~ m#^/#) {
+       if ($dry_run) {
+               # We don't want to send the email.
+       } elsif ($smtp_server =~ m#^/#) {
                my $pid = open my $sm, '|-';
                defined $pid or die $!;
                if (!$pid) {
@@ -476,15 +496,21 @@ foreach my $t (@files) {
        my $author_not_sender = undef;
        @cc = @initial_cc;
        @xh = ();
-       my $found_mbox = 0;
+       my $input_format = undef;
        my $header_done = 0;
        $message = "";
        while(<F>) {
                if (!$header_done) {
-                       $found_mbox = 1, next if (/^From /);
+                       if (/^From /) {
+                               $input_format = 'mbox';
+                               next;
+                       }
                        chomp;
+                       if (!defined $input_format && /^[-A-Za-z]+:\s/) {
+                               $input_format = 'mbox';
+                       }
 
-                       if ($found_mbox) {
+                       if (defined $input_format && $input_format eq 'mbox') {
                                if (/^Subject:\s+(.*)$/) {
                                        $subject = $1;
 
@@ -499,7 +525,7 @@ foreach my $t (@files) {
                                                $2, $_) unless $quiet;
                                        push @cc, $2;
                                }
-                               elsif (/^[-A-Za-z]+:\s+\S/) {
+                               elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
                                        push @xh, $_;
                                }
 
@@ -509,6 +535,7 @@ foreach my $t (@files) {
                                # line 1 = cc
                                # line 2 = subject
                                # So let's support that, too.
+                               $input_format = 'lots';
                                if (@cc == 0) {
                                        printf("(non-mbox) Adding cc: %s from line '%s'\n",
                                                $_, $_) unless $quiet;
@@ -537,6 +564,7 @@ foreach my $t (@files) {
        }
        close F;
        if (defined $author_not_sender) {
+               $author_not_sender = unquote_rfc2047($author_not_sender);
                $message = "From: $author_not_sender\n\n$message";
        }