git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "SZEDER Gábor" <szeder@ira.uka.de>
To: git@vger.kernel.org
Cc: "Felipe Contreras" <felipe.contreras@gmail.com>,
	"Jeff King" <peff@peff.net>,
	"Jonathan Nieder" <jrnieder@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"SZEDER Gábor" <szeder@ira.uka.de>
Subject: [PATCH 6/7] completion: fix expansion issue in __gitcomp()
Date: Sat, 17 Nov 2012 12:05:52 +0100	[thread overview]
Message-ID: <1353150353-29874-7-git-send-email-szeder@ira.uka.de> (raw)
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
words passed to __gitcomp() contain expandable substrings.

In __gitcomp() we only use a small subset ot compgen's functionality,
namely the filtering of matching possible completion words and adding
a prefix to each of those words; suffix is added by the __gitcomp_1()
helper function instead.  Now, since we already have to iterate over
all words in the wordlist to append a trailing space if necessary, we
can check in the same loop whether a word matches the word to be
completed and store matching words with possible prefix and/or suffix
in the COMPREPLY array.  This way we achieve the same functionality
without the compgen builtin and, more importantly, without it's
problematic expansions.

An additional benefit, especially for Windows/MSysGit users, is the
loss of two subshells, one to run __gitcomp_1() and the other to run
the compgen builtin.

Note, that like the previous patch for __gitcomp_nl(), this patch
doesn't quote expandable words either, but that could be implemented
later on top by unquoting $cur and then quoting what get stored in
COMPREPLY.

Also update the function's description a bit.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash | 27 ++++++++++++++++++++-------
 t/t9902-completion.sh                  |  2 +-
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 65196ddd..283ef99b 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -225,12 +225,13 @@ _get_comp_words_by_ref ()
 fi
 fi
 
-# Generates completion reply with compgen, appending a space to possible
-# completion words, if necessary.
+# Generates completion reply for the word in $cur, appending a space to
+# possible completion words, if necessary.
 # It accepts 1 to 4 arguments:
 # 1: List of possible completion words.
 # 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 (optional).
 __gitcomp ()
 {
@@ -241,10 +242,22 @@ __gitcomp ()
 		COMPREPLY=()
 		;;
 	*)
-		local IFS=$'\n'
-		COMPREPLY=($(compgen -P "${2-}" \
-			-W "$(__gitcomp_1 "${1-}" "${4-}")" \
-			-- "$cur_"))
+		local i=0 c IFS=$' \t\n'
+		for c in $1; do
+			case $c in
+			"$cur_"*)
+				c="$c${4-}"
+				case $c in
+				--*=*|*.) ;;
+				*) c="$c " ;;
+				esac
+				COMPREPLY[$i]="${2-}$c"
+				i=$((++i))
+				;;
+			*)
+				;;
+			esac
+		done
 		;;
 	esac
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index fa289324..d08f4259 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -156,7 +156,7 @@ test_expect_success '__gitcomp - suffix' '
 	test_cmp expected out
 '
 
-test_expect_failure '__gitcomp - doesnt fail because of invalid variable name' '
+test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
 	(
 		__gitcomp "$invalid_variable_name"
 	)
-- 
1.8.0.220.g4d14ece

  parent reply	other threads:[~2012-11-17 11:07 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-17 11:05 [PATCH 0/7] completion: fix expansion issues with compgen SZEDER Gábor
2012-11-17 11:05 ` [PATCH 1/7] completion: make the 'basic' test more tester-friendly SZEDER Gábor
2012-11-17 23:00   ` Jonathan Nieder
2012-11-18  9:38     ` Felipe Contreras
2012-11-18  9:39   ` Felipe Contreras
2012-11-17 11:05 ` [PATCH 2/7] completion: fix args of run_completion() test helper SZEDER Gábor
2012-11-17 23:02   ` Jonathan Nieder
2012-11-18  8:48   ` Felipe Contreras
2012-11-17 11:05 ` [PATCH 3/7] completion: add tests for the __gitcomp_nl() completion helper function SZEDER Gábor
2012-11-17 23:31   ` Jonathan Nieder
2012-11-18  8:53   ` Felipe Contreras
2012-11-17 11:05 ` [PATCH 4/7] completion: add tests for invalid variable name among completion words SZEDER Gábor
2012-11-17 23:40   ` Jonathan Nieder
2012-11-18  8:34     ` Felipe Contreras
2012-11-18  8:34   ` Felipe Contreras
2012-11-17 11:05 ` [PATCH 5/7] completion: fix expansion issues in __gitcomp_nl() SZEDER Gábor
2012-11-17 11:50   ` Felipe Contreras
2012-11-17 14:14     ` SZEDER Gábor
2012-11-17 19:08       ` Felipe Contreras
2012-11-17 19:28         ` Felipe Contreras
2012-11-18  0:55           ` Felipe Contreras
2012-11-18  0:58   ` Felipe Contreras
2012-11-17 11:05 ` SZEDER Gábor [this message]
2012-11-17 11:39   ` [PATCH 6/7] completion: fix expansion issue in __gitcomp() Felipe Contreras
2012-11-17 14:09     ` SZEDER Gábor
2012-11-17 11:05 ` [PATCH 7/7] completion: remove the now unused __gitcomp_1() internal helper function SZEDER Gábor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1353150353-29874-7-git-send-email-szeder@ira.uka.de \
    --to=szeder@ira.uka.de \
    --cc=felipe.contreras@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).