Skip to content

Commit e94fb44

Browse files
Denton-Lgitster
authored andcommitted
git-completion.bash: pass $__git_subcommand_idx from __git_main()
Many completion functions perform hardcoded comparisons with $cword. This fails in the case where the main git command is given arguments (e.g. `git -C . bundle<TAB>` would fail to complete its subcommands). Even _git_worktree(), which uses __git_find_on_cmdline(), could still fail. With something like `git -C add worktree move<TAB>`, the subcommand would be incorrectly identified as "add" instead of "move". Assign $__git_subcommand_idx in __git_main(), where the git subcommand is actually found and the corresponding completion function is called. Use this variable to replace hardcoded comparisons with $cword. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5828ae commit e94fb44

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

contrib/completion/git-completion.bash

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,12 +1474,12 @@ _git_branch ()
14741474

14751475
_git_bundle ()
14761476
{
1477-
local cmd="${words[2]}"
1477+
local cmd="${words[__git_subcommand_idx+1]}"
14781478
case "$cword" in
1479-
2)
1479+
$((__git_subcommand_idx+1)))
14801480
__gitcomp "create list-heads verify unbundle"
14811481
;;
1482-
3)
1482+
$((__git_subcommand_idx+2)))
14831483
# looking for a file
14841484
;;
14851485
*)
@@ -1894,7 +1894,7 @@ _git_grep ()
18941894
esac
18951895

18961896
case "$cword,$prev" in
1897-
2,*|*,-*)
1897+
$((__git_subcommand_idx+1)),*|*,-*)
18981898
__git_complete_symbol && return
18991899
;;
19001900
esac
@@ -3058,7 +3058,7 @@ _git_stash ()
30583058
branch,--*)
30593059
;;
30603060
branch,*)
3061-
if [ $cword -eq 3 ]; then
3061+
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
30623062
__git_complete_refs
30633063
else
30643064
__gitcomp_nl "$(__git stash list \
@@ -3277,11 +3277,9 @@ __git_complete_worktree_paths ()
32773277
_git_worktree ()
32783278
{
32793279
local subcommands="add list lock move prune remove unlock"
3280-
local subcommand subcommand_idx
3280+
local subcommand
32813281

3282-
subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3283-
subcommand_idx="${subcommand% *}"
3284-
subcommand="${subcommand#* }"
3282+
subcommand="$(__git_find_on_cmdline "$subcommands")"
32853283

32863284
case "$subcommand,$cur" in
32873285
,*)
@@ -3306,7 +3304,7 @@ _git_worktree ()
33063304
# be either the 'add' subcommand, the unstuck
33073305
# argument of an option (e.g. branch for -b|-B), or
33083306
# the path for the new worktree.
3309-
if [ $cword -eq $((subcommand_idx+1)) ]; then
3307+
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
33103308
# Right after the 'add' subcommand: have to
33113309
# complete the path, so fall back to Bash
33123310
# filename completion.
@@ -3330,7 +3328,7 @@ _git_worktree ()
33303328
__git_complete_worktree_paths
33313329
;;
33323330
move,*)
3333-
if [ $cword -eq $((subcommand_idx+1)) ]; then
3331+
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
33343332
# The first parameter must be an existing working
33353333
# tree to be moved.
33363334
__git_complete_worktree_paths
@@ -3398,6 +3396,7 @@ __git_main ()
33983396
{
33993397
local i c=1 command __git_dir __git_repo_path
34003398
local __git_C_args C_args_count=0
3399+
local __git_subcommand_idx
34013400

34023401
while [ $c -lt $cword ]; do
34033402
i="${words[c]}"
@@ -3412,7 +3411,7 @@ __git_main ()
34123411
__git_C_args[C_args_count++]="${words[c]}"
34133412
;;
34143413
-*) ;;
3415-
*) command="$i"; break ;;
3414+
*) command="$i"; __git_subcommand_idx="$c"; break ;;
34163415
esac
34173416
((c++))
34183417
done

0 commit comments

Comments
 (0)