Skip to content

Commit 18e1ba1

Browse files
committed
Merge branch 'dl/complete-stash-updates'
Further update the command line completion (in contrib/) for "git stash". * dl/complete-stash-updates: git-completion.bash: consolidate cases in _git_stash() git-completion.bash: use $__git_cmd_idx in more places git-completion.bash: rename to $__git_cmd_idx git-completion.bash: separate some commands onto their own line
2 parents 848a17c + 7cdb096 commit 18e1ba1

File tree

2 files changed

+70
-47
lines changed

2 files changed

+70
-47
lines changed

contrib/completion/git-completion.bash

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,8 @@ __git_complete_revlist ()
10061006

10071007
__git_complete_remote_or_refspec ()
10081008
{
1009-
local cur_="$cur" cmd="${words[1]}"
1010-
local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
1009+
local cur_="$cur" cmd="${words[__git_cmd_idx]}"
1010+
local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
10111011
if [ "$cmd" = "remote" ]; then
10121012
((c++))
10131013
fi
@@ -1176,7 +1176,7 @@ __git_aliased_command ()
11761176
# --show-idx: Optionally show the index of the found word in the $words array.
11771177
__git_find_on_cmdline ()
11781178
{
1179-
local word c=1 show_idx
1179+
local word c="$__git_cmd_idx" show_idx
11801180

11811181
while test $# -gt 1; do
11821182
case "$1" in
@@ -1221,7 +1221,7 @@ __git_find_last_on_cmdline ()
12211221
done
12221222
local wordlist="$1"
12231223

1224-
while [ $c -gt 1 ]; do
1224+
while [ $c -gt "$__git_cmd_idx" ]; do
12251225
((c--))
12261226
for word in $wordlist; do
12271227
if [ "$word" = "${words[c]}" ]; then
@@ -1306,7 +1306,7 @@ __git_count_arguments ()
13061306
local word i c=0
13071307

13081308
# Skip "git" (first argument)
1309-
for ((i=1; i < ${#words[@]}; i++)); do
1309+
for ((i="$__git_cmd_idx"; i < ${#words[@]}; i++)); do
13101310
word="${words[i]}"
13111311

13121312
case "$word" in
@@ -1442,7 +1442,7 @@ __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD
14421442

14431443
_git_branch ()
14441444
{
1445-
local i c=1 only_local_ref="n" has_r="n"
1445+
local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
14461446

14471447
while [ $c -lt $cword ]; do
14481448
i="${words[c]}"
@@ -1474,12 +1474,12 @@ _git_branch ()
14741474

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

18961896
case "$cword,$prev" in
1897-
$((__git_subcommand_idx+1)),*|*,-*)
1897+
$((__git_cmd_idx+1)),*|*,-*)
18981898
__git_complete_symbol && return
18991899
;;
19001900
esac
@@ -2474,7 +2474,7 @@ _git_switch ()
24742474
__git_config_get_set_variables ()
24752475
{
24762476
local prevword word config_file= c=$cword
2477-
while [ $c -gt 1 ]; do
2477+
while [ $c -gt "$__git_cmd_idx" ]; do
24782478
word="${words[c]}"
24792479
case "$word" in
24802480
--system|--global|--local|--file=*)
@@ -3017,7 +3017,7 @@ _git_stash ()
30173017
local subcommand="$(__git_find_on_cmdline "$subcommands save")"
30183018

30193019
if [ -z "$subcommand" ]; then
3020-
case "$((cword - __git_subcommand_idx)),$cur" in
3020+
case "$((cword - __git_cmd_idx)),$cur" in
30213021
*,--*)
30223022
__gitcomp_builtin stash_push
30233023
;;
@@ -3032,33 +3032,18 @@ _git_stash ()
30323032
fi
30333033

30343034
case "$subcommand,$cur" in
3035-
push,--*)
3036-
__gitcomp_builtin stash_push
3037-
;;
3038-
save,--*)
3039-
__gitcomp_builtin stash_save
3040-
;;
3041-
pop,--*)
3042-
__gitcomp_builtin stash_pop
3043-
;;
3044-
apply,--*)
3045-
__gitcomp_builtin stash_apply
3046-
;;
3047-
drop,--*)
3048-
__gitcomp_builtin stash_drop
3049-
;;
30503035
list,--*)
30513036
# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
30523037
__gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
30533038
;;
30543039
show,--*)
30553040
__gitcomp_builtin stash_show "$__git_diff_common_options"
30563041
;;
3057-
branch,--*)
3058-
__gitcomp_builtin stash_branch
3042+
*,--*)
3043+
__gitcomp_builtin "stash_$subcommand"
30593044
;;
30603045
branch,*)
3061-
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
3046+
if [ $cword -eq $((__git_cmd_idx+2)) ]; then
30623047
__git_complete_refs
30633048
else
30643049
__gitcomp_nl "$(__git stash list \
@@ -3069,8 +3054,6 @@ _git_stash ()
30693054
__gitcomp_nl "$(__git stash list \
30703055
| sed -n -e 's/:.*//p')"
30713056
;;
3072-
*)
3073-
;;
30743057
esac
30753058
}
30763059

@@ -3224,7 +3207,7 @@ _git_svn ()
32243207

32253208
_git_tag ()
32263209
{
3227-
local i c=1 f=0
3210+
local i c="$__git_cmd_idx" f=0
32283211
while [ $c -lt $cword ]; do
32293212
i="${words[c]}"
32303213
case "$i" in
@@ -3276,9 +3259,11 @@ __git_complete_worktree_paths ()
32763259
_git_worktree ()
32773260
{
32783261
local subcommands="add list lock move prune remove unlock"
3279-
local subcommand
3262+
local subcommand subcommand_idx
32803263

3281-
subcommand="$(__git_find_on_cmdline "$subcommands")"
3264+
subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
3265+
subcommand_idx="${subcommand% *}"
3266+
subcommand="${subcommand#* }"
32823267

32833268
case "$subcommand,$cur" in
32843269
,*)
@@ -3303,7 +3288,7 @@ _git_worktree ()
33033288
# be either the 'add' subcommand, the unstuck
33043289
# argument of an option (e.g. branch for -b|-B), or
33053290
# the path for the new worktree.
3306-
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
3291+
if [ $cword -eq $((subcommand_idx+1)) ]; then
33073292
# Right after the 'add' subcommand: have to
33083293
# complete the path, so fall back to Bash
33093294
# filename completion.
@@ -3327,7 +3312,7 @@ _git_worktree ()
33273312
__git_complete_worktree_paths
33283313
;;
33293314
move,*)
3330-
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
3315+
if [ $cword -eq $((subcommand_idx+1)) ]; then
33313316
# The first parameter must be an existing working
33323317
# tree to be moved.
33333318
__git_complete_worktree_paths
@@ -3395,22 +3380,40 @@ __git_main ()
33953380
{
33963381
local i c=1 command __git_dir __git_repo_path
33973382
local __git_C_args C_args_count=0
3398-
local __git_subcommand_idx
3383+
local __git_cmd_idx
33993384

34003385
while [ $c -lt $cword ]; do
34013386
i="${words[c]}"
34023387
case "$i" in
3403-
--git-dir=*) __git_dir="${i#--git-dir=}" ;;
3404-
--git-dir) ((c++)) ; __git_dir="${words[c]}" ;;
3405-
--bare) __git_dir="." ;;
3406-
--help) command="help"; break ;;
3407-
-c|--work-tree|--namespace) ((c++)) ;;
3408-
-C) __git_C_args[C_args_count++]=-C
3388+
--git-dir=*)
3389+
__git_dir="${i#--git-dir=}"
3390+
;;
3391+
--git-dir)
3392+
((c++))
3393+
__git_dir="${words[c]}"
3394+
;;
3395+
--bare)
3396+
__git_dir="."
3397+
;;
3398+
--help)
3399+
command="help"
3400+
break
3401+
;;
3402+
-c|--work-tree|--namespace)
3403+
((c++))
3404+
;;
3405+
-C)
3406+
__git_C_args[C_args_count++]=-C
34093407
((c++))
34103408
__git_C_args[C_args_count++]="${words[c]}"
34113409
;;
3412-
-*) ;;
3413-
*) command="$i"; __git_subcommand_idx="$c"; break ;;
3410+
-*)
3411+
;;
3412+
*)
3413+
command="$i"
3414+
__git_cmd_idx="$c"
3415+
break
3416+
;;
34143417
esac
34153418
((c++))
34163419
done
@@ -3432,7 +3435,8 @@ __git_main ()
34323435
;;
34333436
esac
34343437
case "$cur" in
3435-
--*) __gitcomp "
3438+
--*)
3439+
__gitcomp "
34363440
--paginate
34373441
--no-pager
34383442
--git-dir=

t/t9902-completion.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,7 @@ test_expect_success '__git_find_on_cmdline - single match' '
18791879
(
18801880
words=(git command --opt list) &&
18811881
cword=${#words[@]} &&
1882+
__git_cmd_idx=1 &&
18821883
__git_find_on_cmdline "add list remove" >actual
18831884
) &&
18841885
test_cmp expect actual
@@ -1889,6 +1890,7 @@ test_expect_success '__git_find_on_cmdline - multiple matches' '
18891890
(
18901891
words=(git command -o --opt remove list add) &&
18911892
cword=${#words[@]} &&
1893+
__git_cmd_idx=1 &&
18921894
__git_find_on_cmdline "add list remove" >actual
18931895
) &&
18941896
test_cmp expect actual
@@ -1898,6 +1900,7 @@ test_expect_success '__git_find_on_cmdline - no match' '
18981900
(
18991901
words=(git command --opt branch) &&
19001902
cword=${#words[@]} &&
1903+
__git_cmd_idx=1 &&
19011904
__git_find_on_cmdline "add list remove" >actual
19021905
) &&
19031906
test_must_be_empty actual
@@ -1908,6 +1911,7 @@ test_expect_success '__git_find_on_cmdline - single match with index' '
19081911
(
19091912
words=(git command --opt list) &&
19101913
cword=${#words[@]} &&
1914+
__git_cmd_idx=1 &&
19111915
__git_find_on_cmdline --show-idx "add list remove" >actual
19121916
) &&
19131917
test_cmp expect actual
@@ -1918,6 +1922,7 @@ test_expect_success '__git_find_on_cmdline - multiple matches with index' '
19181922
(
19191923
words=(git command -o --opt remove list add) &&
19201924
cword=${#words[@]} &&
1925+
__git_cmd_idx=1 &&
19211926
__git_find_on_cmdline --show-idx "add list remove" >actual
19221927
) &&
19231928
test_cmp expect actual
@@ -1927,11 +1932,23 @@ test_expect_success '__git_find_on_cmdline - no match with index' '
19271932
(
19281933
words=(git command --opt branch) &&
19291934
cword=${#words[@]} &&
1935+
__git_cmd_idx=1 &&
19301936
__git_find_on_cmdline --show-idx "add list remove" >actual
19311937
) &&
19321938
test_must_be_empty actual
19331939
'
19341940

1941+
test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
1942+
echo "6 remove" >expect &&
1943+
(
1944+
words=(git -C remove command -o --opt remove list add) &&
1945+
cword=${#words[@]} &&
1946+
__git_cmd_idx=3 &&
1947+
__git_find_on_cmdline --show-idx "add list remove" >actual
1948+
) &&
1949+
test_cmp expect actual
1950+
'
1951+
19351952
test_expect_success '__git_get_config_variables' '
19361953
cat >expect <<-EOF &&
19371954
name-1
@@ -2275,6 +2292,7 @@ do
22752292
(
22762293
words=(git push '$flag' other ma) &&
22772294
cword=${#words[@]} cur=${words[cword-1]} &&
2295+
__git_cmd_idx=1 &&
22782296
__git_complete_remote_or_refspec &&
22792297
print_comp
22802298
) &&
@@ -2288,6 +2306,7 @@ do
22882306
(
22892307
words=(git push other '$flag' ma) &&
22902308
cword=${#words[@]} cur=${words[cword-1]} &&
2309+
__git_cmd_idx=1 &&
22912310
__git_complete_remote_or_refspec &&
22922311
print_comp
22932312
) &&

0 commit comments

Comments
 (0)