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
This commit is contained in:
Junio C Hamano 2020-08-27 14:04:49 -07:00
commit bd3ae9fb7d
2 changed files with 30 additions and 10 deletions

View File

@ -39,6 +39,11 @@
# When set to "1", do not include "DWIM" suggestions in git-checkout # When set to "1", do not include "DWIM" suggestions in git-checkout
# and git-switch completion (e.g., completing "foo" when "origin/foo" # and git-switch completion (e.g., completing "foo" when "origin/foo"
# exists). # exists).
#
# GIT_COMPLETION_SHOW_ALL
#
# When set to "1" suggest all options, including options which are
# typically hidden (e.g. '--allow-empty' for 'git commit').
case "$COMP_WORDBREAKS" in case "$COMP_WORDBREAKS" in
*:*) : great ;; *:*) : great ;;
@ -411,10 +416,17 @@ __gitcomp_builtin ()
local options local options
eval "options=\${$var-}" eval "options=\${$var-}"
local completion_helper
if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
completion_helper="--git-completion-helper-all"
else
completion_helper="--git-completion-helper"
fi
if [ -z "$options" ]; then if [ -z "$options" ]; then
# leading and trailing spaces are significant to make # leading and trailing spaces are significant to make
# option removal work correctly. # option removal work correctly.
options=" $incl $(__git ${cmd/_/ } --git-completion-helper) " || return options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
for i in $excl; do for i in $excl; do
options="${options/ $i / }" options="${options/ $i / }"

View File

@ -525,7 +525,8 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
parse_options_start_1(ctx, argc, argv, prefix, options, flags); parse_options_start_1(ctx, argc, argv, prefix, options, flags);
} }
static void show_negated_gitcomp(const struct option *opts, int nr_noopts) static void show_negated_gitcomp(const struct option *opts, int show_all,
int nr_noopts)
{ {
int printed_dashdash = 0; int printed_dashdash = 0;
@ -535,7 +536,8 @@ static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
if (!opts->long_name) if (!opts->long_name)
continue; continue;
if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)) if (!show_all &&
(opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
continue; continue;
if (opts->flags & PARSE_OPT_NONEG) if (opts->flags & PARSE_OPT_NONEG)
continue; continue;
@ -572,7 +574,7 @@ static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
} }
} }
static int show_gitcomp(const struct option *opts) static int show_gitcomp(const struct option *opts, int show_all)
{ {
const struct option *original_opts = opts; const struct option *original_opts = opts;
int nr_noopts = 0; int nr_noopts = 0;
@ -582,7 +584,8 @@ static int show_gitcomp(const struct option *opts)
if (!opts->long_name) if (!opts->long_name)
continue; continue;
if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)) if (!show_all &&
(opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
continue; continue;
switch (opts->type) { switch (opts->type) {
@ -610,8 +613,8 @@ static int show_gitcomp(const struct option *opts)
nr_noopts++; nr_noopts++;
printf(" --%s%s", opts->long_name, suffix); printf(" --%s%s", opts->long_name, suffix);
} }
show_negated_gitcomp(original_opts, -1); show_negated_gitcomp(original_opts, show_all, -1);
show_negated_gitcomp(original_opts, nr_noopts); show_negated_gitcomp(original_opts, show_all, nr_noopts);
fputc('\n', stdout); fputc('\n', stdout);
return PARSE_OPT_COMPLETE; return PARSE_OPT_COMPLETE;
} }
@ -723,9 +726,14 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h")) if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
goto show_usage; goto show_usage;
/* lone --git-completion-helper is asked by git-completion.bash */ /*
if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper")) * lone --git-completion-helper and --git-completion-helper-all
return show_gitcomp(options); * are asked by git-completion.bash
*/
if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
return show_gitcomp(options, 0);
if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
return show_gitcomp(options, 1);
if (arg[1] != '-') { if (arg[1] != '-') {
ctx->opt = arg + 1; ctx->opt = arg + 1;