git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] completion: bash: fix late declaration of __git_cmd_idx

From
FWFabian Wermelinger <fabianw@mavt.ethz.ch>
Date
Jun 18, 2021, 14:02 UTC
Message-ID
<20210618140256.256481-1-fabianw@mavt.ethz.ch>

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
Next: Felipe Contreras
Message 1 of 2 in “completion: bash: fix late declaration of __git_cmd_idx”
  1. completion: bash: fix late declaration of __git_cmd_idxFabian Wermelinger, Jun 18, 2021
  2. Felipe ContrerasJun 18, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.