diff mbox series

[v7,2/3] send-email: programmatically generate bash completions

Message ID 20211011041033.20004-3-tbperrotta@gmail.com (mailing list archive)
State New, archived
Headers show
Series send-email: shell completion improvements | expand

Commit Message

Thiago Perrotta Oct. 11, 2021, 4:10 a.m. UTC
"git send-email --git-completion-helper" only prints "format-patch"
flags. Make it print "send-email" flags as well, generating them
programmatically from the usage.

Extract flags from the three existing `GetOptions`.

Introduce a uniq subroutine, otherwise --cc-cover, --to-cover and other
flags would show up twice. In addition, deduplicate options common to
both send-email and format-patch, like --from.

Remove extraneous flags: --, --h and --git-completion-helper.

Add a completion test for "send-email --validate", a send-email flag.

Signed-off-by: Thiago Perrotta <tbperrotta@gmail.com>
Based-on-patch-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 contrib/completion/git-completion.bash |  11 +-
 git-send-email.perl                    | 153 ++++++++++++++-----------
 t/t9902-completion.sh                  |   3 +
 3 files changed, 91 insertions(+), 76 deletions(-)
diff mbox series

Patch

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 4bdd27ddc8..1b73a4dcc0 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2359,16 +2359,7 @@  _git_send_email ()
 		return
 		;;
 	--*)
-		__gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
-			--compose --confirm= --dry-run --envelope-sender
-			--from --identity
-			--in-reply-to --no-chain-reply-to --no-signed-off-by-cc
-			--no-suppress-from --no-thread --quiet --reply-to
-			--signed-off-by-cc --smtp-pass --smtp-server
-			--smtp-server-port --smtp-encryption= --smtp-user
-			--subject --suppress-cc= --suppress-from --thread --to
-			--validate --no-validate
-			$__git_format_patch_extra_options"
+		__gitcomp_builtin send-email "$__git_format_patch_extra_options"
 		return
 		;;
 	esac
diff --git a/git-send-email.perl b/git-send-email.perl
index d1731c1755..587e52d1d8 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -113,9 +113,24 @@  sub usage {
 	exit(1);
 }
 
+sub uniq {
+	my %seen;
+	grep !$seen{$_}++, @_;
+}
+
 sub completion_helper {
-    print Git::command('format-patch', '--git-completion-helper'), "\n";
-    exit(0);
+	my ($original_opts) = @_;
+	my @send_email_opts = map {
+		"--$_"
+	} map {
+		s/(?:[:=][si]|!)$//;
+		split /\|/;
+	} keys %$original_opts;
+	my @format_patch_opts = split(/ /, Git::command('format-patch', '--git-completion-helper'));
+	my @options = (@send_email_opts, @format_patch_opts);
+	@options = uniq (grep !/^$|^--$|--git-completion-helper|--h/, @options);
+	print "@options\n";
+	exit(0);
 }
 
 # most mail servers generate the Date: header, but not all...
@@ -425,10 +440,11 @@  sub config_regexp {
 	my $key = "sendemail.identity";
 	$identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
 }
-my $rc = GetOptions(
+my %identity_options = (
 	"identity=s" => \$identity,
 	"no-identity" => \$no_identity,
 );
+my $rc = GetOptions(%identity_options);
 usage() unless $rc;
 undef $identity if $no_identity;
 
@@ -444,71 +460,75 @@  sub config_regexp {
 
 my $help;
 my $git_completion_helper;
-$rc = GetOptions("h" => \$help,
-                 "dump-aliases" => \$dump_aliases);
+my %dump_aliases_options = (
+	"h" => \$help,
+	"dump-aliases" => \$dump_aliases,
+);
+$rc = GetOptions(%dump_aliases_options);
 usage() unless $rc;
 die __("--dump-aliases incompatible with other options\n")
     if !$help and $dump_aliases and @ARGV;
-$rc = GetOptions(
-		    "sender|from=s" => \$sender,
-                    "in-reply-to=s" => \$initial_in_reply_to,
-		    "reply-to=s" => \$reply_to,
-		    "subject=s" => \$initial_subject,
-		    "to=s" => \@getopt_to,
-		    "to-cmd=s" => \$to_cmd,
-		    "no-to" => \$no_to,
-		    "cc=s" => \@getopt_cc,
-		    "no-cc" => \$no_cc,
-		    "bcc=s" => \@getopt_bcc,
-		    "no-bcc" => \$no_bcc,
-		    "chain-reply-to!" => \$chain_reply_to,
-		    "no-chain-reply-to" => sub {$chain_reply_to = 0},
-		    "sendmail-cmd=s" => \$sendmail_cmd,
-		    "smtp-server=s" => \$smtp_server,
-		    "smtp-server-option=s" => \@smtp_server_options,
-		    "smtp-server-port=s" => \$smtp_server_port,
-		    "smtp-user=s" => \$smtp_authuser,
-		    "smtp-pass:s" => \$smtp_authpass,
-		    "smtp-ssl" => sub { $smtp_encryption = 'ssl' },
-		    "smtp-encryption=s" => \$smtp_encryption,
-		    "smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
-		    "smtp-debug:i" => \$debug_net_smtp,
-		    "smtp-domain:s" => \$smtp_domain,
-		    "smtp-auth=s" => \$smtp_auth,
-		    "no-smtp-auth" => sub {$smtp_auth = 'none'},
-		    "annotate!" => \$annotate,
-		    "no-annotate" => sub {$annotate = 0},
-		    "compose" => \$compose,
-		    "quiet" => \$quiet,
-		    "cc-cmd=s" => \$cc_cmd,
-		    "suppress-from!" => \$suppress_from,
-		    "no-suppress-from" => sub {$suppress_from = 0},
-		    "suppress-cc=s" => \@suppress_cc,
-		    "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
-		    "no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
-		    "cc-cover|cc-cover!" => \$cover_cc,
-		    "no-cc-cover" => sub {$cover_cc = 0},
-		    "to-cover|to-cover!" => \$cover_to,
-		    "no-to-cover" => sub {$cover_to = 0},
-		    "confirm=s" => \$confirm,
-		    "dry-run" => \$dry_run,
-		    "envelope-sender=s" => \$envelope_sender,
-		    "thread!" => \$thread,
-		    "no-thread" => sub {$thread = 0},
-		    "validate!" => \$validate,
-		    "no-validate" => sub {$validate = 0},
-		    "transfer-encoding=s" => \$target_xfer_encoding,
-		    "format-patch!" => \$format_patch,
-		    "no-format-patch" => sub {$format_patch = 0},
-		    "8bit-encoding=s" => \$auto_8bit_encoding,
-		    "compose-encoding=s" => \$compose_encoding,
-		    "force" => \$force,
-		    "xmailer!" => \$use_xmailer,
-		    "no-xmailer" => sub {$use_xmailer = 0},
-		    "batch-size=i" => \$batch_size,
-		    "relogin-delay=i" => \$relogin_delay,
-		    "git-completion-helper" => \$git_completion_helper,
-	 );
+my %options = (
+	"sender|from=s" => \$sender,
+	"in-reply-to=s" => \$initial_in_reply_to,
+	"reply-to=s" => \$reply_to,
+	"subject=s" => \$initial_subject,
+	"to=s" => \@getopt_to,
+	"to-cmd=s" => \$to_cmd,
+	"no-to" => \$no_to,
+	"cc=s" => \@getopt_cc,
+	"no-cc" => \$no_cc,
+	"bcc=s" => \@getopt_bcc,
+	"no-bcc" => \$no_bcc,
+	"chain-reply-to!" => \$chain_reply_to,
+	"no-chain-reply-to" => sub {$chain_reply_to = 0},
+	"sendmail-cmd=s" => \$sendmail_cmd,
+	"smtp-server=s" => \$smtp_server,
+	"smtp-server-option=s" => \@smtp_server_options,
+	"smtp-server-port=s" => \$smtp_server_port,
+	"smtp-user=s" => \$smtp_authuser,
+	"smtp-pass:s" => \$smtp_authpass,
+	"smtp-ssl" => sub { $smtp_encryption = 'ssl' },
+	"smtp-encryption=s" => \$smtp_encryption,
+	"smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
+	"smtp-debug:i" => \$debug_net_smtp,
+	"smtp-domain:s" => \$smtp_domain,
+	"smtp-auth=s" => \$smtp_auth,
+	"no-smtp-auth" => sub {$smtp_auth = 'none'},
+	"annotate!" => \$annotate,
+	"no-annotate" => sub {$annotate = 0},
+	"compose" => \$compose,
+	"quiet" => \$quiet,
+	"cc-cmd=s" => \$cc_cmd,
+	"suppress-from!" => \$suppress_from,
+	"no-suppress-from" => sub {$suppress_from = 0},
+	"suppress-cc=s" => \@suppress_cc,
+	"signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
+	"no-signed-off-cc|no-signed-off-by-cc" => sub {$signed_off_by_cc = 0},
+	"cc-cover|cc-cover!" => \$cover_cc,
+	"no-cc-cover" => sub {$cover_cc = 0},
+	"to-cover|to-cover!" => \$cover_to,
+	"no-to-cover" => sub {$cover_to = 0},
+	"confirm=s" => \$confirm,
+	"dry-run" => \$dry_run,
+	"envelope-sender=s" => \$envelope_sender,
+	"thread!" => \$thread,
+	"no-thread" => sub {$thread = 0},
+	"validate!" => \$validate,
+	"no-validate" => sub {$validate = 0},
+	"transfer-encoding=s" => \$target_xfer_encoding,
+	"format-patch!" => \$format_patch,
+	"no-format-patch" => sub {$format_patch = 0},
+	"8bit-encoding=s" => \$auto_8bit_encoding,
+	"compose-encoding=s" => \$compose_encoding,
+	"force" => \$force,
+	"xmailer!" => \$use_xmailer,
+	"no-xmailer" => sub {$use_xmailer = 0},
+	"batch-size=i" => \$batch_size,
+	"relogin-delay=i" => \$relogin_delay,
+	"git-completion-helper" => \$git_completion_helper,
+);
+$rc = GetOptions(%options);
 
 # Munge any "either config or getopt, not both" variables
 my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
@@ -516,7 +536,8 @@  sub config_regexp {
 my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
 
 usage() if $help;
-completion_helper() if $git_completion_helper;
+my %all_options = (%options, %dump_aliases_options, %identity_options);
+completion_helper(\%all_options) if $git_completion_helper;
 unless ($rc) {
     usage();
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 11573936d5..a4faf64184 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2139,6 +2139,9 @@  test_expect_success PERL 'send-email' '
 	--cover-from-description=Z
 	--cover-letter Z
 	EOF
+	test_completion "git send-email --val" <<-\EOF &&
+	--validate Z
+	EOF
 	test_completion "git send-email ma" "main "
 '