summaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorJacob Keller <jacob.keller@gmail.com>2020-05-28 20:10:47 +0200
committerJunio C Hamano <gitster@pobox.com>2020-05-28 21:57:07 +0200
commitacb658fe7d6bc11136be0756270fda675af74761 (patch)
treee8e5cd9c27796e74b5c83b60d24d6d4ff63021dd /contrib
parentcompletion: improve handling of --track in switch/checkout (diff)
downloadgit-acb658fe7d6bc11136be0756270fda675af74761.tar.xz
git-acb658fe7d6bc11136be0756270fda675af74761.zip
completion: improve handling of -c/-C and -b/-B in switch/checkout
A previous commit added several test cases highlighting the subpar completion logic for -c/-C and -b/-B when completing git switch and git checkout. In order to distinguish completing the argument vs the start-point for this option, we now use the wordlist to determine the previous full word on the command line. If it's -c or -C (-b/-B for checkout), then we know that we are completing the argument for the branch name. Given that a user who already knows the branch name they want to complete will simply not use completion, it makes sense to complete the small subset of local branches when completing the argument for -c/-C. In all other cases, if -c/-C are on the command line but are not the most recent option, then we must be completing a start-point, and should allow completing against all references. Update the -c/-C and -b/-B tests to indicate they now pass. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'contrib')
-rw-r--r--contrib/completion/git-completion.bash49
1 files changed, 47 insertions, 2 deletions
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index e520dda35e..9dd0412ecc 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1505,8 +1505,31 @@ _git_checkout ()
;;
*)
local dwim_opt="$(__git_checkout_default_dwim_mode)"
+ local prevword prevword="${words[cword-1]}"
+
+ case "$prevword" in
+ -b|-B)
+ # Complete local branches (and DWIM branch
+ # remote branch names) for an option argument
+ # specifying a new branch name. This is for
+ # convenience, assuming new branches are
+ # possibly based on pre-existing branch names.
+ __git_complete_refs $dwim_opt --mode="heads"
+ return
+ ;;
+ *)
+ ;;
+ esac
+
+ # At this point, we've already handled special completion for
+ # the arguments to -b/-B. There are 3 main things left we can
+ # possibly complete:
+ # 1) a start-point for -b/-B or -d/--detach
+ # 2) a remote head, for --track
+ # 3) an arbitrary reference, possibly including DWIM names
+ #
- if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
+ if [ -n "$(__git_find_on_cmdline "-b -B -d --detach")" ]; then
__git_complete_refs --mode="refs"
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
__git_complete_refs --mode="remote-heads"
@@ -2361,8 +2384,30 @@ _git_switch ()
;;
*)
local dwim_opt="$(__git_checkout_default_dwim_mode)"
+ local prevword prevword="${words[cword-1]}"
+
+ case "$prevword" in
+ -c|-C)
+ # Complete local branches (and DWIM branch
+ # remote branch names) for an option argument
+ # specifying a new branch name. This is for
+ # convenience, assuming new branches are
+ # possibly based on pre-existing branch names.
+ __git_complete_refs $dwim_opt --mode="heads"
+ return
+ ;;
+ *)
+ ;;
+ esac
+
+ # At this point, we've already handled special completion for
+ # -c/-C. There are 3 main things left to
+ # complete:
+ # 1) a start-point for -c/-C or -d/--detach
+ # 2) a remote head, for --track
+ # 3) a branch name, possibly including DWIM remote branches
- if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
+ if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
__git_complete_refs --mode="refs"
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
__git_complete_refs --mode="remote-heads"