send-email: programmatically generate bash completions

"git send-email --git-completion-helper" only prints "format-patch" flags. Make it print "send-email" flags as well, extracting them programmatically from its three existing "GetOptions". Introduce a "uniq" subroutine, otherwise --cc-cover, --to-cover and other flags would show up twice. In addition, deduplicate flags common to both "send-email" and "format-patch", like --from. Remove extraneous flags: --h and --git-completion-helper. Add trailing "=" to options that expect an argument, inline with the format-patch implementation. 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thiago Perrotta committed Oct 25, 2021 at 17:27 UTC 2b7b75850c6cffba3f33ce99e23bd05f95640e3f
3 files changed +48 -19
contrib/completion/git-completion.bash
+1 -10
@@ -2359,16 +2359,7 @@ _git_send_email ()
2359 return
2360 ;;
2361 --*)
2362 - __gitcomp_builtin send-email "--annotate --bcc --cc --cc-cmd --chain-reply-to
2363 - --compose --confirm= --dry-run --envelope-sender
2364 - --from --identity
2365 - --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2366 - --no-suppress-from --no-thread --quiet --reply-to
2367 - --signed-off-by-cc --smtp-pass --smtp-server
2368 - --smtp-server-port --smtp-encryption= --smtp-user
2369 - --subject --suppress-cc= --suppress-from --thread --to
2370 - --validate --no-validate
2371 - $__git_format_patch_extra_options"
2362 + __gitcomp_builtin send-email "$__git_format_patch_extra_options"
2363 return
2364 ;;
2365 esac
git-send-email.perl
+44 -9
@@ -113,9 +113,38 @@ EOT
113 exit(1);
114 }
115
116 +sub uniq {
117 + my %seen;
118 + grep !$seen{$_}++, @_;
119 +}
120 +
121 sub completion_helper {
117 - print Git::command('format-patch', '--git-completion-helper');
118 - exit(0);
122 + my ($original_opts) = @_;
123 + my %not_for_completion = (
124 + "git-completion-helper" => undef,
125 + "h" => undef,
126 + );
127 + my @send_email_opts = ();
128 +
129 + foreach my $key (keys %$original_opts) {
130 + unless (exists $not_for_completion{$key}) {
131 + $key =~ s/!$//;
132 +
133 + if ($key =~ /[:=][si]$/) {
134 + $key =~ s/[:=][si]$//;
135 + push (@send_email_opts, "--$_=") foreach (split (/\|/, $key));
136 + } else {
137 + push (@send_email_opts, "--$_") foreach (split (/\|/, $key));
138 + }
139 + }
140 + }
141 +
142 + my @format_patch_opts = split(/ /, Git::command('format-patch', '--git-completion-helper'));
143 + my @opts = (@send_email_opts, @format_patch_opts);
144 + @opts = uniq (grep !/^$/, @opts);
145 + # There's an implicit '\n' here already, no need to add an explicit one.
146 + print "@opts";
147 + exit(0);
148 }
149
150 # most mail servers generate the Date: header, but not all...
@@ -425,10 +454,11 @@ my %known_config_keys;
454 my $key = "sendemail.identity";
455 $identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
456 }
428 -my $rc = GetOptions(
457 +my %identity_options = (
458 "identity=s" => \$identity,
459 "no-identity" => \$no_identity,
460 );
461 +my $rc = GetOptions(%identity_options);
462 usage() unless $rc;
463 undef $identity if $no_identity;
464
@@ -444,14 +474,17 @@ undef $identity if $no_identity;
474
475 my $help;
476 my $git_completion_helper;
447 -$rc = GetOptions("h" => \$help,
448 - "dump-aliases" => \$dump_aliases);
477 +my %dump_aliases_options = (
478 + "h" => \$help,
479 + "dump-aliases" => \$dump_aliases,
480 +);
481 +$rc = GetOptions(%dump_aliases_options);
482 usage() unless $rc;
483 die __("--dump-aliases incompatible with other options\n")
484 if !$help and $dump_aliases and @ARGV;
452 -$rc = GetOptions(
485 +my %options = (
486 "sender|from=s" => \$sender,
454 - "in-reply-to=s" => \$initial_in_reply_to,
487 + "in-reply-to=s" => \$initial_in_reply_to,
488 "reply-to=s" => \$reply_to,
489 "subject=s" => \$initial_subject,
490 "to=s" => \@getopt_to,
@@ -508,7 +541,8 @@ $rc = GetOptions(
541 "batch-size=i" => \$batch_size,
542 "relogin-delay=i" => \$relogin_delay,
543 "git-completion-helper" => \$git_completion_helper,
511 - );
544 +);
545 +$rc = GetOptions(%options);
546
547 # Munge any "either config or getopt, not both" variables
548 my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
@@ -516,7 +550,8 @@ my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
550 my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
551
552 usage() if $help;
519 -completion_helper() if $git_completion_helper;
553 +my %all_options = (%options, %dump_aliases_options, %identity_options);
554 +completion_helper(\%all_options) if $git_completion_helper;
555 unless ($rc) {
556 usage();
557 }
t/t9902-completion.sh
+3
@@ -2148,6 +2148,9 @@ test_expect_success PERL 'send-email' '
2148 --cover-from-description=Z
2149 --cover-letter Z
2150 EOF
2151 + test_completion "git send-email --val" <<-\EOF &&
2152 + --validate Z
2153 + EOF
2154 test_completion "git send-email ma" "main "
2155 '
2156