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

[PATCH 4/3] completion: quote completions we find

From
Jeff King <peff@peff.net>
Date
Sep 26, 2012, 21:57 UTC
Message-ID
<20120926215700.GB18628@sigill.intra.peff.net>
In-Reply-To
<20120926215119.GC18653@sigill.intra.peff.net>
On Wed, Sep 26, 2012 at 05:51:19PM -0400, Jeff King wrote:
Show 11 quoted lines
> 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:
> [...]
>   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.

Here is on attempt at that. It seems to work for me, but I know it is not what bash does internally with:

  $ ls
  name with spaces
  $ cat name<TAB>

Because in bash's internal case, it knows that "name with spaces" is the real entry (because if you have many entries and double-tab, it shows it without the quotes), and only later adds the quoting.

So while this works, I'm not sure if it is optimal or if it has any weird side effects.

-- >8 --
Subject: [PATCH] completion: quote completions we find

If you try to complete a filename with spaces, you might end up with this:

  $ git show HEAD:name<TAB>
  $ git show HEAD:name with spaces

which is technically correct, but does not help you, since the shell will split the words as soon as you hit enter. Instead, let's quote completions coming out of __gitcomp_nl to yield:

  $ git show HEAD:'name with spaces'
Signed-off-by: Jeff King <peff@peff.net>
---
 contrib/completion/git-completion.bash | 23 +++++++++++++++++++++++
 t/t9902-completion.sh                  |  6 +++---
 2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b0416ea..1fc43f7 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -274,6 +274,29 @@ __gitcomp_nl ()
 {
 	local IFS=$'\n'
 	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$(__git_quote "$1")" -- "${3-$cur}"))
+
+	local i
+	for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
+		# Hack to handle the extra space we add for some
+		# entries, since we use "-o nospace".
+		local stripped
+		case "${COMPREPLY[$i]}" in
+		*\ )
+			stripped=' '
+			COMPREPLY[$i]=${COMPREPLY[$i]% }
+			;;
+		esac
+
+		# Now we can check if we need actual quoting.
+		case "${COMPREPLY[$i]}" in
+		*[\ \$\'\"\(\)]*)
+			COMPREPLY[$i]="'${COMPREPLY[$i]//\'/\'\\\'\'}'"
+			;;
+		esac
+
+		# And then restore any stripped space.
+		COMPREPLY[$i]="${COMPREPLY[$i]}$stripped"
+	done
 }
 
 __git_heads ()
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index da67705..d2c5104 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -274,7 +274,7 @@ test_expect_success 'complete tree filename with spaces' '
 	git add . &&
 	git commit -m spaces &&
 	test_completion_long "git show HEAD:nam" <<-\EOF
-	name with spaces_
+	'\''name with spaces'\''_
 	EOF
 '
 
@@ -283,8 +283,8 @@ test_expect_success 'complete tree filename with metacharacters' '
 	git add . &&
 	git commit -m meta &&
 	test_completion_long "git show HEAD:nam" <<-\EOF
-	name with ${meta}_
-	name with spaces_
+	'\''name with ${meta}'\''_
+	'\''name with spaces'\''_
 	EOF
 '
 
-- 
1.7.12.10.g31da6dd
Previous: Jeff KingNext: SZEDER Gábor
Message 15 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.