Merge branch 'jc/send-email-skip-backup'
authorJunio C Hamano <gitster@pobox.com>
Mon, 11 Jul 2016 17:31:04 +0000 (10:31 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 11 Jul 2016 17:31:04 +0000 (10:31 -0700)
A careless invocation of "git send-email directory/" after editing
0001-change.patch with an editor often ends up sending both
0001-change.patch and its backup file, 0001-change.patch~, causing
embarrassment and a minor confusion. Detect such an input and
offer to skip the backup files when sending the patches out.

* jc/send-email-skip-backup:
send-email: detect and offer to skip backup files

1  2 
git-send-email.perl
diff --combined git-send-email.perl
index 69587856df1706a2da42d6ce2a87c25a054e9eb6,b1be698e2157ac62a00f7c26bf4968bd5c697be6..da81be40cb7f9af1a960be2c22f8e75c9d16822e
  use 5.008;
  use strict;
  use warnings;
 +use POSIX qw/strftime/;
  use Term::ReadLine;
  use Getopt::Long;
  use Text::ParseWords;
 -use Data::Dumper;
  use Term::ANSIColor;
  use File::Temp qw/ tempdir tempfile /;
  use File::Spec::Functions qw(catfile);
@@@ -533,7 -533,7 +533,7 @@@ my %parse_alias = 
                        $aliases{$alias} = \@addr
                }}},
        mailrc => sub { my $fh = shift; while (<$fh>) {
 -              if (/^alias\s+(\S+)\s+(.*)$/) {
 +              if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
                        # spaces delimit multiple addresses
                        $aliases{$1} = [ quotewords('\s+', 0, $2) ];
                }}},
@@@ -621,6 -621,8 +621,8 @@@ if (@rev_list_opts) 
        push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
  }
  
+ @files = handle_backup_files(@files);
  if ($validate) {
        foreach my $f (@files) {
                unless (-p $f) {
@@@ -827,10 -829,9 +829,10 @@@ if (defined $sender) 
  # But it's a no-op to run sanitize_address on an already sanitized address.
  $sender = sanitize_address($sender);
  
 +my $to_whom = "To whom should the emails be sent (if anyone)?";
  my $prompting = 0;
  if (!@initial_to && !defined $to_cmd) {
 -      my $to = ask("Who should the emails be sent to (if any)? ",
 +      my $to = ask("$to_whom ",
                     default => "",
                     valid_re => qr/\@.*\./, confirm_only => 1);
        push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
@@@ -925,7 -926,7 +927,7 @@@ sub validate_address 
                        cleanup_compose_files();
                        exit(0);
                }
 -              $address = ask("Who should the email be sent to (if any)? ",
 +              $address = ask("$to_whom ",
                        default => "",
                        valid_re => qr/\@.*\./, confirm_only => 1);
        }
@@@ -950,7 -951,7 +952,7 @@@ my ($message_id_stamp, $message_id_seri
  sub make_message_id {
        my $uniq;
        if (!defined $message_id_stamp) {
 -              $message_id_stamp = sprintf("%s-%s", time, $$);
 +              $message_id_stamp = strftime("%Y%m%d%H%M%S.$$", gmtime(time));
                $message_id_serial = 0;
        }
        $message_id_serial++;
                require Sys::Hostname;
                $du_part = 'user@' . Sys::Hostname::hostname();
        }
 -      my $message_id_template = "<%s-git-send-email-%s>";
 +      my $message_id_template = "<%s-%s>";
        $message_id = sprintf($message_id_template, $uniq, $du_part);
        #print "new message id = $message_id\n"; # Was useful for debugging
  }
@@@ -1727,6 -1728,44 +1729,44 @@@ sub validate_patch 
        return;
  }
  
+ sub handle_backup {
+       my ($last, $lastlen, $file, $known_suffix) = @_;
+       my ($suffix, $skip);
+       $skip = 0;
+       if (defined $last &&
+           ($lastlen < length($file)) &&
+           (substr($file, 0, $lastlen) eq $last) &&
+           ($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
+               if (defined $known_suffix && $suffix eq $known_suffix) {
+                       print "Skipping $file with backup suffix '$known_suffix'.\n";
+                       $skip = 1;
+               } else {
+                       my $answer = ask("Do you really want to send $file? (y|N): ",
+                                        valid_re => qr/^(?:y|n)/i,
+                                        default => 'n');
+                       $skip = ($answer ne 'y');
+                       if ($skip) {
+                               $known_suffix = $suffix;
+                       }
+               }
+       }
+       return ($skip, $known_suffix);
+ }
+ sub handle_backup_files {
+       my @file = @_;
+       my ($last, $lastlen, $known_suffix, $skip, @result);
+       for my $file (@file) {
+               ($skip, $known_suffix) = handle_backup($last, $lastlen,
+                                                      $file, $known_suffix);
+               push @result, $file unless $skip;
+               $last = $file;
+               $lastlen = length($file);
+       }
+       return @result;
+ }
  sub file_has_nonascii {
        my $fn = shift;
        open(my $fh, '<', $fn)