Code

Merge branch 'maint' of git://linux-nfs.org/~bfields/git into maint
[git.git] / git-send-email.perl
index 4767249e6d9ef25a6618e99a2557cd909be1573c..2b1f1b598c62ddaed693635adef12e490f49838f 100755 (executable)
@@ -21,8 +21,11 @@ use warnings;
 use Term::ReadLine;
 use Getopt::Long;
 use Data::Dumper;
+use Term::ANSIColor;
 use Git;
 
+$SIG{INT} = sub { print color("reset"), "\n"; exit };
+
 package FakeTerm;
 sub new {
        my ($class, $reason) = @_;
@@ -300,7 +303,7 @@ sub expand_aliases {
 
 if (!defined $initial_subject && $compose) {
        do {
-               $_ = $term->readline("What subject should the emails start with? ",
+               $_ = $term->readline("What subject should the initial email start with? ",
                        $initial_subject);
        } while (!defined $_);
        $initial_subject = $_;
@@ -314,7 +317,8 @@ if ($thread && !defined $initial_reply_to && $prompting) {
        } while (!defined $_);
 
        $initial_reply_to = $_;
-       $initial_reply_to =~ s/(^\s+|\s+$)//g;
+       $initial_reply_to =~ s/^\s+<?/</;
+       $initial_reply_to =~ s/>?\s+$/>/;
 }
 
 if (!$smtp_server) {
@@ -433,10 +437,17 @@ sub extract_valid_address {
 
 # We'll setup a template for the message id, using the "from" address:
 
+my ($message_id_stamp, $message_id_serial);
 sub make_message_id
 {
-       my $date = time;
-       my $pseudo_rand = int (rand(4200));
+       my $uniq;
+       if (!defined $message_id_stamp) {
+               $message_id_stamp = sprintf("%s-%s", time, $$);
+               $message_id_serial = 0;
+       }
+       $message_id_serial++;
+       $uniq = "$message_id_stamp-$message_id_serial";
+
        my $du_part;
        for ($sender, $repocommitter, $repoauthor) {
                $du_part = extract_valid_address(sanitize_address($_));
@@ -446,8 +457,8 @@ sub make_message_id
                use Sys::Hostname qw();
                $du_part = 'user@' . Sys::Hostname::hostname();
        }
-       my $message_id_template = "<%s-git-send-email-$du_part>";
-       $message_id = sprintf $message_id_template, "$date$pseudo_rand";
+       my $message_id_template = "<%s-git-send-email-%s>";
+       $message_id = sprintf($message_id_template, $uniq, $du_part);
        #print "new message id = $message_id\n"; # Was useful for debugging
 }
 
@@ -457,11 +468,13 @@ $time = time - scalar $#files;
 
 sub unquote_rfc2047 {
        local ($_) = @_;
-       if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
+       my $encoding;
+       if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) {
+               $encoding = $1;
                s/_/ /g;
                s/=([0-9A-F]{2})/chr(hex($1))/eg;
        }
-       return "$_";
+       return wantarray ? ($_, $encoding) : $_;
 }
 
 # use the simplest quoting being able to handle the recipient
@@ -588,6 +601,9 @@ foreach my $t (@files) {
        open(F,"<",$t) or die "can't open file $t";
 
        my $author = undef;
+       my $author_encoding;
+       my $has_content_type;
+       my $body_encoding;
        @cc = @initial_cc;
        @xh = ();
        my $input_format = undef;
@@ -613,12 +629,20 @@ foreach my $t (@files) {
                                                next if ($suppress_from);
                                        }
                                        elsif ($1 eq 'From') {
-                                               $author = unquote_rfc2047($2);
+                                               ($author, $author_encoding)
+                                                 = unquote_rfc2047($2);
                                        }
                                        printf("(mbox) Adding cc: %s from line '%s'\n",
                                                $2, $_) unless $quiet;
                                        push @cc, $2;
                                }
+                               elsif (/^Content-type:/i) {
+                                       $has_content_type = 1;
+                                       if (/charset="?[^ "]+/) {
+                                               $body_encoding = $1;
+                                       }
+                                       push @xh, $_;
+                               }
                                elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
                                        push @xh, $_;
                                }
@@ -675,6 +699,21 @@ foreach my $t (@files) {
 
        if (defined $author) {
                $message = "From: $author\n\n$message";
+               if (defined $author_encoding) {
+                       if ($has_content_type) {
+                               if ($body_encoding eq $author_encoding) {
+                                       # ok, we already have the right encoding
+                               }
+                               else {
+                                       # uh oh, we should re-encode
+                               }
+                       }
+                       else {
+                               push @xh,
+                                 'MIME-Version: 1.0',
+                                 "Content-Type: text/plain; charset=$author_encoding";
+                       }
+               }
        }
 
        send_message();