{"thread":{"id":"55958","subject":"[PATCH] completion: bash: fix late declaration of __git_cmd_idx","startedAt":"2021-06-18T14:18:49Z","lastAt":"2021-06-18T19:10:03Z","messageCount":2,"participants":["Fabian Wermelinger","Felipe Contreras"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"427831","messageId":"20210618140256.256481-1-fabianw@mavt.ethz.ch","threadId":"55958","inReplyTo":null,"subject":"[PATCH] completion: bash: fix late declaration of __git_cmd_idx","fromName":"Fabian Wermelinger","fromEmail":"fabianw@mavt.ethz.ch","sentAt":"2021-06-18T14:02:57Z","receivedAt":"2021-06-18T14:18:49Z","isPatch":true,"sender":{"key":"fabianw@mavt.ethz.ch","avatar":null},"body":"A recent update to contrib/completion/git-completion.bash causes bash to fail\nauto complete custom commands that are wrapped with __git_func_wrap. Declaring\n__git_cmd_idx=0 inside __git_func_wrap resolves the issue.\n\nSigned-off-by: Fabian Wermelinger <fabianw@mavt.ethz.ch>\n---\nFixes `bash: [: -lt: unary operator expected` error for tab completions\nof wrapped `git` commands.  For example, assume the convenience command\n`gaa` defined in a shell script as\n\n```bash\ngit aa \"$@\"\n```\n\nwhere `git aa` is an alias to `git add --all`.  Then `gaa` is wrapped\nfor bash completion like\n\n```bash\n__git_complete gaa _git_add\n```\n\nFor this example, the current bash completion fails at\n`contrib/completion/git-completion.bash:1190` due to empty local\nvariable `c` as `__git_cmd_idx` is empty at the definition in\n`contrib/completion/git-completion.bash:1179`.  Defining\n`__git_cmd_idx=0` earlier in `__git_func_wrap` attempts to solve this\nproblem.\n\n contrib/completion/git-completion.bash | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex b50c5d0ea3..ca5b456173 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -3512,6 +3512,7 @@ fi\n __git_func_wrap ()\n {\n \tlocal cur words cword prev\n+\tlocal __git_cmd_idx=0\n \t_get_comp_words_by_ref -n =: cur words cword prev\n \t$1\n }\n-- \n2.32.0\n\n"},{"id":"427895","messageId":"60ccef865312e_a69392081a@natae.notmuch","threadId":"55958","inReplyTo":"20210618140256.256481-1-fabianw@mavt.ethz.ch","subject":"RE: [PATCH] completion: bash: fix late declaration of __git_cmd_idx","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2021-06-18T19:09:58Z","receivedAt":"2021-06-18T19:10:03Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Fabian Wermelinger wrote:\n> A recent update to contrib/completion/git-completion.bash causes bash to fail\n> auto complete custom commands that are wrapped with __git_func_wrap. Declaring\n> __git_cmd_idx=0 inside __git_func_wrap resolves the issue.\n\nI was about to say the idx should be 1, and that fixes the issue, but\nactually 0 is more correct, since when we do __git_complete there's\nactually no real command.\n\nIn the future we might want to improve __git_complete to manufacture a\nreal command since some completions do check the command at\n__git_cmd_idx (e.g. __git_complete_remote_or_refspec), and then set\n__git_cmd_idx to 1.\n\nBut for now your patch fixes the issue better than my previous patch\n[1], however, it's still missing the zsh part. I've sent an update patch\nwith the best of both [2].\n\nCheers.\n\n[1] https://lore.kernel.org/git/20210618182518.697912-2-felipe.contreras@gmail.com/\n[2] https://lore.kernel.org/git/20210618185832.700477-1-felipe.contreras@gmail.com/\n\nCheers.\n\n-- \nFelipe Contreras\n"}]}