completion: add a GIT_COMPLETION_SHOW_ALL_COMMANDS

Add a GIT_COMPLETION_SHOW_ALL_COMMANDS=1 configuration setting to go with the existing GIT_COMPLETION_SHOW_ALL=1 added in c099f579b98 (completion: add GIT_COMPLETION_SHOW_ALL env var, 2020-08-19). This will include plumbing commands such as "cat-file" in "git <TAB>" and "git c<TAB>" completion. Without/with this I have 134 and 243 completion with git <TAB>, respectively. It was already possible to do this by tweaking GIT_TESTING_PORCELAIN_COMMAND_LIST= from the outside, that testing variable was added in 84a97131065 (completion: let git provide the completable command list, 2018-05-20). Doing this before loading git-completion.bash worked: export GIT_TESTING_PORCELAIN_COMMAND_LIST="$(git --list-cmds=builtins,main,list-mainporcelain,others,nohelpers,alias,list-complete,config)" But such testing variables are not meant to be used from the outside, and we make no guarantees that those internal won't change. So let's expose this as a dedicated configuration knob. It would be better to teach --list-cmds=* a new category which would include all of these groups, but that's a larger change that we can leave for some other time. 1. https://lore.kernel.org/git/CAGP6POJ9gwp+t-eP3TPkivBLLbNb2+qj=61Mehcj=1BgrVOSLA@mail.gmail.com/ Reported-by: Hongyi Zhao <hongyi.zhao@gmail.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Feb 2, 2022 at 12:15 UTC d9f88dd8bbf5302256ece5e3c50a1d3d59d2cd0e
2 files changed +43 -1
contrib/completion/git-completion.bash
+12 -1
@@ -49,6 +49,11 @@
49 # and git-switch completion (e.g., completing "foo" when "origin/foo"
50 # exists).
51 #
52 +# GIT_COMPLETION_SHOW_ALL_COMMANDS
53 +#
54 +# When set to "1" suggest all commands, including plumbing commands
55 +# which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
56 +#
57 # GIT_COMPLETION_SHOW_ALL
58 #
59 # When set to "1" suggest all options, including options which are
@@ -3455,7 +3460,13 @@ __git_main ()
3460 then
3461 __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3462 else
3458 - __gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3463 + local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3464 +
3465 + if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3466 + then
3467 + list_cmds=builtins,$list_cmds
3468 + fi
3469 + __gitcomp "$(__git --list-cmds=$list_cmds)"
3470 fi
3471 ;;
3472 esac
t/t9902-completion.sh
+31
@@ -2440,6 +2440,37 @@ test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2440 )
2441 '
2442
2443 +test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2444 + (
2445 + . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2446 + sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2447 +
2448 + # Just mainporcelain, not plumbing commands
2449 + run_completion "git c" &&
2450 + grep checkout out &&
2451 + ! grep cat-file out
2452 + )
2453 +'
2454 +
2455 +test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2456 + (
2457 + . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2458 + GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2459 + export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2460 + sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2461 +
2462 + # Both mainporcelain and plumbing commands
2463 + run_completion "git c" &&
2464 + grep checkout out &&
2465 + grep cat-file out &&
2466 +
2467 + # Check "gitk", a "main" command, but not a built-in + more plumbing
2468 + run_completion "git g" &&
2469 + grep gitk out &&
2470 + grep get-tar-commit-id out
2471 + )
2472 +'
2473 +
2474 test_expect_success '__git_complete' '
2475 unset -f __git_wrap__git_main &&
2476