Skip to content

Commit ca2d62b

Browse files
phil-blaingitster
authored andcommitted
parse-options: don't complete option aliases by default
Since 'OPT_ALIAS' was created in 5c38742 (parse-options: don't emit "ambiguous option" for aliases, 2019-04-29), 'git clone --git-completion-helper', which is used by the Bash completion script to list options accepted by clone (via '__gitcomp_builtin'), lists both '--recurse-submodules' and its alias '--recursive', which was not the case before since '--recursive' had the PARSE_OPT_HIDDEN flag set, and options with this flag are skipped by 'parse-options.c::show_gitcomp', which implements 'git <cmd> --git-completion-helper'. This means that typing 'git clone --recurs<TAB>' will yield both '--recurse-submodules' and '--recursive', which is not ideal since both do the same thing, and so the completion should directly complete the canonical option. At the point where 'show_gitcomp' is called in 'parse_options_step', 'preprocess_options' was already called in 'parse_options', so any aliases are now copies of the original options with a modified help text indicating they are aliases. Helpfully, since 64cc539 (parse-options: don't leak alias help messages, 2021-03-21) these copies have the PARSE_OPT_FROM_ALIAS flag set, so check that flag early in 'show_gitcomp' and do not print them, unless the user explicitely requested that *all* completion be shown (by setting 'GIT_COMPLETION_SHOW_ALL'). After all, if we want to encourage the use of '--recurse-submodules' over '--recursive', we'd better just suggest the former. The only other options alias is 'log' and friends' '--mailmap', which is an alias for '--use-mailmap', but the Bash completion helpers for these commands do not use '__gitcomp_builtin', and thus are unnaffected by this change. Test the new behaviour in t9902-completion.sh. As a side effect, this also tests the correct behaviour of GIT_COMPLETION_SHOW_ALL, which was not tested before. Note that since '__gitcomp_builtin' caches the options it shows, we need to re-source the completion script to clear that cache for the second test. Signed-off-by: Philippe Blain <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 75ae10b commit ca2d62b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

parse-options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ static int show_gitcomp(const struct option *opts, int show_all)
585585
if (!opts->long_name)
586586
continue;
587587
if (!show_all &&
588-
(opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
588+
(opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
589589
continue;
590590

591591
switch (opts->type) {

t/t9902-completion.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,19 @@ test_expect_success 'sourcing the completion script clears cached --options' '
24042404
verbose test -z "$__gitcomp_builtin_notes_edit"
24052405
'
24062406

2407+
test_expect_success 'option aliases are not shown by default' '
2408+
test_completion "git clone --recurs" "--recurse-submodules "
2409+
'
2410+
2411+
test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2412+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2413+
GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2414+
test_completion "git clone --recurs" <<-\EOF
2415+
--recurse-submodules Z
2416+
--recursive Z
2417+
EOF
2418+
'
2419+
24072420
test_expect_success '__git_complete' '
24082421
unset -f __git_wrap__git_main &&
24092422

0 commit comments

Comments
 (0)