Skip to content

Commit ae36fe6

Browse files
pcloudsgitster
authored andcommitted
completion: support switch
Completion support for --guess could be made better. If no --detach is given, we should only provide a list of refs/heads/* and dwim ones, not the entire ref space. But I still can't penetrate that __git_refs() function yet. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c479ea3 commit ae36fe6

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

contrib/completion/git-completion.bash

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
# GIT_COMPLETION_CHECKOUT_NO_GUESS
3838
#
3939
# When set to "1", do not include "DWIM" suggestions in git-checkout
40-
# completion (e.g., completing "foo" when "origin/foo" exists).
40+
# and git-switch completion (e.g., completing "foo" when "origin/foo"
41+
# exists).
4142

4243
case "$COMP_WORDBREAKS" in
4344
*:*) : great ;;
@@ -2158,6 +2159,40 @@ _git_status ()
21582159
__git_complete_index_file "$complete_opt"
21592160
}
21602161

2162+
_git_switch ()
2163+
{
2164+
case "$cur" in
2165+
--conflict=*)
2166+
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
2167+
;;
2168+
--*)
2169+
__gitcomp_builtin switch
2170+
;;
2171+
*)
2172+
# check if --track, --no-track, or --no-guess was specified
2173+
# if so, disable DWIM mode
2174+
local track_opt="--track" only_local_ref=n
2175+
if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
2176+
[ -n "$(__git_find_on_cmdline "--track --no-track --no-guess")" ]; then
2177+
track_opt=''
2178+
fi
2179+
# explicit --guess enables DWIM mode regardless of
2180+
# $GIT_COMPLETION_CHECKOUT_NO_GUESS
2181+
if [ -n "$(__git_find_on_cmdline "--guess")" ]; then
2182+
track_opt='--track'
2183+
fi
2184+
if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then
2185+
only_local_ref=y
2186+
fi
2187+
if [ $only_local_ref = y -a -z "$track_opt" ]; then
2188+
__gitcomp_direct "$(__git_heads "" "$cur" " ")"
2189+
else
2190+
__git_complete_refs $track_opt
2191+
fi
2192+
;;
2193+
esac
2194+
}
2195+
21612196
__git_config_get_set_variables ()
21622197
{
21632198
local prevword word config_file= c=$cword

0 commit comments

Comments
 (0)