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

[PATCH 3/3] completion: improve shell expansion of items

From
Jeff King <peff@peff.net>
Date
Sep 26, 2012, 21:51 UTC
Message-ID
<20120926215119.GC18653@sigill.intra.peff.net>
In-Reply-To
<20120926214653.GA18628@sigill.intra.peff.net>

The current completion code doesn't deal properly with items (tags, branches, etc.) that have ${} in them because they get expanded by bash while using compgen.

This patch is a rewrite of Felipe Contreras's 25ae7cf, which attempted to fix the problem by quoting the values we pass to __gitcomp_nl. However, that patch ended up quoting the whole list as a single item, which broke other completions. Instead, we need to split the newline-delimited list into elements and quote each one individually.

This is not a complete fix, as the completion result does will still contain metacharacters, so it would need extra quoting to actually be used on a command line. But it's still a step in the right direction, because:

  1. The current code's expansion means that things that are
     broken expansions (e.g., "${foo:bar}") will actually
     cause the completion function to barf, breaking all
     completion (even if you weren't going to complete that
     item). This patch fixes that so you can at least
     complete "foo" when "${foo:bar}" exists.
  2. We don't know yet what the final fix will look like,
     but this is probably the first step towards it. It
     handles quoting on the input side of compgen; the next
     step will likely be handling the quoting on the output
     side of compgen to yield a usable string for the
     command line.
Signed-off-by: Jeff King <peff@peff.net>
---
 contrib/completion/git-completion.bash | 14 +++++++++++++-
 t/t9902-completion.sh                  |  2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index be800e0..b0416ea 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -225,6 +225,18 @@ fi
 fi
 fi
 
+# Quotes each element of an IFS-delimited list for shell reuse
+__git_quote()
+{
+	local i
+	local delim
+	for i in $1; do
+		local quoted=${i//\'/\'\\\'\'}
+		printf "${delim:+$IFS}'%s'" "$quoted"
+		delim=t
+	done
+}
+
 # Generates completion reply with compgen, appending a space to possible
 # completion words, if necessary.
 # It accepts 1 to 4 arguments:
@@ -261,7 +273,7 @@ __gitcomp_nl ()
 __gitcomp_nl ()
 {
 	local IFS=$'\n'
-	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
+	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$(__git_quote "$1")" -- "${3-$cur}"))
 }
 
 __git_heads ()
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index cbd0fb6..da67705 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -278,7 +278,7 @@ test_expect_success 'complete tree filename with spaces' '
 	EOF
 '
 
-test_expect_failure 'complete tree filename with metacharacters' '
+test_expect_success 'complete tree filename with metacharacters' '
 	echo content >"name with \${meta}" &&
 	git add . &&
 	git commit -m meta &&
-- 
1.7.12.10.g31da6dd
Previous: Jeff KingNext: Jeff King
Message 14 of 37 in “completion: fix shell expansion of items”
  1. completion: fix shell expansion of itemsFelipe Contreras, Sep 20, 2012
  2. Jeff KingSep 20, 2012
  3. Junio C HamanoSep 20, 2012
  4. SZEDER GáborSep 20, 2012
  5. Jeff KingSep 20, 2012
  6. Felipe ContrerasSep 20, 2012
  7. Revert "completion: fix shell expansion of items"Jeff King, Sep 25, 2012
  8. Junio C HamanoSep 25, 2012
  9. SZEDER GáborSep 25, 2012
  10. Junio C HamanoSep 25, 2012
  11. Jeff KingSep 26, 2012
  12. 1/3 t9902: add a few basic completion testsJeff King, Sep 26, 2012
  13. 2/3 t9902: add completion tests for "odd" filenamesJeff King, Sep 26, 2012
  14. 3/3 completion: improve shell expansion of itemsJeff King, Sep 26, 2012
  15. 4/3 completion: quote completions we findJeff King, Sep 26, 2012
  16. SZEDER GáborSep 27, 2012
  17. Junio C HamanoSep 27, 2012
  18. SZEDER GáborSep 27, 2012
  19. SZEDER GáborSep 27, 2012
  20. Jeff KingSep 27, 2012
  21. Jeff KingSep 27, 2012
  22. Jeff KingSep 27, 2012
  23. SZEDER GáborSep 28, 2012
  24. 1/5 completion: fix non-critical bugs in __gitcomp() testsSZEDER Gábor, Sep 28, 2012
  25. 2/5 completion: fix args of run_completion() test helperSZEDER Gábor, Sep 28, 2012
  26. Junio C HamanoSep 28, 2012
  27. SZEDER GáborSep 28, 2012
  28. Junio C HamanoSep 28, 2012
  29. Jeff KingSep 28, 2012
  30. Junio C HamanoSep 28, 2012
  31. Jeff KingSep 28, 2012
  32. SZEDER GáborSep 28, 2012
  33. 3/5 completion: add tests for the __gitcomp_nl() completion helper functionSZEDER Gábor, Sep 28, 2012
  34. 4/5 completion: test __gitcomp() and __gitcomp_nl() with expandable wordsSZEDER Gábor, Sep 28, 2012
  35. 5/5 completion: avoid compgen to fix expansion issues in __gitcomp_nl()SZEDER Gábor, Sep 28, 2012
  36. SZEDER GáborSep 28, 2012
  37. Jeff KingSep 28, 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.