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

[PATCH 5/7] completion: fix expansion issues in __gitcomp_nl()

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Nov 17, 2012, 11:05 UTC
Message-ID
<1353150353-29874-6-git-send-email-szeder@ira.uka.de>
In-Reply-To
<1353150353-29874-1-git-send-email-szeder@ira.uka.de>

The compgen Bash-builtin performs expansion on all words in the wordlist given to its -W option, breaking Git's completion script when refs or filenames passed to __gitcomp_nl() contain expandable substrings. At least one user can't use ref completion at all in a repository, which contains tags with metacharacters like

  Build%20V%20${bamboo.custom.jiraversion.name}%20Build%20721

Such a ref causes a failure in compgen as it tries to expand the variable with invalid name.

Unfortunately, compgen has no option to turn off this expansion. However, in __gitcomp_nl() we use only a small subset of compgen's functionality, namely the filtering of matching possible completion words and adding a prefix and/or suffix to each of those words. So, to avoid compgen's problematic expansion we get rid of compgen, and implement the equivalent functionality in a small awk script. The reason for using awk instead of sed is that awk allows plain (i.e. non-regexp) string comparison, making quoting of regexp characters in the current word unnecessary.

Note, that such expandable words need quoting to be of any use on the command line. This patch doesn't perform that quoting, but it could be implemented later on top by extending the awk script to unquote $cur and to quote matching words. Nonetheless, this patch still a step forward, because at least refs can be completed even in the presence of one with metacharacters.

Also update the function's description a bit.
Reported-by: Jeroen Meijer <jjgmeijer@hotmail.com>
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---

awk doesn't have a prefixcmp(). I'm no awk wizard, so I'm not sure this is the right way to do it.

 contrib/completion/git-completion.bash | 17 +++++++++++++----
 t/t9902-completion.sh                  |  4 ++--
 2 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index bc0657a2..65196ddd 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -249,19 +249,28 @@ __gitcomp ()
 	esac
 }
 
-# Generates completion reply with compgen from newline-separated possible
-# completion words by appending a space to all of them.
+# Generates completion reply for the word in $cur from newline-separated
+# possible completion words, appending a space to all of them.
 # It accepts 1 to 4 arguments:
 # 1: List of possible completion words, separated by a single newline.
 # 2: A prefix to be added to each possible completion word (optional).
-# 3: Generate possible completion matches for this word (optional).
+# 3: Generate possible completion matches for this word instead of $cur
+#    (optional).
 # 4: A suffix to be appended to each possible completion word instead of
 #    the default space (optional).  If specified but empty, nothing is
 #    appended.
 __gitcomp_nl ()
 {
 	local IFS=$'\n'
-	COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
+	COMPREPLY=($(awk -v pfx="${2-}" -v sfx="${4- }" -v cur="${3-$cur}" '
+		BEGIN {
+			FS="\n";
+			len=length(cur);
+		}
+		{
+			if (cur == substr($1, 1, len))
+				print pfx$1sfx;
+		}' <<< "$1" ))
 }
 
 __git_heads ()
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index a108ec1c..fa289324 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -246,7 +246,7 @@ test_expect_success '__gitcomp_nl - no suffix' '
 	test_cmp expected out
 '
 
-test_expect_failure '__gitcomp_nl - doesnt fail because of invalid variable name' '
+test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
 	(
 		__gitcomp_nl "$invalid_variable_name"
 	)
@@ -383,7 +383,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.8.0.220.g4d14ece
Previous: Felipe ContrerasNext: Felipe Contreras
Message 16 of 26 in “completion: fix expansion issues with compgen”
  1. 0/7 completion: fix expansion issues with compgenSZEDER Gábor, Nov 17, 2012
  2. 1/7 completion: make the 'basic' test more tester-friendlySZEDER Gábor, Nov 17, 2012
  3. Jonathan NiederNov 17, 2012
  4. Felipe ContrerasNov 18, 2012
  5. Felipe ContrerasNov 18, 2012
  6. 2/7 completion: fix args of run_completion() test helperSZEDER Gábor, Nov 17, 2012
  7. Jonathan NiederNov 17, 2012
  8. Felipe ContrerasNov 18, 2012
  9. 3/7 completion: add tests for the __gitcomp_nl() completion helper functionSZEDER Gábor, Nov 17, 2012
  10. Jonathan NiederNov 17, 2012
  11. Felipe ContrerasNov 18, 2012
  12. 4/7 completion: add tests for invalid variable name among completion wordsSZEDER Gábor, Nov 17, 2012
  13. Jonathan NiederNov 17, 2012
  14. Felipe ContrerasNov 18, 2012
  15. Felipe ContrerasNov 18, 2012
  16. 5/7 completion: fix expansion issues in __gitcomp_nl()SZEDER Gábor, Nov 17, 2012
  17. Felipe ContrerasNov 17, 2012
  18. SZEDER GáborNov 17, 2012
  19. Felipe ContrerasNov 17, 2012
  20. Felipe ContrerasNov 17, 2012
  21. Felipe ContrerasNov 18, 2012
  22. Felipe ContrerasNov 18, 2012
  23. 6/7 completion: fix expansion issue in __gitcomp()SZEDER Gábor, Nov 17, 2012
  24. Felipe ContrerasNov 17, 2012
  25. SZEDER GáborNov 17, 2012
  26. 7/7 completion: remove the now unused __gitcomp_1() internal helper functionSZEDER Gábor, Nov 17, 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.