Skip to content

Commit bd3ae9f

Browse files
committed
Merge branch 'rz/complete-more-options'
Command line completion (in contrib/) usually omits redundant, deprecated and/or dangerous options from its output; it learned to optionally include all of them. * rz/complete-more-options: completion: add GIT_COMPLETION_SHOW_ALL env var parse-options: add --git-completion-helper-all
2 parents 0d9a8e3 + c099f57 commit bd3ae9f

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

contrib/completion/git-completion.bash

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
# When set to "1", do not include "DWIM" suggestions in git-checkout
4040
# and git-switch completion (e.g., completing "foo" when "origin/foo"
4141
# exists).
42+
#
43+
# GIT_COMPLETION_SHOW_ALL
44+
#
45+
# When set to "1" suggest all options, including options which are
46+
# typically hidden (e.g. '--allow-empty' for 'git commit').
4247

4348
case "$COMP_WORDBREAKS" in
4449
*:*) : great ;;
@@ -411,10 +416,17 @@ __gitcomp_builtin ()
411416
local options
412417
eval "options=\${$var-}"
413418

419+
local completion_helper
420+
if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
421+
completion_helper="--git-completion-helper-all"
422+
else
423+
completion_helper="--git-completion-helper"
424+
fi
425+
414426
if [ -z "$options" ]; then
415427
# leading and trailing spaces are significant to make
416428
# option removal work correctly.
417-
options=" $incl $(__git ${cmd/_/ } --git-completion-helper) " || return
429+
options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
418430

419431
for i in $excl; do
420432
options="${options/ $i / }"

parse-options.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
525525
parse_options_start_1(ctx, argc, argv, prefix, options, flags);
526526
}
527527

528-
static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
528+
static void show_negated_gitcomp(const struct option *opts, int show_all,
529+
int nr_noopts)
529530
{
530531
int printed_dashdash = 0;
531532

@@ -535,7 +536,8 @@ static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
535536

536537
if (!opts->long_name)
537538
continue;
538-
if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
539+
if (!show_all &&
540+
(opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
539541
continue;
540542
if (opts->flags & PARSE_OPT_NONEG)
541543
continue;
@@ -572,7 +574,7 @@ static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
572574
}
573575
}
574576

575-
static int show_gitcomp(const struct option *opts)
577+
static int show_gitcomp(const struct option *opts, int show_all)
576578
{
577579
const struct option *original_opts = opts;
578580
int nr_noopts = 0;
@@ -582,7 +584,8 @@ static int show_gitcomp(const struct option *opts)
582584

583585
if (!opts->long_name)
584586
continue;
585-
if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
587+
if (!show_all &&
588+
(opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
586589
continue;
587590

588591
switch (opts->type) {
@@ -610,8 +613,8 @@ static int show_gitcomp(const struct option *opts)
610613
nr_noopts++;
611614
printf(" --%s%s", opts->long_name, suffix);
612615
}
613-
show_negated_gitcomp(original_opts, -1);
614-
show_negated_gitcomp(original_opts, nr_noopts);
616+
show_negated_gitcomp(original_opts, show_all, -1);
617+
show_negated_gitcomp(original_opts, show_all, nr_noopts);
615618
fputc('\n', stdout);
616619
return PARSE_OPT_COMPLETE;
617620
}
@@ -723,9 +726,14 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
723726
if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
724727
goto show_usage;
725728

726-
/* lone --git-completion-helper is asked by git-completion.bash */
727-
if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
728-
return show_gitcomp(options);
729+
/*
730+
* lone --git-completion-helper and --git-completion-helper-all
731+
* are asked by git-completion.bash
732+
*/
733+
if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
734+
return show_gitcomp(options, 0);
735+
if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
736+
return show_gitcomp(options, 1);
729737

730738
if (arg[1] != '-') {
731739
ctx->opt = arg + 1;

0 commit comments

Comments
 (0)