send-email: 'References:' should only reference what is sent
[gitweb.git] / git-send-email.perl
index 5916c86b6874c5d39952fc9565ad0e24e301bbb9..43f956b780846471742b6f486a3a558930d4ebd1 100755 (executable)
@@ -529,7 +529,7 @@ ($)
 
        print C <<EOT;
 From $tpl_sender # This line is ignored.
-GIT: Lines beginning in "GIT: " will be removed.
+GIT: Lines beginning in "GIT:" will be removed.
 GIT: Consider including an overall diffstat or table of contents
 GIT: for the patch you are writing.
 GIT:
@@ -562,7 +562,7 @@ ($)
        my $in_body = 0;
        my $summary_empty = 1;
        while(<C>) {
-               next if m/^GIT: /;
+               next if m/^GIT:/;
                if ($in_body) {
                        $summary_empty = 0 unless (/^\n$/);
                } elsif (/^\n$/) {
@@ -608,10 +608,13 @@ ($)
 
 sub ask {
        my ($prompt, %arg) = @_;
-       my $valid_re = $arg{valid_re} || ""; # "" matches anything
+       my $valid_re = $arg{valid_re};
        my $default = $arg{default};
        my $resp;
        my $i = 0;
+       return defined $default ? $default : undef
+               unless defined $term->IN and defined fileno($term->IN) and
+                      defined $term->OUT and defined fileno($term->OUT);
        while ($i++ < 10) {
                $resp = $term->readline($prompt);
                if (!defined $resp) { # EOF
@@ -621,7 +624,7 @@ sub ask {
                if ($resp eq '' and defined $default) {
                        return $default;
                }
-               if ($resp =~ /$valid_re/) {
+               if (!defined $valid_re or $resp =~ /$valid_re/) {
                        return $resp;
                }
        }
@@ -684,7 +687,7 @@ sub expand_aliases {
 
 # Variables we set as part of the loop over files
 our ($message_id, %mail, $subject, $reply_to, $references, $message,
-       $needs_confirm, $message_num);
+       $needs_confirm, $message_num, $ask_default);
 
 sub extract_valid_address {
        my $address = shift;
@@ -773,12 +776,13 @@ sub sanitize_address
        }
 
        # if recipient_name is already quoted, do nothing
-       if ($recipient_name =~ /^(".*"|=\?utf-8\?q\?.*\?=)$/) {
+       if ($recipient_name =~ /^("[[:ascii:]]*"|=\?utf-8\?q\?.*\?=)$/) {
                return $recipient;
        }
 
        # rfc2047 is needed if a non-ascii char is included
        if ($recipient_name =~ /[^[:ascii:]]/) {
+               $recipient_name =~ s/^"(.*)"$/$1/;
                $recipient_name = quote_rfc2047($recipient_name);
        }
 
@@ -792,6 +796,10 @@ sub sanitize_address
 
 }
 
+# Returns 1 if the message was sent, and 0 otherwise.
+# In actuality, the whole program dies when a there
+# is an error sending a message.
+
 sub send_message
 {
        my @recipients = unique_email_list(@to);
@@ -842,7 +850,6 @@ sub send_message
 
        if ($needs_confirm && !$dry_run) {
                print "\n$header\n";
-               my $ask_default;
                if ($needs_confirm eq "inform") {
                        $confirm_unconfigured = 0; # squelch this message for the rest of this run
                        $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
@@ -861,7 +868,7 @@ sub send_message
                         default => $ask_default);
                die "Send this email reply required" unless defined $_;
                if (/^n/i) {
-                       return;
+                       return 0;
                } elsif (/^q/i) {
                        cleanup_compose_files();
                        exit(0);
@@ -942,7 +949,7 @@ sub send_message
                $smtp->data or die $smtp->message;
                $smtp->datasend("$header\n$message") or die $smtp->message;
                $smtp->dataend() or die $smtp->message;
-               $smtp->ok or die "Failed to send $subject\n".$smtp->message;
+               $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message;
        }
        if ($quiet) {
                printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
@@ -963,6 +970,8 @@ sub send_message
                        print "Result: OK\n";
                }
        }
+
+       return 1;
 }
 
 $reply_to = $initial_reply_to;
@@ -1123,10 +1132,10 @@ sub send_message
 
        @cc = (@initial_cc, @cc);
 
-       send_message();
+       my $message_was_sent = send_message();
 
        # set up for the next message
-       if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
+       if ($message_was_sent and $chain_reply_to || not defined $reply_to || length($reply_to) == 0) {
                $reply_to = $message_id;
                if (length $references > 0) {
                        $references .= "\n $message_id";