{"thread":{"id":"55527","subject":"[PATCH 0/5] git-completion.bash: fixes on top of 'dl/complete-stash'","startedAt":"2021-04-20T09:20:03Z","lastAt":"2021-04-22T10:01:15Z","messageCount":18,"participants":["Denton Liu","Ævar Arnfjörð Bjarmason","Junio C Hamano","SZEDER Gábor"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"422372","messageId":"cover.1618910364.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":null,"subject":"[PATCH 0/5] git-completion.bash: fixes on top of 'dl/complete-stash'","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-20T09:19:46Z","receivedAt":"2021-04-20T09:20:03Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"Gábor pointed out some potential fixes what's currently queued in\n'dl/complete-stash'[0][1][2][3]. This series addresses those concerns.\n\nSorry for the long wait, I've been quite busy over the past month.\n\n[0]: https://lore.kernel.org/git/20210327183554.GD2271@szeder.dev/\n[1]: https://lore.kernel.org/git/20210328103134.GF2271@szeder.dev/\n[2]: https://lore.kernel.org/git/20210328103057.GE2271@szeder.dev/\n[3]: https://lore.kernel.org/git/20210328110427.GG2271@szeder.dev/\n\nDenton Liu (5):\n  git-completion.bash: separate some commands onto their own line\n  git-completion.bash: rename to $__git_cmd_idx\n  git-completion.bash: use $__git_cmd_idx in more places\n  git-completion.bash: consolidate cases in _git_stash()\n  git-completion.bash: consolidate no-subcommand case for _git_stash()\n\n contrib/completion/git-completion.bash | 118 +++++++++++++------------\n t/t9902-completion.sh                  |  19 ++++\n 2 files changed, 80 insertions(+), 57 deletions(-)\n\n-- \n2.31.1.424.g95a8dafae5\n\n"},{"id":"422373","messageId":"65c485ea0c1726c96d292d7d7385def8d2f19f25.1618910364.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1618910364.git.liu.denton@gmail.com","subject":"[PATCH 1/5] git-completion.bash: separate some commands onto their own line","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-20T09:19:47Z","receivedAt":"2021-04-20T09:20:07Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n__git_main(), 2021-03-24), a line was introduced which contained\nmultiple statements. This is difficult to read so break it into multiple\nlines.\n\nWhile we're at it, follow this convention for the rest of the\n__git_main() and break up lines that contain multiple statements.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 37 +++++++++++++++++++-------\n 1 file changed, 28 insertions(+), 9 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c926ca26c6..1dedb14b47 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3400,17 +3400,35 @@ __git_main ()\n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n \t\tcase \"$i\" in\n-\t\t--git-dir=*) __git_dir=\"${i#--git-dir=}\" ;;\n-\t\t--git-dir)   ((c++)) ; __git_dir=\"${words[c]}\" ;;\n-\t\t--bare)      __git_dir=\".\" ;;\n-\t\t--help) command=\"help\"; break ;;\n-\t\t-c|--work-tree|--namespace) ((c++)) ;;\n-\t\t-C)\t__git_C_args[C_args_count++]=-C\n+\t\t--git-dir=*)\n+\t\t\t__git_dir=\"${i#--git-dir=}\"\n+\t\t\t;;\n+\t\t--git-dir)\n+\t\t\t((c++))\n+\t\t\t__git_dir=\"${words[c]}\"\n+\t\t\t;;\n+\t\t--bare)\n+\t\t\t__git_dir=\".\"\n+\t\t\t;;\n+\t\t--help)\n+\t\t\tcommand=\"help\"\n+\t\t\tbreak\n+\t\t\t;;\n+\t\t-c|--work-tree|--namespace)\n+\t\t\t((c++))\n+\t\t\t;;\n+\t\t-C)\n+\t\t\t__git_C_args[C_args_count++]=-C\n \t\t\t((c++))\n \t\t\t__git_C_args[C_args_count++]=\"${words[c]}\"\n \t\t\t;;\n-\t\t-*) ;;\n-\t\t*) command=\"$i\"; __git_subcommand_idx=\"$c\"; break ;;\n+\t\t-*)\n+\t\t\t;;\n+\t\t*)\n+\t\t\tcommand=\"$i\"\n+\t\t\t__git_subcommand_idx=\"$c\"\n+\t\t\tbreak\n+\t\t\t;;\n \t\tesac\n \t\t((c++))\n \tdone\n@@ -3432,7 +3450,8 @@ __git_main ()\n \t\t\t;;\n \t\tesac\n \t\tcase \"$cur\" in\n-\t\t--*)   __gitcomp \"\n+\t\t--*)\n+\t\t\t__gitcomp \"\n \t\t\t--paginate\n \t\t\t--no-pager\n \t\t\t--git-dir=\n-- \n2.31.1.424.g95a8dafae5\n\n"},{"id":"422374","messageId":"7c7d6de380d1aaa860e86627778bef6c0c78ca08.1618910364.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1618910364.git.liu.denton@gmail.com","subject":"[PATCH 2/5] git-completion.bash: rename to $__git_cmd_idx","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-20T09:19:48Z","receivedAt":"2021-04-20T09:20:08Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n__git_main(), 2021-03-24), the $__git_subcommand_idx variable was\nintroduced. Naming it after the index of the subcommand is flat-out\nwrong as this variable really holds the index of the git comand (e.g.\n\"stash\").\n\nRename this variable so that it's obvious it's about git commands. While\nwe're at it, shorten up its name so that it's still readable without\nbeing a handful to type.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 20 ++++++++++----------\n 1 file changed, 10 insertions(+), 10 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 1dedb14b47..c29c129f87 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -1474,12 +1474,12 @@ _git_branch ()\n \n _git_bundle ()\n {\n-\tlocal cmd=\"${words[__git_subcommand_idx+1]}\"\n+\tlocal cmd=\"${words[__git_cmd_idx+1]}\"\n \tcase \"$cword\" in\n-\t$((__git_subcommand_idx+1)))\n+\t$((__git_cmd_idx+1)))\n \t\t__gitcomp \"create list-heads verify unbundle\"\n \t\t;;\n-\t$((__git_subcommand_idx+2)))\n+\t$((__git_cmd_idx+2)))\n \t\t# looking for a file\n \t\t;;\n \t*)\n@@ -1894,7 +1894,7 @@ _git_grep ()\n \tesac\n \n \tcase \"$cword,$prev\" in\n-\t$((__git_subcommand_idx+1)),*|*,-*)\n+\t$((__git_cmd_idx+1)),*|*,-*)\n \t\t__git_complete_symbol && return\n \t\t;;\n \tesac\n@@ -3017,7 +3017,7 @@ _git_stash ()\n \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands save\")\"\n \n \tif [ -z \"$subcommand\" ]; then\n-\t\tcase \"$((cword - __git_subcommand_idx)),$cur\" in\n+\t\tcase \"$((cword - __git_cmd_idx)),$cur\" in\n \t\t*,--*)\n \t\t\t__gitcomp_builtin stash_push\n \t\t\t;;\n@@ -3058,7 +3058,7 @@ _git_stash ()\n \t\t__gitcomp_builtin stash_branch\n \t\t;;\n \tbranch,*)\n-\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n+\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n \t\t\t__git_complete_refs\n \t\telse\n \t\t\t__gitcomp_nl \"$(__git stash list \\\n@@ -3303,7 +3303,7 @@ _git_worktree ()\n \t\t\t# be either the 'add' subcommand, the unstuck\n \t\t\t# argument of an option (e.g. branch for -b|-B), or\n \t\t\t# the path for the new worktree.\n-\t\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n+\t\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n \t\t\t\t# Right after the 'add' subcommand: have to\n \t\t\t\t# complete the path, so fall back to Bash\n \t\t\t\t# filename completion.\n@@ -3327,7 +3327,7 @@ _git_worktree ()\n \t\t__git_complete_worktree_paths\n \t\t;;\n \tmove,*)\n-\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n+\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n \t\t\t# The first parameter must be an existing working\n \t\t\t# tree to be moved.\n \t\t\t__git_complete_worktree_paths\n@@ -3395,7 +3395,7 @@ __git_main ()\n {\n \tlocal i c=1 command __git_dir __git_repo_path\n \tlocal __git_C_args C_args_count=0\n-\tlocal __git_subcommand_idx\n+\tlocal __git_cmd_idx\n \n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n@@ -3426,7 +3426,7 @@ __git_main ()\n \t\t\t;;\n \t\t*)\n \t\t\tcommand=\"$i\"\n-\t\t\t__git_subcommand_idx=\"$c\"\n+\t\t\t__git_cmd_idx=\"$c\"\n \t\t\tbreak\n \t\t\t;;\n \t\tesac\n-- \n2.31.1.424.g95a8dafae5\n\n"},{"id":"422375","messageId":"63a699258545d55e47d9f6fcf6e70055a76e8949.1618910364.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1618910364.git.liu.denton@gmail.com","subject":"[PATCH 3/5] git-completion.bash: use $__git_cmd_idx in more places","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-20T09:19:49Z","receivedAt":"2021-04-20T09:20:09Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"With the introduction of the $__git_cmd_idx variable in e94fb44042\n(git-completion.bash: pass $__git_subcommand_idx from __git_main(),\n2021-03-24), completion functions were able to know the index at which\nthe git command is listed, allowing them to skip options that are given\nto the underlying git itself, not the corresponding command (e.g.\n`-C asdf` in `git -C asdf branch`).\n\nWhile most of the changes here are self-explanatory, some bear further\nexplanation.\n\nFor the __git_find_on_cmdline() and __git_find_last_on_cmdline() pair of\nfunctions, these functions are only ever called in the context of a git\ncommand completion function. These functions will only care about words\nafter the command so we can safely ignore the words before this.\n\nFor _git_worktree(), this change is technically a no-op (once the\n__git_find_last_on_cmdline change is also applied). It was in poor style\nto have hard-coded on the index right after `worktree`. In case\n`git worktree` were to ever learn to accept options, the current\nsituation would be inflexible.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 26 ++++++++++++++------------\n t/t9902-completion.sh                  | 19 +++++++++++++++++++\n 2 files changed, 33 insertions(+), 12 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c29c129f87..30c9a97616 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -1006,8 +1006,8 @@ __git_complete_revlist ()\n \n __git_complete_remote_or_refspec ()\n {\n-\tlocal cur_=\"$cur\" cmd=\"${words[1]}\"\n-\tlocal i c=2 remote=\"\" pfx=\"\" lhs=1 no_complete_refspec=0\n+\tlocal cur_=\"$cur\" cmd=\"${words[__git_cmd_idx]}\"\n+\tlocal i c=$((__git_cmd_idx+1)) remote=\"\" pfx=\"\" lhs=1 no_complete_refspec=0\n \tif [ \"$cmd\" = \"remote\" ]; then\n \t\t((c++))\n \tfi\n@@ -1176,7 +1176,7 @@ __git_aliased_command ()\n # --show-idx: Optionally show the index of the found word in the $words array.\n __git_find_on_cmdline ()\n {\n-\tlocal word c=1 show_idx\n+\tlocal word c=\"$__git_cmd_idx\" show_idx\n \n \twhile test $# -gt 1; do\n \t\tcase \"$1\" in\n@@ -1221,7 +1221,7 @@ __git_find_last_on_cmdline ()\n \tdone\n \tlocal wordlist=\"$1\"\n \n-\twhile [ $c -gt 1 ]; do\n+\twhile [ $c -gt \"$__git_cmd_idx\" ]; do\n \t\t((c--))\n \t\tfor word in $wordlist; do\n \t\t\tif [ \"$word\" = \"${words[c]}\" ]; then\n@@ -1306,7 +1306,7 @@ __git_count_arguments ()\n \tlocal word i c=0\n \n \t# Skip \"git\" (first argument)\n-\tfor ((i=1; i < ${#words[@]}; i++)); do\n+\tfor ((i=\"$__git_cmd_idx\"; i < ${#words[@]}; i++)); do\n \t\tword=\"${words[i]}\"\n \n \t\tcase \"$word\" in\n@@ -1442,7 +1442,7 @@ __git_ref_fieldlist=\"refname objecttype objectsize objectname upstream push HEAD\n \n _git_branch ()\n {\n-\tlocal i c=1 only_local_ref=\"n\" has_r=\"n\"\n+\tlocal i c=\"$__git_cmd_idx\" only_local_ref=\"n\" has_r=\"n\"\n \n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n@@ -2474,7 +2474,7 @@ _git_switch ()\n __git_config_get_set_variables ()\n {\n \tlocal prevword word config_file= c=$cword\n-\twhile [ $c -gt 1 ]; do\n+\twhile [ $c -gt \"$__git_cmd_idx\" ]; do\n \t\tword=\"${words[c]}\"\n \t\tcase \"$word\" in\n \t\t--system|--global|--local|--file=*)\n@@ -3224,7 +3224,7 @@ _git_svn ()\n \n _git_tag ()\n {\n-\tlocal i c=1 f=0\n+\tlocal i c=\"$__git_cmd_idx\" f=0\n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n \t\tcase \"$i\" in\n@@ -3276,9 +3276,11 @@ __git_complete_worktree_paths ()\n _git_worktree ()\n {\n \tlocal subcommands=\"add list lock move prune remove unlock\"\n-\tlocal subcommand\n+\tlocal subcommand subcommand_idx\n \n-\tsubcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n+\tsubcommand=\"$(__git_find_on_cmdline --show-idx \"$subcommands\")\"\n+\tsubcommand_idx=\"${subcommand% *}\"\n+\tsubcommand=\"${subcommand#* }\"\n \n \tcase \"$subcommand,$cur\" in\n \t,*)\n@@ -3303,7 +3305,7 @@ _git_worktree ()\n \t\t\t# be either the 'add' subcommand, the unstuck\n \t\t\t# argument of an option (e.g. branch for -b|-B), or\n \t\t\t# the path for the new worktree.\n-\t\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n+\t\t\tif [ $cword -eq $((subcommand_idx+1)) ]; then\n \t\t\t\t# Right after the 'add' subcommand: have to\n \t\t\t\t# complete the path, so fall back to Bash\n \t\t\t\t# filename completion.\n@@ -3327,7 +3329,7 @@ _git_worktree ()\n \t\t__git_complete_worktree_paths\n \t\t;;\n \tmove,*)\n-\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n+\t\tif [ $cword -eq $((subcommand_idx+1)) ]; then\n \t\t\t# The first parameter must be an existing working\n \t\t\t# tree to be moved.\n \t\t\t__git_complete_worktree_paths\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 04ce884ef5..9439fec8f0 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -1879,6 +1879,7 @@ test_expect_success '__git_find_on_cmdline - single match' '\n \t(\n \t\twords=(git command --opt list) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1889,6 +1890,7 @@ test_expect_success '__git_find_on_cmdline - multiple matches' '\n \t(\n \t\twords=(git command -o --opt remove list add) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1898,6 +1900,7 @@ test_expect_success '__git_find_on_cmdline - no match' '\n \t(\n \t\twords=(git command --opt branch) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline \"add list remove\" >actual\n \t) &&\n \ttest_must_be_empty actual\n@@ -1908,6 +1911,7 @@ test_expect_success '__git_find_on_cmdline - single match with index' '\n \t(\n \t\twords=(git command --opt list) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1918,6 +1922,7 @@ test_expect_success '__git_find_on_cmdline - multiple matches with index' '\n \t(\n \t\twords=(git command -o --opt remove list add) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1927,11 +1932,23 @@ test_expect_success '__git_find_on_cmdline - no match with index' '\n \t(\n \t\twords=(git command --opt branch) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n \t) &&\n \ttest_must_be_empty actual\n '\n \n+test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '\n+\techo \"6 remove\" >expect &&\n+\t(\n+\t\twords=(git -C remove command -o --opt remove list add) &&\n+\t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=3 &&\n+\t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n+\t) &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success '__git_get_config_variables' '\n \tcat >expect <<-EOF &&\n \tname-1\n@@ -2275,6 +2292,7 @@ do\n \t\t(\n \t\t\twords=(git push '$flag' other ma) &&\n \t\t\tcword=${#words[@]} cur=${words[cword-1]} &&\n+\t\t\t__git_cmd_idx=1 &&\n \t\t\t__git_complete_remote_or_refspec &&\n \t\t\tprint_comp\n \t\t) &&\n@@ -2288,6 +2306,7 @@ do\n \t\t(\n \t\t\twords=(git push other '$flag' ma) &&\n \t\t\tcword=${#words[@]} cur=${words[cword-1]} &&\n+\t\t\t__git_cmd_idx=1 &&\n \t\t\t__git_complete_remote_or_refspec &&\n \t\t\tprint_comp\n \t\t) &&\n-- \n2.31.1.424.g95a8dafae5\n\n"},{"id":"422376","messageId":"4f8d015d54376af277883f57e8b4cf2c63ed8a03.1618910364.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1618910364.git.liu.denton@gmail.com","subject":"[PATCH 4/5] git-completion.bash: consolidate cases in _git_stash()","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-20T09:19:50Z","receivedAt":"2021-04-20T09:20:09Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"The $subcommand case statement in _git_stash() is quite repetitive.\nConsolidate the cases together into one catch-all case to reduce the\nrepetition.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 21 ++-------------------\n 1 file changed, 2 insertions(+), 19 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 30c9a97616..7bce9a0112 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3032,21 +3032,6 @@ _git_stash ()\n \tfi\n \n \tcase \"$subcommand,$cur\" in\n-\tpush,--*)\n-\t\t__gitcomp_builtin stash_push\n-\t\t;;\n-\tsave,--*)\n-\t\t__gitcomp_builtin stash_save\n-\t\t;;\n-\tpop,--*)\n-\t\t__gitcomp_builtin stash_pop\n-\t\t;;\n-\tapply,--*)\n-\t\t__gitcomp_builtin stash_apply\n-\t\t;;\n-\tdrop,--*)\n-\t\t__gitcomp_builtin stash_drop\n-\t\t;;\n \tlist,--*)\n \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n@@ -3054,8 +3039,8 @@ _git_stash ()\n \tshow,--*)\n \t\t__gitcomp_builtin stash_show \"$__git_diff_common_options\"\n \t\t;;\n-\tbranch,--*)\n-\t\t__gitcomp_builtin stash_branch\n+\t*,--*)\n+\t\t__gitcomp_builtin \"stash_$subcommand\"\n \t\t;;\n \tbranch,*)\n \t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n@@ -3069,8 +3054,6 @@ _git_stash ()\n \t\t__gitcomp_nl \"$(__git stash list \\\n \t\t\t\t| sed -n -e 's/:.*//p')\"\n \t\t;;\n-\t*)\n-\t\t;;\n \tesac\n }\n \n-- \n2.31.1.424.g95a8dafae5\n\n"},{"id":"422377","messageId":"b4a9b0afa7ab28b701499982f5a8fc66eb7e19e8.1618910364.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1618910364.git.liu.denton@gmail.com","subject":"[PATCH 5/5] git-completion.bash: consolidate no-subcommand case for _git_stash()","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-20T09:19:51Z","receivedAt":"2021-04-20T09:20:11Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"We have a separate if case for when no subcommand is given. It is\nsimpler to just consolidate this logic into the case statement below.\n\nIt would be nice to complete remove the magic that deals with indices\nand replace it with what was originally there,\n\n\tif [ -z \"$subcommand\" -a -n \"$(__git_find_on_cmdline \"-p\")\" ]; then\n\t        subcommand=\"push\"\n\tfi\n\nbut this gives a slightly incorrect completion. In the case where we're\nattempting to complete `git stash -a <TAB>` we will get the subcommands\nback as a respose instead of the completions for `git stash push`, which\nis what we'd expect. We could potentially hardcode all of the short\noptions but that would be too much work to maintain so we stick with the\nindex solution.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 30 +++++++++++++-------------\n 1 file changed, 15 insertions(+), 15 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 7bce9a0112..060adc0ed7 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3016,22 +3016,22 @@ _git_stash ()\n \tlocal subcommands='push list show apply clear drop pop create branch'\n \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands save\")\"\n \n-\tif [ -z \"$subcommand\" ]; then\n-\t\tcase \"$((cword - __git_cmd_idx)),$cur\" in\n-\t\t*,--*)\n-\t\t\t__gitcomp_builtin stash_push\n-\t\t\t;;\n-\t\t1,sa*)\n-\t\t\t__gitcomp \"save\"\n-\t\t\t;;\n-\t\t1,*)\n-\t\t\t__gitcomp \"$subcommands\"\n-\t\t\t;;\n-\t\tesac\n-\t\treturn\n-\tfi\n-\n \tcase \"$subcommand,$cur\" in\n+\t,--*)\n+\t\t__gitcomp_builtin stash_save\n+\t\t;;\n+\t,sa*)\n+\t\t__git_init_builtin_opts stash_save\n+\t\tif ((cword - __git_cmd_idx == 1)); then\n+\t\t\t__gitcomp \"save\"\n+\t\tfi\n+\t\t;;\n+\t,*)\n+\t\t__git_init_builtin_opts stash_save\n+\t\tif ((cword - __git_cmd_idx == 1)); then\n+\t\t\t__gitcomp \"$subcommands\"\n+\t\tfi\n+\t\t;;\n \tlist,--*)\n \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n-- \n2.31.1.424.g95a8dafae5\n\n"},{"id":"422380","messageId":"877dkx44ma.fsf@evledraar.gmail.com","threadId":"55527","inReplyTo":"4f8d015d54376af277883f57e8b4cf2c63ed8a03.1618910364.git.liu.denton@gmail.com","subject":"Re: [PATCH 4/5] git-completion.bash: consolidate cases in _git_stash()","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-04-20T10:44:45Z","receivedAt":"2021-04-20T10:44:52Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Apr 20 2021, Denton Liu wrote:\n\n> The $subcommand case statement in _git_stash() is quite repetitive.\n> Consolidate the cases together into one catch-all case to reduce the\n> repetition.\n>\n> Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> ---\n>  contrib/completion/git-completion.bash | 21 ++-------------------\n>  1 file changed, 2 insertions(+), 19 deletions(-)\n>\n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index 30c9a97616..7bce9a0112 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -3032,21 +3032,6 @@ _git_stash ()\n>  \tfi\n>  \n>  \tcase \"$subcommand,$cur\" in\n> -\tpush,--*)\n> -\t\t__gitcomp_builtin stash_push\n> -\t\t;;\n> -\tsave,--*)\n> -\t\t__gitcomp_builtin stash_save\n> -\t\t;;\n> -\tpop,--*)\n> -\t\t__gitcomp_builtin stash_pop\n> -\t\t;;\n> -\tapply,--*)\n> -\t\t__gitcomp_builtin stash_apply\n> -\t\t;;\n> -\tdrop,--*)\n> -\t\t__gitcomp_builtin stash_drop\n> -\t\t;;\n>  \tlist,--*)\n>  \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n>  \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n> @@ -3054,8 +3039,8 @@ _git_stash ()\n>  \tshow,--*)\n>  \t\t__gitcomp_builtin stash_show \"$__git_diff_common_options\"\n>  \t\t;;\n> -\tbranch,--*)\n> -\t\t__gitcomp_builtin stash_branch\n> +\t*,--*)\n> +\t\t__gitcomp_builtin \"stash_$subcommand\"\n>  \t\t;;\n>  \tbranch,*)\n>  \t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n> @@ -3069,8 +3054,6 @@ _git_stash ()\n>  \t\t__gitcomp_nl \"$(__git stash list \\\n>  \t\t\t\t| sed -n -e 's/:.*//p')\"\n>  \t\t;;\n> -\t*)\n> -\t\t;;\n>  \tesac\n>  }\n\nOne might think that this introduces a logic error in \"git stash\ndoesnotexist\" now dispatching to a non-existing \"stash_doesnotexist\" or\nsomething, bu tI see that earlier (omitted from context) in the function\nthere's an exhaustive lit of push/save/pop etc. which guards against\nthis, is is that correct?\n"},{"id":"422474","messageId":"xmqqr1j44r5g.fsf@gitster.g","threadId":"55527","inReplyTo":"7c7d6de380d1aaa860e86627778bef6c0c78ca08.1618910364.git.liu.denton@gmail.com","subject":"Re: [PATCH 2/5] git-completion.bash: rename to $__git_cmd_idx","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-04-20T20:50:19Z","receivedAt":"2021-04-20T20:50:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Denton Liu <liu.denton@gmail.com> writes:\n\n> In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n> __git_main(), 2021-03-24), the $__git_subcommand_idx variable was\n> introduced. Naming it after the index of the subcommand is flat-out\n> wrong as this variable really holds the index of the git comand (e.g.\n\ncomand -> command\n\n> \"stash\").\n>\n> Rename this variable so that it's obvious it's about git commands. While\n> we're at it, shorten up its name so that it's still readable without\n> being a handful to type.\n\nAs the patch has already written, I won't complain too much, but to\nmany people \"git\" is a command and \"add\", \"commit\" etc. are\nsubcommands of \"git\", so I do not see git_subcommand_idx so wrong\nthat it needs to be renamed.  I do understand that it is a bit too\nlong and it may be easier to type if renamed to say git_cmd_idx,\nthough ;-)\n\n> Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> ---\n>  contrib/completion/git-completion.bash | 20 ++++++++++----------\n>  1 file changed, 10 insertions(+), 10 deletions(-)\n>\n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index 1dedb14b47..c29c129f87 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -1474,12 +1474,12 @@ _git_branch ()\n>  \n>  _git_bundle ()\n>  {\n> -\tlocal cmd=\"${words[__git_subcommand_idx+1]}\"\n> +\tlocal cmd=\"${words[__git_cmd_idx+1]}\"\n>  \tcase \"$cword\" in\n> -\t$((__git_subcommand_idx+1)))\n> +\t$((__git_cmd_idx+1)))\n>  \t\t__gitcomp \"create list-heads verify unbundle\"\n>  \t\t;;\n> -\t$((__git_subcommand_idx+2)))\n> +\t$((__git_cmd_idx+2)))\n>  \t\t# looking for a file\n>  \t\t;;\n>  \t*)\n> @@ -1894,7 +1894,7 @@ _git_grep ()\n>  \tesac\n>  \n>  \tcase \"$cword,$prev\" in\n> -\t$((__git_subcommand_idx+1)),*|*,-*)\n> +\t$((__git_cmd_idx+1)),*|*,-*)\n>  \t\t__git_complete_symbol && return\n>  \t\t;;\n>  \tesac\n> @@ -3017,7 +3017,7 @@ _git_stash ()\n>  \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands save\")\"\n>  \n>  \tif [ -z \"$subcommand\" ]; then\n> -\t\tcase \"$((cword - __git_subcommand_idx)),$cur\" in\n> +\t\tcase \"$((cword - __git_cmd_idx)),$cur\" in\n>  \t\t*,--*)\n>  \t\t\t__gitcomp_builtin stash_push\n>  \t\t\t;;\n> @@ -3058,7 +3058,7 @@ _git_stash ()\n>  \t\t__gitcomp_builtin stash_branch\n>  \t\t;;\n>  \tbranch,*)\n> -\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n> +\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n>  \t\t\t__git_complete_refs\n>  \t\telse\n>  \t\t\t__gitcomp_nl \"$(__git stash list \\\n> @@ -3303,7 +3303,7 @@ _git_worktree ()\n>  \t\t\t# be either the 'add' subcommand, the unstuck\n>  \t\t\t# argument of an option (e.g. branch for -b|-B), or\n>  \t\t\t# the path for the new worktree.\n> -\t\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n> +\t\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n>  \t\t\t\t# Right after the 'add' subcommand: have to\n>  \t\t\t\t# complete the path, so fall back to Bash\n>  \t\t\t\t# filename completion.\n> @@ -3327,7 +3327,7 @@ _git_worktree ()\n>  \t\t__git_complete_worktree_paths\n>  \t\t;;\n>  \tmove,*)\n> -\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n> +\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n>  \t\t\t# The first parameter must be an existing working\n>  \t\t\t# tree to be moved.\n>  \t\t\t__git_complete_worktree_paths\n> @@ -3395,7 +3395,7 @@ __git_main ()\n>  {\n>  \tlocal i c=1 command __git_dir __git_repo_path\n>  \tlocal __git_C_args C_args_count=0\n> -\tlocal __git_subcommand_idx\n> +\tlocal __git_cmd_idx\n>  \n>  \twhile [ $c -lt $cword ]; do\n>  \t\ti=\"${words[c]}\"\n> @@ -3426,7 +3426,7 @@ __git_main ()\n>  \t\t\t;;\n>  \t\t*)\n>  \t\t\tcommand=\"$i\"\n> -\t\t\t__git_subcommand_idx=\"$c\"\n> +\t\t\t__git_cmd_idx=\"$c\"\n>  \t\t\tbreak\n>  \t\t\t;;\n>  \t\tesac\n"},{"id":"422475","messageId":"xmqqk0ow4q7k.fsf@gitster.g","threadId":"55527","inReplyTo":"b4a9b0afa7ab28b701499982f5a8fc66eb7e19e8.1618910364.git.liu.denton@gmail.com","subject":"Re: [PATCH 5/5] git-completion.bash: consolidate no-subcommand case for _git_stash()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-04-20T21:10:39Z","receivedAt":"2021-04-20T21:10:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Denton Liu <liu.denton@gmail.com> writes:\n\n> We have a separate if case for when no subcommand is given. It is\n> simpler to just consolidate this logic into the case statement below.\n\nHmph, I am not quite sure if the removal of the first case is making\nthe code easier to follow.  Is this supposed to be a no-op clean-up,\nor is it fixing some bugs?\n\n> It would be nice to complete remove the magic that deals with indices\n> and replace it with what was originally there,\n>\n> \tif [ -z \"$subcommand\" -a -n \"$(__git_find_on_cmdline \"-p\")\" ]; then\n> \t        subcommand=\"push\"\n> \tfi\n>\n> but this gives a slightly incorrect completion. In the case where we're\n> attempting to complete `git stash -a <TAB>` we will get the subcommands\n> back as a respose instead of the completions for `git stash push`, which\n> is what we'd expect. We could potentially hardcode all of the short\n> options but that would be too much work to maintain so we stick with the\n> index solution.\n>\n> Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> ---\n>  contrib/completion/git-completion.bash | 30 +++++++++++++-------------\n>  1 file changed, 15 insertions(+), 15 deletions(-)\n>\n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index 7bce9a0112..060adc0ed7 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -3016,22 +3016,22 @@ _git_stash ()\n>  \tlocal subcommands='push list show apply clear drop pop create branch'\n>  \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands save\")\"\n>  \n> -\tif [ -z \"$subcommand\" ]; then\n> -\t\tcase \"$((cword - __git_cmd_idx)),$cur\" in\n> -\t\t*,--*)\n> -\t\t\t__gitcomp_builtin stash_push\n> -\t\t\t;;\n> -\t\t1,sa*)\n> -\t\t\t__gitcomp \"save\"\n> -\t\t\t;;\n> -\t\t1,*)\n> -\t\t\t__gitcomp \"$subcommands\"\n> -\t\t\t;;\n> -\t\tesac\n> -\t\treturn\n> -\tfi\n> -\n>  \tcase \"$subcommand,$cur\" in\n> +\t,--*)\n> +\t\t__gitcomp_builtin stash_save\n> +\t\t;;\n> +\t,sa*)\n> +\t\t__git_init_builtin_opts stash_save\n> +\t\tif ((cword - __git_cmd_idx == 1)); then\n> +\t\t\t__gitcomp \"save\"\n> +\t\tfi\n> +\t\t;;\n> +\t,*)\n> +\t\t__git_init_builtin_opts stash_save\n> +\t\tif ((cword - __git_cmd_idx == 1)); then\n> +\t\t\t__gitcomp \"$subcommands\"\n> +\t\tfi\n> +\t\t;;\n>  \tlist,--*)\n>  \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n>  \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n"},{"id":"422476","messageId":"20210420211432.GA3842406@szeder.dev","threadId":"55527","inReplyTo":"xmqqr1j44r5g.fsf@gitster.g","subject":"Re: [PATCH 2/5] git-completion.bash: rename to $__git_cmd_idx","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2021-04-20T21:14:32Z","receivedAt":"2021-04-20T21:14:57Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Tue, Apr 20, 2021 at 01:50:19PM -0700, Junio C Hamano wrote:\n> Denton Liu <liu.denton@gmail.com> writes:\n> \n> > In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n> > __git_main(), 2021-03-24), the $__git_subcommand_idx variable was\n> > introduced. Naming it after the index of the subcommand is flat-out\n> > wrong as this variable really holds the index of the git comand (e.g.\n> \n> comand -> command\n> \n> > \"stash\").\n> >\n> > Rename this variable so that it's obvious it's about git commands. While\n> > we're at it, shorten up its name so that it's still readable without\n> > being a handful to type.\n> \n> As the patch has already written, I won't complain too much, but to\n> many people \"git\" is a command and \"add\", \"commit\" etc. are\n> subcommands of \"git\", so I do not see git_subcommand_idx so wrong\n> that it needs to be renamed.  I do understand that it is a bit too\n> long and it may be easier to type if renamed to say git_cmd_idx,\n> though ;-)\n\nThe completion functions for git commands having subcommands usually\nstart like this:\n\n    _git_remote ()\n    {\n        local subcommands=\"\n                add rename remove set-head set-branches\n                get-url set-url show prune update\n                \"\n        local subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n        if [ -z \"$subcommand\" ]; then\n\n__git_subcommand_idx holds the index of the word \"remote\", not the\nindex of \"add/rename/etc.\", so in the context of the completion script\nthat name is misleading.\n\n"},{"id":"422486","messageId":"xmqqo8e837vw.fsf@gitster.g","threadId":"55527","inReplyTo":"20210420211432.GA3842406@szeder.dev","subject":"Re: [PATCH 2/5] git-completion.bash: rename to $__git_cmd_idx","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-04-20T22:31:47Z","receivedAt":"2021-04-20T22:31:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder.dev@gmail.com> writes:\n\n> The completion functions for git commands having subcommands usually\n> start like this:\n>\n>     _git_remote ()\n>     {\n>         local subcommands=\"\n>                 add rename remove set-head set-branches\n>                 get-url set-url show prune update\n>                 \"\n>         local subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n>         if [ -z \"$subcommand\" ]; then\n>\n> __git_subcommand_idx holds the index of the word \"remote\", not the\n> index of \"add/rename/etc.\", so in the context of the completion script\n> that name is misleading.\n\nOK.  As subsubcommands would be a mouthful, calling \"stash\" etc. as\n\"cmd\" would be fine.\n"},{"id":"422522","messageId":"YH+kHGLBLpPVW+88@generichostname","threadId":"55527","inReplyTo":"877dkx44ma.fsf@evledraar.gmail.com","subject":"Re: [PATCH 4/5] git-completion.bash: consolidate cases in _git_stash()","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-21T04:03:40Z","receivedAt":"2021-04-21T04:03:45Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"Hi Ævar,\n\nOn Tue, Apr 20, 2021 at 12:44:45PM +0200, Ævar Arnfjörð Bjarmason wrote:\n> \n> On Tue, Apr 20 2021, Denton Liu wrote:\n> \n> > The $subcommand case statement in _git_stash() is quite repetitive.\n> > Consolidate the cases together into one catch-all case to reduce the\n> > repetition.\n> >\n> > Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> > ---\n> >  contrib/completion/git-completion.bash | 21 ++-------------------\n> >  1 file changed, 2 insertions(+), 19 deletions(-)\n> >\n> > diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> > index 30c9a97616..7bce9a0112 100644\n> > --- a/contrib/completion/git-completion.bash\n> > +++ b/contrib/completion/git-completion.bash\n> > @@ -3032,21 +3032,6 @@ _git_stash ()\n> >  \tfi\n> >  \n> >  \tcase \"$subcommand,$cur\" in\n> > -\tpush,--*)\n> > -\t\t__gitcomp_builtin stash_push\n> > -\t\t;;\n> > -\tsave,--*)\n> > -\t\t__gitcomp_builtin stash_save\n> > -\t\t;;\n> > -\tpop,--*)\n> > -\t\t__gitcomp_builtin stash_pop\n> > -\t\t;;\n> > -\tapply,--*)\n> > -\t\t__gitcomp_builtin stash_apply\n> > -\t\t;;\n> > -\tdrop,--*)\n> > -\t\t__gitcomp_builtin stash_drop\n> > -\t\t;;\n> >  \tlist,--*)\n> >  \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n> >  \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n> > @@ -3054,8 +3039,8 @@ _git_stash ()\n> >  \tshow,--*)\n> >  \t\t__gitcomp_builtin stash_show \"$__git_diff_common_options\"\n> >  \t\t;;\n> > -\tbranch,--*)\n> > -\t\t__gitcomp_builtin stash_branch\n> > +\t*,--*)\n> > +\t\t__gitcomp_builtin \"stash_$subcommand\"\n> >  \t\t;;\n> >  \tbranch,*)\n> >  \t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n> > @@ -3069,8 +3054,6 @@ _git_stash ()\n> >  \t\t__gitcomp_nl \"$(__git stash list \\\n> >  \t\t\t\t| sed -n -e 's/:.*//p')\"\n> >  \t\t;;\n> > -\t*)\n> > -\t\t;;\n> >  \tesac\n> >  }\n> \n> One might think that this introduces a logic error in \"git stash\n> doesnotexist\" now dispatching to a non-existing \"stash_doesnotexist\" or\n> something, bu tI see that earlier (omitted from context) in the function\n> there's an exhaustive lit of push/save/pop etc. which guards against\n> this, is is that correct?\n\nYep, that's exactly correct.\n"},{"id":"422523","messageId":"YH+lMZCg5cqniPaU@generichostname","threadId":"55527","inReplyTo":"xmqqk0ow4q7k.fsf@gitster.g","subject":"Re: [PATCH 5/5] git-completion.bash: consolidate no-subcommand case for _git_stash()","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-21T04:08:17Z","receivedAt":"2021-04-21T04:08:24Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"Hi Junio,\n\nOn Tue, Apr 20, 2021 at 02:10:39PM -0700, Junio C Hamano wrote:\n> Denton Liu <liu.denton@gmail.com> writes:\n> \n> > We have a separate if case for when no subcommand is given. It is\n> > simpler to just consolidate this logic into the case statement below.\n> \n> Hmph, I am not quite sure if the removal of the first case is making\n> the code easier to follow.  Is this supposed to be a no-op clean-up,\n> or is it fixing some bugs?\n\nThis is simply a no-op clean-up. I am on the fence about doing this\nchange as well so I can drop it on the next reroll unless someone has\nobjections.\n\n> > It would be nice to complete remove the magic that deals with indices\n> > and replace it with what was originally there,\n> >\n> > \tif [ -z \"$subcommand\" -a -n \"$(__git_find_on_cmdline \"-p\")\" ]; then\n> > \t        subcommand=\"push\"\n> > \tfi\n> >\n> > but this gives a slightly incorrect completion. In the case where we're\n> > attempting to complete `git stash -a <TAB>` we will get the subcommands\n> > back as a respose instead of the completions for `git stash push`, which\n> > is what we'd expect. We could potentially hardcode all of the short\n> > options but that would be too much work to maintain so we stick with the\n> > index solution.\n> >\n> > Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> > ---\n> >  contrib/completion/git-completion.bash | 30 +++++++++++++-------------\n> >  1 file changed, 15 insertions(+), 15 deletions(-)\n> >\n> > diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> > index 7bce9a0112..060adc0ed7 100644\n> > --- a/contrib/completion/git-completion.bash\n> > +++ b/contrib/completion/git-completion.bash\n> > @@ -3016,22 +3016,22 @@ _git_stash ()\n> >  \tlocal subcommands='push list show apply clear drop pop create branch'\n> >  \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands save\")\"\n> >  \n> > -\tif [ -z \"$subcommand\" ]; then\n> > -\t\tcase \"$((cword - __git_cmd_idx)),$cur\" in\n> > -\t\t*,--*)\n> > -\t\t\t__gitcomp_builtin stash_push\n> > -\t\t\t;;\n> > -\t\t1,sa*)\n> > -\t\t\t__gitcomp \"save\"\n> > -\t\t\t;;\n> > -\t\t1,*)\n> > -\t\t\t__gitcomp \"$subcommands\"\n> > -\t\t\t;;\n> > -\t\tesac\n> > -\t\treturn\n> > -\tfi\n> > -\n> >  \tcase \"$subcommand,$cur\" in\n> > +\t,--*)\n> > +\t\t__gitcomp_builtin stash_save\n> > +\t\t;;\n> > +\t,sa*)\n> > +\t\t__git_init_builtin_opts stash_save\n\nAlso, I just noticed upon re-reading this patch that this is some\nleftover cruft. But moot point since I'll be dropping this patch.\n\n> > +\t\tif ((cword - __git_cmd_idx == 1)); then\n> > +\t\t\t__gitcomp \"save\"\n> > +\t\tfi\n> > +\t\t;;\n> > +\t,*)\n> > +\t\t__git_init_builtin_opts stash_save\n> > +\t\tif ((cword - __git_cmd_idx == 1)); then\n> > +\t\t\t__gitcomp \"$subcommands\"\n> > +\t\tfi\n> > +\t\t;;\n> >  \tlist,--*)\n> >  \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n> >  \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n"},{"id":"422672","messageId":"cover.1619085387.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1618910364.git.liu.denton@gmail.com","subject":"[PATCH v2 0/4] git-completion.bash: fixes on top of 'dl/complete-stash'","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-22T10:00:48Z","receivedAt":"2021-04-22T10:01:08Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"Gábor pointed out some potential fixes what's currently queued in\n'dl/complete-stash'[0][1][2][3]. This series addresses those concerns.\n\nChanges since v1:\n\n* Make the commit message for \"git-completion.bash: rename to\n  $__git_cmd_idx\" descriptive and accurate\n\n* Drop \"git-completion.bash: consolidate no-subcommand case for\n  _git_stash()\"\n\n[0]: https://lore.kernel.org/git/20210327183554.GD2271@szeder.dev/\n[1]: https://lore.kernel.org/git/20210328103134.GF2271@szeder.dev/\n[2]: https://lore.kernel.org/git/20210328103057.GE2271@szeder.dev/\n[3]: https://lore.kernel.org/git/20210328110427.GG2271@szeder.dev/\n\nDenton Liu (4):\n  git-completion.bash: separate some commands onto their own line\n  git-completion.bash: rename to $__git_cmd_idx\n  git-completion.bash: use $__git_cmd_idx in more places\n  git-completion.bash: consolidate cases in _git_stash()\n\n contrib/completion/git-completion.bash | 98 ++++++++++++++------------\n t/t9902-completion.sh                  | 19 +++++\n 2 files changed, 70 insertions(+), 47 deletions(-)\n\nRange-diff against v1:\n1:  65c485ea0c = 1:  65c485ea0c git-completion.bash: separate some commands onto their own line\n2:  7c7d6de380 ! 2:  76328e3123 git-completion.bash: rename to $__git_cmd_idx\n    @@ Commit message\n     \n         In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n         __git_main(), 2021-03-24), the $__git_subcommand_idx variable was\n    -    introduced. Naming it after the index of the subcommand is flat-out\n    -    wrong as this variable really holds the index of the git comand (e.g.\n    -    \"stash\").\n    +    introduced. Naming it after the index of the subcommand is needlessly\n    +    confusing as, when this variable is used, it is in the completion\n    +    functions for commands (e.g. _git_remote()) where for `git remote add`,\n    +    the `remote` is referred to as the command and `add` is referred to as\n    +    the subcommand.\n     \n         Rename this variable so that it's obvious it's about git commands. While\n         we're at it, shorten up its name so that it's still readable without\n3:  63a6992585 = 3:  70fda62db1 git-completion.bash: use $__git_cmd_idx in more places\n4:  4f8d015d54 = 4:  103d38e293 git-completion.bash: consolidate cases in _git_stash()\n5:  b4a9b0afa7 < -:  ---------- git-completion.bash: consolidate no-subcommand case for _git_stash()\n-- \n2.31.1.499.g90b4fd31cd\n\n"},{"id":"422673","messageId":"65c485ea0c1726c96d292d7d7385def8d2f19f25.1619085387.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1619085387.git.liu.denton@gmail.com","subject":"[PATCH v2 1/4] git-completion.bash: separate some commands onto their own line","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-22T10:00:49Z","receivedAt":"2021-04-22T10:01:10Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n__git_main(), 2021-03-24), a line was introduced which contained\nmultiple statements. This is difficult to read so break it into multiple\nlines.\n\nWhile we're at it, follow this convention for the rest of the\n__git_main() and break up lines that contain multiple statements.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 37 +++++++++++++++++++-------\n 1 file changed, 28 insertions(+), 9 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c926ca26c6..1dedb14b47 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3400,17 +3400,35 @@ __git_main ()\n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n \t\tcase \"$i\" in\n-\t\t--git-dir=*) __git_dir=\"${i#--git-dir=}\" ;;\n-\t\t--git-dir)   ((c++)) ; __git_dir=\"${words[c]}\" ;;\n-\t\t--bare)      __git_dir=\".\" ;;\n-\t\t--help) command=\"help\"; break ;;\n-\t\t-c|--work-tree|--namespace) ((c++)) ;;\n-\t\t-C)\t__git_C_args[C_args_count++]=-C\n+\t\t--git-dir=*)\n+\t\t\t__git_dir=\"${i#--git-dir=}\"\n+\t\t\t;;\n+\t\t--git-dir)\n+\t\t\t((c++))\n+\t\t\t__git_dir=\"${words[c]}\"\n+\t\t\t;;\n+\t\t--bare)\n+\t\t\t__git_dir=\".\"\n+\t\t\t;;\n+\t\t--help)\n+\t\t\tcommand=\"help\"\n+\t\t\tbreak\n+\t\t\t;;\n+\t\t-c|--work-tree|--namespace)\n+\t\t\t((c++))\n+\t\t\t;;\n+\t\t-C)\n+\t\t\t__git_C_args[C_args_count++]=-C\n \t\t\t((c++))\n \t\t\t__git_C_args[C_args_count++]=\"${words[c]}\"\n \t\t\t;;\n-\t\t-*) ;;\n-\t\t*) command=\"$i\"; __git_subcommand_idx=\"$c\"; break ;;\n+\t\t-*)\n+\t\t\t;;\n+\t\t*)\n+\t\t\tcommand=\"$i\"\n+\t\t\t__git_subcommand_idx=\"$c\"\n+\t\t\tbreak\n+\t\t\t;;\n \t\tesac\n \t\t((c++))\n \tdone\n@@ -3432,7 +3450,8 @@ __git_main ()\n \t\t\t;;\n \t\tesac\n \t\tcase \"$cur\" in\n-\t\t--*)   __gitcomp \"\n+\t\t--*)\n+\t\t\t__gitcomp \"\n \t\t\t--paginate\n \t\t\t--no-pager\n \t\t\t--git-dir=\n-- \n2.31.1.499.g90b4fd31cd\n\n"},{"id":"422674","messageId":"76328e3123bd670435632244cbdd0e68db815804.1619085387.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1619085387.git.liu.denton@gmail.com","subject":"[PATCH v2 2/4] git-completion.bash: rename to $__git_cmd_idx","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-22T10:00:50Z","receivedAt":"2021-04-22T10:01:12Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In e94fb44042 (git-completion.bash: pass $__git_subcommand_idx from\n__git_main(), 2021-03-24), the $__git_subcommand_idx variable was\nintroduced. Naming it after the index of the subcommand is needlessly\nconfusing as, when this variable is used, it is in the completion\nfunctions for commands (e.g. _git_remote()) where for `git remote add`,\nthe `remote` is referred to as the command and `add` is referred to as\nthe subcommand.\n\nRename this variable so that it's obvious it's about git commands. While\nwe're at it, shorten up its name so that it's still readable without\nbeing a handful to type.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 20 ++++++++++----------\n 1 file changed, 10 insertions(+), 10 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 1dedb14b47..c29c129f87 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -1474,12 +1474,12 @@ _git_branch ()\n \n _git_bundle ()\n {\n-\tlocal cmd=\"${words[__git_subcommand_idx+1]}\"\n+\tlocal cmd=\"${words[__git_cmd_idx+1]}\"\n \tcase \"$cword\" in\n-\t$((__git_subcommand_idx+1)))\n+\t$((__git_cmd_idx+1)))\n \t\t__gitcomp \"create list-heads verify unbundle\"\n \t\t;;\n-\t$((__git_subcommand_idx+2)))\n+\t$((__git_cmd_idx+2)))\n \t\t# looking for a file\n \t\t;;\n \t*)\n@@ -1894,7 +1894,7 @@ _git_grep ()\n \tesac\n \n \tcase \"$cword,$prev\" in\n-\t$((__git_subcommand_idx+1)),*|*,-*)\n+\t$((__git_cmd_idx+1)),*|*,-*)\n \t\t__git_complete_symbol && return\n \t\t;;\n \tesac\n@@ -3017,7 +3017,7 @@ _git_stash ()\n \tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands save\")\"\n \n \tif [ -z \"$subcommand\" ]; then\n-\t\tcase \"$((cword - __git_subcommand_idx)),$cur\" in\n+\t\tcase \"$((cword - __git_cmd_idx)),$cur\" in\n \t\t*,--*)\n \t\t\t__gitcomp_builtin stash_push\n \t\t\t;;\n@@ -3058,7 +3058,7 @@ _git_stash ()\n \t\t__gitcomp_builtin stash_branch\n \t\t;;\n \tbranch,*)\n-\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n+\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n \t\t\t__git_complete_refs\n \t\telse\n \t\t\t__gitcomp_nl \"$(__git stash list \\\n@@ -3303,7 +3303,7 @@ _git_worktree ()\n \t\t\t# be either the 'add' subcommand, the unstuck\n \t\t\t# argument of an option (e.g. branch for -b|-B), or\n \t\t\t# the path for the new worktree.\n-\t\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n+\t\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n \t\t\t\t# Right after the 'add' subcommand: have to\n \t\t\t\t# complete the path, so fall back to Bash\n \t\t\t\t# filename completion.\n@@ -3327,7 +3327,7 @@ _git_worktree ()\n \t\t__git_complete_worktree_paths\n \t\t;;\n \tmove,*)\n-\t\tif [ $cword -eq $((__git_subcommand_idx+2)) ]; then\n+\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n \t\t\t# The first parameter must be an existing working\n \t\t\t# tree to be moved.\n \t\t\t__git_complete_worktree_paths\n@@ -3395,7 +3395,7 @@ __git_main ()\n {\n \tlocal i c=1 command __git_dir __git_repo_path\n \tlocal __git_C_args C_args_count=0\n-\tlocal __git_subcommand_idx\n+\tlocal __git_cmd_idx\n \n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n@@ -3426,7 +3426,7 @@ __git_main ()\n \t\t\t;;\n \t\t*)\n \t\t\tcommand=\"$i\"\n-\t\t\t__git_subcommand_idx=\"$c\"\n+\t\t\t__git_cmd_idx=\"$c\"\n \t\t\tbreak\n \t\t\t;;\n \t\tesac\n-- \n2.31.1.499.g90b4fd31cd\n\n"},{"id":"422675","messageId":"103d38e29394d7e4d1bf01817795aed5be568673.1619085387.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1619085387.git.liu.denton@gmail.com","subject":"[PATCH v2 4/4] git-completion.bash: consolidate cases in _git_stash()","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-22T10:00:52Z","receivedAt":"2021-04-22T10:01:14Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"The $subcommand case statement in _git_stash() is quite repetitive.\nConsolidate the cases together into one catch-all case to reduce the\nrepetition.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 21 ++-------------------\n 1 file changed, 2 insertions(+), 19 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 30c9a97616..7bce9a0112 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3032,21 +3032,6 @@ _git_stash ()\n \tfi\n \n \tcase \"$subcommand,$cur\" in\n-\tpush,--*)\n-\t\t__gitcomp_builtin stash_push\n-\t\t;;\n-\tsave,--*)\n-\t\t__gitcomp_builtin stash_save\n-\t\t;;\n-\tpop,--*)\n-\t\t__gitcomp_builtin stash_pop\n-\t\t;;\n-\tapply,--*)\n-\t\t__gitcomp_builtin stash_apply\n-\t\t;;\n-\tdrop,--*)\n-\t\t__gitcomp_builtin stash_drop\n-\t\t;;\n \tlist,--*)\n \t\t# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()\n \t\t__gitcomp_builtin stash_list \"$__git_log_common_options $__git_diff_common_options\"\n@@ -3054,8 +3039,8 @@ _git_stash ()\n \tshow,--*)\n \t\t__gitcomp_builtin stash_show \"$__git_diff_common_options\"\n \t\t;;\n-\tbranch,--*)\n-\t\t__gitcomp_builtin stash_branch\n+\t*,--*)\n+\t\t__gitcomp_builtin \"stash_$subcommand\"\n \t\t;;\n \tbranch,*)\n \t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n@@ -3069,8 +3054,6 @@ _git_stash ()\n \t\t__gitcomp_nl \"$(__git stash list \\\n \t\t\t\t| sed -n -e 's/:.*//p')\"\n \t\t;;\n-\t*)\n-\t\t;;\n \tesac\n }\n \n-- \n2.31.1.499.g90b4fd31cd\n\n"},{"id":"422676","messageId":"70fda62db198ad4a25702861565a47ee8f1dd62e.1619085387.git.liu.denton@gmail.com","threadId":"55527","inReplyTo":"cover.1619085387.git.liu.denton@gmail.com","subject":"[PATCH v2 3/4] git-completion.bash: use $__git_cmd_idx in more places","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2021-04-22T10:00:51Z","receivedAt":"2021-04-22T10:01:15Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"With the introduction of the $__git_cmd_idx variable in e94fb44042\n(git-completion.bash: pass $__git_subcommand_idx from __git_main(),\n2021-03-24), completion functions were able to know the index at which\nthe git command is listed, allowing them to skip options that are given\nto the underlying git itself, not the corresponding command (e.g.\n`-C asdf` in `git -C asdf branch`).\n\nWhile most of the changes here are self-explanatory, some bear further\nexplanation.\n\nFor the __git_find_on_cmdline() and __git_find_last_on_cmdline() pair of\nfunctions, these functions are only ever called in the context of a git\ncommand completion function. These functions will only care about words\nafter the command so we can safely ignore the words before this.\n\nFor _git_worktree(), this change is technically a no-op (once the\n__git_find_last_on_cmdline change is also applied). It was in poor style\nto have hard-coded on the index right after `worktree`. In case\n`git worktree` were to ever learn to accept options, the current\nsituation would be inflexible.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n contrib/completion/git-completion.bash | 26 ++++++++++++++------------\n t/t9902-completion.sh                  | 19 +++++++++++++++++++\n 2 files changed, 33 insertions(+), 12 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c29c129f87..30c9a97616 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -1006,8 +1006,8 @@ __git_complete_revlist ()\n \n __git_complete_remote_or_refspec ()\n {\n-\tlocal cur_=\"$cur\" cmd=\"${words[1]}\"\n-\tlocal i c=2 remote=\"\" pfx=\"\" lhs=1 no_complete_refspec=0\n+\tlocal cur_=\"$cur\" cmd=\"${words[__git_cmd_idx]}\"\n+\tlocal i c=$((__git_cmd_idx+1)) remote=\"\" pfx=\"\" lhs=1 no_complete_refspec=0\n \tif [ \"$cmd\" = \"remote\" ]; then\n \t\t((c++))\n \tfi\n@@ -1176,7 +1176,7 @@ __git_aliased_command ()\n # --show-idx: Optionally show the index of the found word in the $words array.\n __git_find_on_cmdline ()\n {\n-\tlocal word c=1 show_idx\n+\tlocal word c=\"$__git_cmd_idx\" show_idx\n \n \twhile test $# -gt 1; do\n \t\tcase \"$1\" in\n@@ -1221,7 +1221,7 @@ __git_find_last_on_cmdline ()\n \tdone\n \tlocal wordlist=\"$1\"\n \n-\twhile [ $c -gt 1 ]; do\n+\twhile [ $c -gt \"$__git_cmd_idx\" ]; do\n \t\t((c--))\n \t\tfor word in $wordlist; do\n \t\t\tif [ \"$word\" = \"${words[c]}\" ]; then\n@@ -1306,7 +1306,7 @@ __git_count_arguments ()\n \tlocal word i c=0\n \n \t# Skip \"git\" (first argument)\n-\tfor ((i=1; i < ${#words[@]}; i++)); do\n+\tfor ((i=\"$__git_cmd_idx\"; i < ${#words[@]}; i++)); do\n \t\tword=\"${words[i]}\"\n \n \t\tcase \"$word\" in\n@@ -1442,7 +1442,7 @@ __git_ref_fieldlist=\"refname objecttype objectsize objectname upstream push HEAD\n \n _git_branch ()\n {\n-\tlocal i c=1 only_local_ref=\"n\" has_r=\"n\"\n+\tlocal i c=\"$__git_cmd_idx\" only_local_ref=\"n\" has_r=\"n\"\n \n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n@@ -2474,7 +2474,7 @@ _git_switch ()\n __git_config_get_set_variables ()\n {\n \tlocal prevword word config_file= c=$cword\n-\twhile [ $c -gt 1 ]; do\n+\twhile [ $c -gt \"$__git_cmd_idx\" ]; do\n \t\tword=\"${words[c]}\"\n \t\tcase \"$word\" in\n \t\t--system|--global|--local|--file=*)\n@@ -3224,7 +3224,7 @@ _git_svn ()\n \n _git_tag ()\n {\n-\tlocal i c=1 f=0\n+\tlocal i c=\"$__git_cmd_idx\" f=0\n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n \t\tcase \"$i\" in\n@@ -3276,9 +3276,11 @@ __git_complete_worktree_paths ()\n _git_worktree ()\n {\n \tlocal subcommands=\"add list lock move prune remove unlock\"\n-\tlocal subcommand\n+\tlocal subcommand subcommand_idx\n \n-\tsubcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n+\tsubcommand=\"$(__git_find_on_cmdline --show-idx \"$subcommands\")\"\n+\tsubcommand_idx=\"${subcommand% *}\"\n+\tsubcommand=\"${subcommand#* }\"\n \n \tcase \"$subcommand,$cur\" in\n \t,*)\n@@ -3303,7 +3305,7 @@ _git_worktree ()\n \t\t\t# be either the 'add' subcommand, the unstuck\n \t\t\t# argument of an option (e.g. branch for -b|-B), or\n \t\t\t# the path for the new worktree.\n-\t\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n+\t\t\tif [ $cword -eq $((subcommand_idx+1)) ]; then\n \t\t\t\t# Right after the 'add' subcommand: have to\n \t\t\t\t# complete the path, so fall back to Bash\n \t\t\t\t# filename completion.\n@@ -3327,7 +3329,7 @@ _git_worktree ()\n \t\t__git_complete_worktree_paths\n \t\t;;\n \tmove,*)\n-\t\tif [ $cword -eq $((__git_cmd_idx+2)) ]; then\n+\t\tif [ $cword -eq $((subcommand_idx+1)) ]; then\n \t\t\t# The first parameter must be an existing working\n \t\t\t# tree to be moved.\n \t\t\t__git_complete_worktree_paths\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex 04ce884ef5..9439fec8f0 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -1879,6 +1879,7 @@ test_expect_success '__git_find_on_cmdline - single match' '\n \t(\n \t\twords=(git command --opt list) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1889,6 +1890,7 @@ test_expect_success '__git_find_on_cmdline - multiple matches' '\n \t(\n \t\twords=(git command -o --opt remove list add) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1898,6 +1900,7 @@ test_expect_success '__git_find_on_cmdline - no match' '\n \t(\n \t\twords=(git command --opt branch) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline \"add list remove\" >actual\n \t) &&\n \ttest_must_be_empty actual\n@@ -1908,6 +1911,7 @@ test_expect_success '__git_find_on_cmdline - single match with index' '\n \t(\n \t\twords=(git command --opt list) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1918,6 +1922,7 @@ test_expect_success '__git_find_on_cmdline - multiple matches with index' '\n \t(\n \t\twords=(git command -o --opt remove list add) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n \t) &&\n \ttest_cmp expect actual\n@@ -1927,11 +1932,23 @@ test_expect_success '__git_find_on_cmdline - no match with index' '\n \t(\n \t\twords=(git command --opt branch) &&\n \t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=1 &&\n \t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n \t) &&\n \ttest_must_be_empty actual\n '\n \n+test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '\n+\techo \"6 remove\" >expect &&\n+\t(\n+\t\twords=(git -C remove command -o --opt remove list add) &&\n+\t\tcword=${#words[@]} &&\n+\t\t__git_cmd_idx=3 &&\n+\t\t__git_find_on_cmdline --show-idx \"add list remove\" >actual\n+\t) &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success '__git_get_config_variables' '\n \tcat >expect <<-EOF &&\n \tname-1\n@@ -2275,6 +2292,7 @@ do\n \t\t(\n \t\t\twords=(git push '$flag' other ma) &&\n \t\t\tcword=${#words[@]} cur=${words[cword-1]} &&\n+\t\t\t__git_cmd_idx=1 &&\n \t\t\t__git_complete_remote_or_refspec &&\n \t\t\tprint_comp\n \t\t) &&\n@@ -2288,6 +2306,7 @@ do\n \t\t(\n \t\t\twords=(git push other '$flag' ma) &&\n \t\t\tcword=${#words[@]} cur=${words[cword-1]} &&\n+\t\t\t__git_cmd_idx=1 &&\n \t\t\t__git_complete_remote_or_refspec &&\n \t\t\tprint_comp\n \t\t) &&\n-- \n2.31.1.499.g90b4fd31cd\n\n"}]}