Skip to content

Commit 037dbe8

Browse files
committed
Merge branch 'ab/complete-show-all-commands'
The command line completion script (in contrib/) learned to complete all Git subcommands, including the ones that are normally hidden, when GIT_COMPLETION_SHOW_ALL_COMMANDS is used. * ab/complete-show-all-commands: completion: add a GIT_COMPLETION_SHOW_ALL_COMMANDS completion tests: re-source git-completion.bash in a subshell
2 parents 6cfe518 + d9f88dd commit 037dbe8

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

contrib/completion/git-completion.bash

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
# and git-switch completion (e.g., completing "foo" when "origin/foo"
5050
# exists).
5151
#
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+
#
5257
# GIT_COMPLETION_SHOW_ALL
5358
#
5459
# When set to "1" suggest all options, including options which are
@@ -3483,7 +3488,13 @@ __git_main ()
34833488
then
34843489
__gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
34853490
else
3486-
__gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3491+
local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
3492+
3493+
if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
3494+
then
3495+
list_cmds=builtins,$list_cmds
3496+
fi
3497+
__gitcomp "$(__git --list-cmds=$list_cmds)"
34873498
fi
34883499
;;
34893500
esac

t/t9902-completion.sh

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,40 +2534,79 @@ test_expect_success 'options with value' '
25342534
'
25352535

25362536
test_expect_success 'sourcing the completion script clears cached commands' '
2537-
__git_compute_all_commands &&
2538-
verbose test -n "$__git_all_commands" &&
2539-
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2540-
verbose test -z "$__git_all_commands"
2537+
(
2538+
__git_compute_all_commands &&
2539+
verbose test -n "$__git_all_commands" &&
2540+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2541+
verbose test -z "$__git_all_commands"
2542+
)
25412543
'
25422544

25432545
test_expect_success 'sourcing the completion script clears cached merge strategies' '
2544-
__git_compute_merge_strategies &&
2545-
verbose test -n "$__git_merge_strategies" &&
2546-
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2547-
verbose test -z "$__git_merge_strategies"
2546+
(
2547+
__git_compute_merge_strategies &&
2548+
verbose test -n "$__git_merge_strategies" &&
2549+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2550+
verbose test -z "$__git_merge_strategies"
2551+
)
25482552
'
25492553

25502554
test_expect_success 'sourcing the completion script clears cached --options' '
2551-
__gitcomp_builtin checkout &&
2552-
verbose test -n "$__gitcomp_builtin_checkout" &&
2553-
__gitcomp_builtin notes_edit &&
2554-
verbose test -n "$__gitcomp_builtin_notes_edit" &&
2555-
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2556-
verbose test -z "$__gitcomp_builtin_checkout" &&
2557-
verbose test -z "$__gitcomp_builtin_notes_edit"
2555+
(
2556+
__gitcomp_builtin checkout &&
2557+
verbose test -n "$__gitcomp_builtin_checkout" &&
2558+
__gitcomp_builtin notes_edit &&
2559+
verbose test -n "$__gitcomp_builtin_notes_edit" &&
2560+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2561+
verbose test -z "$__gitcomp_builtin_checkout" &&
2562+
verbose test -z "$__gitcomp_builtin_notes_edit"
2563+
)
25582564
'
25592565

25602566
test_expect_success 'option aliases are not shown by default' '
25612567
test_completion "git clone --recurs" "--recurse-submodules "
25622568
'
25632569

25642570
test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2565-
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2566-
GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2567-
test_completion "git clone --recurs" <<-\EOF
2568-
--recurse-submodules Z
2569-
--recursive Z
2570-
EOF
2571+
(
2572+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2573+
GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2574+
test_completion "git clone --recurs" <<-\EOF
2575+
--recurse-submodules Z
2576+
--recursive Z
2577+
EOF
2578+
)
2579+
'
2580+
2581+
test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2582+
(
2583+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2584+
sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2585+
2586+
# Just mainporcelain, not plumbing commands
2587+
run_completion "git c" &&
2588+
grep checkout out &&
2589+
! grep cat-file out
2590+
)
2591+
'
2592+
2593+
test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2594+
(
2595+
. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2596+
GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2597+
export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2598+
sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2599+
2600+
# Both mainporcelain and plumbing commands
2601+
run_completion "git c" &&
2602+
grep checkout out &&
2603+
grep cat-file out &&
2604+
2605+
# Check "gitk", a "main" command, but not a built-in + more plumbing
2606+
run_completion "git g" &&
2607+
grep gitk out &&
2608+
grep get-tar-commit-id out
2609+
)
25712610
'
25722611

25732612
test_expect_success '__git_complete' '

0 commit comments

Comments
 (0)