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

Re: possible bug in autocompletion

From
Jeff King <peff@peff.net>
Date
Jul 17, 2012, 12:12 UTC
Message-ID
<20120717121232.GA32571@sigill.intra.peff.net>
In-Reply-To
<BLU0-SMTP405CDB35308082B180185A6B4DB0@phx.gbl>
On Tue, Jul 17, 2012 at 11:10:39AM +0200, Jeroen Meijer wrote:
Show 6 quoted lines
> We have a tag name with some special characters. The tag name is
> 'Build%20V%20${bamboo.custom.jiraversion.name}%20Build%20721'. In
> somecases where autocompletion is used an error is given, such as
> 'bash: Build%20V%20${bamboo.custom.jiraversion.name}%20Build%20721:
> bad substitution'. This can be invoked by typing 'git checkout B' and
> then pressing tab.

Hrm. Weird. It is the "${}" in your tag name that causes the problem, and it all boils down to bash trying to do parameter expansion on the contents of "compgen -W". You can see it in a much simpler example:

  $ echo '${foo.bar}' ;# no expansion, works
  ${foo.bar}
  $ echo "${foo.bar}" ;# expansion, bash rightfully complains
  bash: ${foo.bar}: bad substitution
  $ compgen -W '${foo.bar}' f
  bash: ${foo.bar}: bad substitution

In the final command, we use single-quotes so there is no expansion before the command execution. So it happens internally to compgen. documentation for compgen says:

  -W wordlist
          The  wordlist is split using the characters in the IFS special vari‐
          able as delimiters, and each resultant word is expanded.  The possi‐
          ble  completions  are  the members of the resultant list which match
          the word being completed.

Which seems kind of crazy to me. It means that we need to be quoting everything we feed to compgen to avoid accidental expansion. But I guess bash is not likely to change anytime soon, so we probably need to work around it.

> Of course; the tag is useless but still I guess this is a bug in the
> autocompletion of git.

Yeah, that tag is crazy. But this can happen anywhere that we feed arbitrary data to compgen. Try this:

  echo content >'${foo.bar}' &&
  git add . &&
  git commit
  git show HEAD:<tab>

which generates the same error. Or even a file named "foo$bar", which is much more likely; that will not generate an error, but it will expand $bar and produce erroneous results. I think we also have issues with files with single and double quotes in them.

Something like this seems to fix it for me:
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ffedce7..2d20824 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -261,7 +261,12 @@ __gitcomp ()
 __gitcomp_nl ()
 {
 	local IFS=$'\n'
-	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
+	local words=$1
+	words=${words//\\/\\\\}
+	words=${words//\$/\\\$}
+	words=${words//\'/\\\'}
+	words=${words//\"/\\\"}
+	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$words" -- "${3-$cur}"))
 }
 
 __git_heads ()

but it is awfully ugly. Maybe completion experts can offer a better
solution.

-Peff
Previous: Jeroen MeijerNext: Felipe Contreras
Message 2 of 10 in “possible bug in autocompletion”
  1. Jeroen MeijerJul 17, 2012
  2. Jeff KingJul 17, 2012
  3. Felipe ContrerasSep 19, 2012
  4. Jeff KingSep 19, 2012
  5. Felipe ContrerasSep 19, 2012
  6. Felipe ContrerasSep 19, 2012
  7. Jeff KingSep 19, 2012
  8. Felipe ContrerasSep 19, 2012
  9. Jeff KingSep 19, 2012
  10. Junio C HamanoSep 19, 2012

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.