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

[PATCH v7 41/49] completion: zsh: fix direct quoting

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 1, 2023, 08:46 UTC
Message-ID
<20230401084626.304356-42-felipe.contreras@gmail.com>
In-Reply-To
<20230401084626.304356-1-felipe.contreras@gmail.com>

Apparently using "compadd -Q" is almost always wrong, we want zsh to add quoting when necessary. However, if we remove the -Q option, that would make zsh add an extra "\ " at the end of some completions.

We can manually remove the spaces from the completions that have them, and then add the suffix with the -S option, thus there's no more need for the -Q option.

This makes completions like "stash@{0}" complete correctly:
  git stash show <tab>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.zsh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index dc04c5b703..e2a7f55539 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -55,7 +55,7 @@ __gitcomp ()
 	emulate -L zsh
 
 	local IFS=$' \t\n'
-	compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
+	compadd -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
 }
 
 __gitcomp_opts ()
@@ -85,14 +85,17 @@ __gitcomp_opts ()
 		fi
 		array+=("$c$sfx")
 	done
-	compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
+	compadd -S '' -p "${2-}" -a -- array && _ret=0
 }
 
 __gitcomp_nl ()
 {
 	emulate -L zsh
 
-	compadd -Q -S "${4- }" -p "${2-}" -- ${(f)1} && _ret=0
+	# words that don't end up in space
+	compadd -p "${2-}" -S "${4- }" -q -- ${${(f)1}:#*\ } && _ret=0
+	# words that end in space
+	compadd -p "${2-}" -S " ${4- }" -q -- ${${(M)${(f)1}:#*\ }% } && _ret=0
 }
 
 __gitcomp_file ()
-- 
2.33.0
Previous: Felipe ContrerasNext: Felipe Contreras
Message 42 of 50 in “completion: git-completion 2.0 patches”
  1. 00/49 completion: git-completion 2.0 patchesFelipe Contreras, Apr 1, 2023
  2. 02/49 completion: bash: trivial cleanupFelipe Contreras, Apr 1, 2023
  3. 01/49 test: completion add test for __git_cmd_idxFelipe Contreras, Apr 1, 2023
  4. 03/49 zsh: remove versionFelipe Contreras, Apr 1, 2023
  5. 04/49 completion: bash: trivial grammar fixFelipe Contreras, Apr 1, 2023
  6. 05/49 completion: zsh: add higher-priority locationFelipe Contreras, Apr 1, 2023
  7. 06/49 zsh: resolve symlink of scriptFelipe Contreras, Apr 1, 2023
  8. 07/49 zsh: simplify realpath dirname idiomFelipe Contreras, Apr 1, 2023
  9. 08/49 test: completion: use global configFelipe Contreras, Apr 1, 2023
  10. 09/49 completion: fix __git_cmd_idx regression for zshFelipe Contreras, Apr 1, 2023
  11. 10/49 completion: fix for suboptions with valueFelipe Contreras, Apr 1, 2023
  12. 11/49 completion: zsh: trivial improvementFelipe Contreras, Apr 1, 2023
  13. 12/49 completion: bash: do not modify COMP_WORDBREAKSFelipe Contreras, Apr 1, 2023
  14. 13/49 test: completion: fix currently typed wordsFelipe Contreras, Apr 1, 2023
  15. 14/49 test: completion: switch __gitcomp_nl prefix testFelipe Contreras, Apr 1, 2023
  16. 15/49 test: completion: add run_func() helperFelipe Contreras, Apr 1, 2023
  17. 16/49 completion: bash: remove non-append functionalityFelipe Contreras, Apr 1, 2023
  18. 17/49 completion: bash: get rid of _append() functionsFelipe Contreras, Apr 1, 2023
  19. 18/49 completion: bash: get rid of any non-append codeFelipe Contreras, Apr 1, 2023
  20. 19/49 completion: zsh: fix options with argumentsFelipe Contreras, Apr 1, 2023
  21. 20/49 completion: zsh: expand --git-dir file argumentFelipe Contreras, Apr 1, 2023
  22. 21/49 completion: zsh: add support for general -C optsFelipe Contreras, Apr 1, 2023
  23. 22/49 completion: zsh: fix for undefined completionsFelipe Contreras, Apr 1, 2023
  24. 23/49 completion: zsh: add support for general -c optsFelipe Contreras, Apr 1, 2023
  25. 24/49 completion: zsh: fix extra space on foo=Felipe Contreras, Apr 1, 2023
  26. 25/49 completion: zsh: add excluded optionsFelipe Contreras, Apr 1, 2023
  27. 26/49 completion: zsh: always set compsetFelipe Contreras, Apr 1, 2023
  28. 27/49 completion: factor out check in __gitcompFelipe Contreras, Apr 1, 2023
  29. 28/49 completion: simplify equal suffix checkFelipe Contreras, Apr 1, 2023
  30. 29/49 completion: refactor __gitcompFelipe Contreras, Apr 1, 2023
  31. 30/49 completion: simplify __gitcompFelipe Contreras, Apr 1, 2023
  32. 31/49 completion: bash: change suffix check in __gitcompFelipe Contreras, Apr 1, 2023
  33. 32/49 completion: improve __gitcomp suffix codeFelipe Contreras, Apr 1, 2023
  34. 33/49 test: completion: add missing testFelipe Contreras, Apr 1, 2023
  35. 34/49 completion: bash: simplify config_variable_nameFelipe Contreras, Apr 1, 2023
  36. 35/49 completion: bash: improve __gitcomp descriptionFelipe Contreras, Apr 1, 2023
  37. 36/49 completion: add __gitcomp_optsFelipe Contreras, Apr 1, 2023
  38. 37/49 completion: bash: cleanup __gitcomp* invocationsFelipe Contreras, Apr 1, 2023
  39. 38/49 completion: bash: shuffle __gitcomp functionsFelipe Contreras, Apr 1, 2023
  40. 39/49 completion: zsh: simplify __gitcomp_directFelipe Contreras, Apr 1, 2023
  41. 40/49 completion: zsh: shuffle __gitcomp* functionsFelipe Contreras, Apr 1, 2023
  42. 41/49 completion: zsh: fix direct quotingFelipe Contreras, Apr 1, 2023
  43. 42/49 completion: zsh: add elements individually in __gitcomp_optsFelipe Contreras, Apr 1, 2023
  44. 43/49 completion: zsh: add __gitcompadd helperFelipe Contreras, Apr 1, 2023
  45. 44/49 completion: zsh: add correct removable suffixFelipe Contreras, Apr 1, 2023
  46. 45/49 completion: bash: simplify _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023
  47. 46/49 completion: bash: refactor _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023
  48. 47/49 completion: bash: cleanup _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023
  49. 48/49 completion: bash: trivial cleanupFelipe Contreras, Apr 1, 2023
  50. 49/49 completion: bash: rename _get_comp_words_by_ref()Felipe Contreras, Apr 1, 2023

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.