Skip to content

Commit 2b7b758

Browse files
thiagowfxgitster
authored andcommitted
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 <[email protected]> Based-on-patch-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 99c99ed commit 2b7b758

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

contrib/completion/git-completion.bash

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,16 +2359,7 @@ _git_send_email ()
23592359
return
23602360
;;
23612361
--*)
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"
23722363
return
23732364
;;
23742365
esac

git-send-email.perl

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,38 @@ sub usage {
113113
exit(1);
114114
}
115115

116+
sub uniq {
117+
my %seen;
118+
grep !$seen{$_}++, @_;
119+
}
120+
116121
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);
119148
}
120149

121150
# most mail servers generate the Date: header, but not all...
@@ -425,10 +454,11 @@ sub config_regexp {
425454
my $key = "sendemail.identity";
426455
$identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
427456
}
428-
my $rc = GetOptions(
457+
my %identity_options = (
429458
"identity=s" => \$identity,
430459
"no-identity" => \$no_identity,
431460
);
461+
my $rc = GetOptions(%identity_options);
432462
usage() unless $rc;
433463
undef $identity if $no_identity;
434464

@@ -444,14 +474,17 @@ sub config_regexp {
444474

445475
my $help;
446476
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);
449482
usage() unless $rc;
450483
die __("--dump-aliases incompatible with other options\n")
451484
if !$help and $dump_aliases and @ARGV;
452-
$rc = GetOptions(
485+
my %options = (
453486
"sender|from=s" => \$sender,
454-
"in-reply-to=s" => \$initial_in_reply_to,
487+
"in-reply-to=s" => \$initial_in_reply_to,
455488
"reply-to=s" => \$reply_to,
456489
"subject=s" => \$initial_subject,
457490
"to=s" => \@getopt_to,
@@ -508,15 +541,17 @@ sub config_regexp {
508541
"batch-size=i" => \$batch_size,
509542
"relogin-delay=i" => \$relogin_delay,
510543
"git-completion-helper" => \$git_completion_helper,
511-
);
544+
);
545+
$rc = GetOptions(%options);
512546

513547
# Munge any "either config or getopt, not both" variables
514548
my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
515549
my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
516550
my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
517551

518552
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;
520555
unless ($rc) {
521556
usage();
522557
}

t/t9902-completion.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,9 @@ test_expect_success PERL 'send-email' '
21482148
--cover-from-description=Z
21492149
--cover-letter Z
21502150
EOF
2151+
test_completion "git send-email --val" <<-\EOF &&
2152+
--validate Z
2153+
EOF
21512154
test_completion "git send-email ma" "main "
21522155
'
21532156

0 commit comments

Comments
 (0)