send-email: validate & reconfirm interactive responses

People answer 'y' to "Who should the emails appear to be from?" and 'n' to "Message-ID to be used as In-Reply-To for the first email?" for some unknown reason. While it is possible that your local username really is "y" and you are sending the mail to your local colleagues, it is possible, and some might even say it is likely, that it is a user error. Fortunately, our interactive prompter already has input validation mechanism built-in. Enhance it so that we can optionally reconfirm and allow the user to pass an input that does not validate, and "softly" require input to the sender, in-reply-to, and recipient to contain "@" and "." in this order, which would catch most cases of mistakes. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 14, 2012 at 15:15 UTC 51bbccfd1b4a9e2807413022c56ab05c835164fb
1 file changed +13 -3
git-send-email.perl
+13 -3
@@ -681,6 +681,7 @@ sub ask {
681 my ($prompt, %arg) = @_;
682 my $valid_re = $arg{valid_re};
683 my $default = $arg{default};
684 + my $confirm_only = $arg{confirm_only};
685 my $resp;
686 my $i = 0;
687 return defined $default ? $default : undef
@@ -698,6 +699,12 @@ sub ask {
699 if (!defined $valid_re or $resp =~ /$valid_re/) {
700 return $resp;
701 }
702 + if ($confirm_only) {
703 + my $yesno = $term->readline("Are you sure you want to use <$resp> [y/N]? ");
704 + if (defined $yesno && $yesno =~ /y/i) {
705 + return $resp;
706 + }
707 + }
708 }
709 return undef;
710 }
@@ -745,13 +752,15 @@ my $prompting = 0;
752 if (!defined $sender) {
753 $sender = $repoauthor || $repocommitter || '';
754 $sender = ask("Who should the emails appear to be from? [$sender] ",
748 - default => $sender);
755 + default => $sender,
756 + valid_re => qr/\@.*\./, confirm_only => 1);
757 print "Emails will be sent from: ", $sender, "\n";
758 $prompting++;
759 }
760
761 if (!@initial_to && !defined $to_cmd) {
754 - my $to = ask("Who should the emails be sent to? ");
762 + my $to = ask("Who should the emails be sent to? ",
763 + valid_re => qr/\@.*\./, confirm_only => 1);
764 push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
765 $prompting++;
766 }
@@ -777,7 +786,8 @@ sub expand_one_alias {
786
787 if ($thread && !defined $initial_reply_to && $prompting) {
788 $initial_reply_to = ask(
780 - "Message-ID to be used as In-Reply-To for the first email? ");
789 + "Message-ID to be used as In-Reply-To for the first email? ",
790 + valid_re => qr/\@.*\./, confirm_only => 1);
791 }
792 if (defined $initial_reply_to) {
793 $initial_reply_to =~ s/^\s*<?//;