git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] completion: bash: fix late declaration of __git_cmd_idx
@ 2021-06-18 14:02 Fabian Wermelinger
  2021-06-18 19:09 ` Felipe Contreras
  0 siblings, 1 reply; 2+ messages in thread
From: Fabian Wermelinger @ 2021-06-18 14:02 UTC (permalink / raw)
  To: git; +Cc: Fabian Wermelinger

A recent update to contrib/completion/git-completion.bash causes bash to fail
auto complete custom commands that are wrapped with __git_func_wrap. Declaring
__git_cmd_idx=0 inside __git_func_wrap resolves the issue.

Signed-off-by: Fabian Wermelinger <fabianw@mavt.ethz.ch>
---
Fixes `bash: [: -lt: unary operator expected` error for tab completions
of wrapped `git` commands.  For example, assume the convenience command
`gaa` defined in a shell script as

```bash
git aa "$@"
```

where `git aa` is an alias to `git add --all`.  Then `gaa` is wrapped
for bash completion like

```bash
__git_complete gaa _git_add
```

For this example, the current bash completion fails at
`contrib/completion/git-completion.bash:1190` due to empty local
variable `c` as `__git_cmd_idx` is empty at the definition in
`contrib/completion/git-completion.bash:1179`.  Defining
`__git_cmd_idx=0` earlier in `__git_func_wrap` attempts to solve this
problem.

 contrib/completion/git-completion.bash | 1 +
 1 file changed, 1 insertion(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b50c5d0ea3..ca5b456173 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3512,6 +3512,7 @@ fi
 __git_func_wrap ()
 {
 	local cur words cword prev
+	local __git_cmd_idx=0
 	_get_comp_words_by_ref -n =: cur words cword prev
 	$1
 }
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* RE: [PATCH] completion: bash: fix late declaration of __git_cmd_idx
  2021-06-18 14:02 [PATCH] completion: bash: fix late declaration of __git_cmd_idx Fabian Wermelinger
@ 2021-06-18 19:09 ` Felipe Contreras
  0 siblings, 0 replies; 2+ messages in thread
From: Felipe Contreras @ 2021-06-18 19:09 UTC (permalink / raw)
  To: Fabian Wermelinger, git; +Cc: Fabian Wermelinger

Fabian Wermelinger wrote:
> A recent update to contrib/completion/git-completion.bash causes bash to fail
> auto complete custom commands that are wrapped with __git_func_wrap. Declaring
> __git_cmd_idx=0 inside __git_func_wrap resolves the issue.

I was about to say the idx should be 1, and that fixes the issue, but
actually 0 is more correct, since when we do __git_complete there's
actually no real command.

In the future we might want to improve __git_complete to manufacture a
real command since some completions do check the command at
__git_cmd_idx (e.g. __git_complete_remote_or_refspec), and then set
__git_cmd_idx to 1.

But for now your patch fixes the issue better than my previous patch
[1], however, it's still missing the zsh part. I've sent an update patch
with the best of both [2].

Cheers.

[1] https://lore.kernel.org/git/20210618182518.697912-2-felipe.contreras@gmail.com/
[2] https://lore.kernel.org/git/20210618185832.700477-1-felipe.contreras@gmail.com/

Cheers.

-- 
Felipe Contreras

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-18 19:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-18 14:02 [PATCH] completion: bash: fix late declaration of __git_cmd_idx Fabian Wermelinger
2021-06-18 19:09 ` Felipe Contreras

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).