Skip to content

Commit 60e71bb

Browse files
peffgitster
authored andcommitted
completion: optionally disable checkout DWIM
When we complete branch names for "git checkout", we also complete remote branch names that could trigger the DWIM behavior. Depending on your workflow and project, this can be either convenient or annoying. For instance, my clone of gitster.git contains 74 local "jk/*" branches, but origin contains another 147. When I want to checkout a local branch but can't quite remember the name, tab completion shows me 251 entries. And worse, for a topic that has been picked up for pu, the upstream branch name is likely to be similar to mine, leading to a high probability that I pick the wrong one and accidentally create a new branch. This patch adds a way for the user to tell the completion code not to include DWIM suggestions for checkout. This can already be done by typing: git checkout --no-guess jk/<TAB> but that's rather cumbersome. The downside, of course, is that you no longer get completion support when you _do_ want to invoke the DWIM behavior. But depending on your workflow, that may not be a big loss (for instance, in git.git I am much more likely to want to detach, so I'd type "git checkout origin/jk/<TAB>" anyway). Signed-off-by: Jeff King <[email protected]> Reviewed-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a2c2f8 commit 60e71bb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

contrib/completion/git-completion.bash

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
# completion style. For example '!f() { : git commit ; ... }; f' will
2929
# tell the completion to use commit completion. This also works with aliases
3030
# of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
31+
#
32+
# You can set the following environment variables to influence the behavior of
33+
# the completion routines:
34+
#
35+
# GIT_COMPLETION_CHECKOUT_NO_GUESS
36+
#
37+
# When set to "1", do not include "DWIM" suggestions in git-checkout
38+
# completion (e.g., completing "foo" when "origin/foo" exists).
3139

3240
case "$COMP_WORDBREAKS" in
3341
*:*) : great ;;
@@ -1248,7 +1256,8 @@ _git_checkout ()
12481256
# check if --track, --no-track, or --no-guess was specified
12491257
# if so, disable DWIM mode
12501258
local flags="--track --no-track --no-guess" track_opt="--track"
1251-
if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1259+
if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1260+
[ -n "$(__git_find_on_cmdline "$flags")" ]; then
12521261
track_opt=''
12531262
fi
12541263
__git_complete_refs $track_opt

0 commit comments

Comments
 (0)