From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
Hi,
I sent these some time ago for comments, but I think they are ready. Basically
some reorganization in order to achieve some performance improvements, also,
fix a few bugs.
Felipe Contreras (7):
completion: trivial test improvement
completion: get rid of empty COMPREPLY assignments
completion: add new __gitcompadd helper
completion: add __gitcomp_nl tests
completion: get rid of compgen
completion: get rid of __gitcomp_1
completion: small optimization
contrib/completion/git-completion.bash | 71 +++++++++++++---------------------
t/t9902-completion.sh | 65 ++++++++++++++++++++++++++++++-
2 files changed, 90 insertions(+), 46 deletions(-)
--
1.8.2.1
From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
Instead of passing a dummy "", let's check if the last character is a
space, and then move the _cword accordingly.
Apparently we were passing "" all the way to compgen, which fortunately
expanded it to nothing.
Lets do the right thing though.
Signed-off-by: Felipe Contreras <redacted>
---
t/t9902-completion.sh | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
There's no functional reason for those, the only purpose they are
supposed to serve is to say "we don't provide any words here", but even
for that it's not used consitently.
Signed-off-by: Felipe Contreras <redacted>
---
contrib/completion/git-completion.bash | 21 ---------------------
1 file changed, 21 deletions(-)
From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
The idea is to never touch the COMPREPLY variable directly.
This allows other completion systems (i.e. zsh) to override
__gitcompadd, and do something different instead.
Also, this allows further optimizations down the line.
There should be no functional changes.
Signed-off-by: Felipe Contreras <redacted>
---
contrib/completion/git-completion.bash | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
@@ -195,6 +195,11 @@ _get_comp_words_by_ref ()}fi+__gitcompadd()+{+COMPREPLY=($(compgen-W"$1"-P"$2"-S"$4"--"$3"))+}+# Generates completion reply with compgen, appending a space to possible# completion words, if necessary.# It accepts 1 to 4 arguments:
@@ -105,6 +105,23 @@ test_gitcomp ()test_cmpexpectedout}+# Test __gitcomp_nl+# Arguments are:+# 1: current word (cur)+# -: the rest are passed to __gitcomp_nl+test_gitcomp_nl()+{+local-aCOMPREPLY&&+sed-e's/Z$//'>expected&&+cur="$1"&&+shift&&+__gitcomp_nl"$@"&&+print_comp&&+test_cmpexpectedout+}++invalid_variable_name='${foo.bar}'+ test_expect_success'__gitcomp - trailing space - options''test_gitcomp"--re""--dry-run --reuse-message= --reedit-message=--reset-author" <<-EOF
@@ -148,6 +165,49 @@ test_expect_success '__gitcomp - suffix' 'EOF'+test_expect_failure'__gitcomp - doesnt fail because of invalid variable name''+__gitcomp"$invalid_variable_name"+'++read-r-d""refs<<-\EOF+maint+master+next+pu+EOF++test_expect_success'__gitcomp_nl - trailing space''+test_gitcomp_nl"m""$refs"<<-EOF+maintZ+masterZ+EOF+'++test_expect_success'__gitcomp_nl - prefix''+test_gitcomp_nl"--fixup=m""$refs""--fixup=""m"<<-EOF+--fixup=maintZ+--fixup=masterZ+EOF+'++test_expect_success'__gitcomp_nl - suffix''+test_gitcomp_nl"branch.ma""$refs""branch.""ma""."<<-\EOF+branch.maint.Z+branch.master.Z+EOF+'++test_expect_success'__gitcomp_nl - no suffix''+test_gitcomp_nl"ma""$refs""""ma"""<<-\EOF+maintZ+masterZ+EOF+'++test_expect_failure'__gitcomp_nl - doesnt fail because of invalid variable name''+__gitcomp_nl"$invalid_variable_name"+'+ test_expect_success'basic''run_completion"git "&&# built-in
From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
The functionality we use from compgen is not much, we can do the same
manually, with drastical improvements in speed, specially when dealing
with only a few words.
This patch also has the sideffect that brekage reported by Jeroen Meijer
and SZEDER Gábor gets fixed because we no longer expand the resulting
words.
Here are some numbers filtering N amount of words:
== 1 ==
original: 0.002s
new: 0.000s
== 10 ==
original: 0.002s
new: 0.000s
== 100 ==
original: 0.003s
new: 0.002s
== 1000 ==
original: 0.012s
new: 0.011s
== 10000 ==
original: 0.056s
new: 0.066s
== 100000 ==
original: 2.669s
new: 0.622s
If the results are not narrowed:
== 1 ==
original: 0.002s
new: 0.000s
== 10 ==
original: 0.002s
new: 0.001s
== 100 ==
original: 0.004s
new: 0.004s
== 1000 ==
original: 0.020s
new: 0.015s
== 10000 ==
original: 0.101s
new: 0.355s
== 100000 ==
original: 2.850s
new: 31.941s
So, unless 'git checkout <tab>' usually gives you more than 100000
results, you'll get an improvement :)
Other possible solutions perform better after 1000 words, but worst if
less than that:
COMPREPLY=($(awk -v cur="$3" -v pre="$2" -v suf="$4"
'$0 ~ cur { print pre$0suf }' <<< "$1" ))
COMPREPLY=($(printf -- "$2%s$4\n" $1 | grep "^$2$3"))
Signed-off-by: Felipe Contreras <redacted>
---
contrib/completion/git-completion.bash | 15 ++++++++++-----
t/t9902-completion.sh | 6 +++---
2 files changed, 13 insertions(+), 8 deletions(-)
@@ -197,11 +197,16 @@ fi __gitcompadd(){-COMPREPLY=($(compgen-W"$1"-P"$2"-S"$4"--"$3"))+locali=0+forxin$1;do+if[["$x"=="$3"*]];then+COMPREPLY[i++]="$2$x$4"+fi+done}-# Generates completion reply with compgen, appending a space to possible-# completion words, if necessary.+# Generates completion reply, 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).
@@ -221,8 +226,8 @@ __gitcomp ()esac}-# Generates completion reply with compgen from newline-separated possible-# completion words by appending a space to all of them.+# Generates completion reply from newline-separated possible completion words+# by 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).
@@ -165,7 +165,7 @@ test_expect_success '__gitcomp - suffix' 'EOF'-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"'
@@ -204,7 +204,7 @@ test_expect_success '__gitcomp_nl - no suffix' 'EOF'-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"'
@@ -332,7 +332,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''echocontent>"name with \${meta}"&&gitadd.&&gitcommit-mmeta&&
From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
There's no point in calling a separate function that is only used in one
place. Specially considering that there's no need to call compgen, and
we traverse the words ourselves both in __gitcompadd, and __gitcomp_1.
So lets squash the functions together, and traverse only once.
This improves performance. For N number of words:
== 1 ==
original: 0.002s
new: 0.000s
== 10 ==
original: 0.005s
new: 0.001s
== 100 ==
original: 0.009s
new: 0.006s
== 1000 ==
original: 0.027s
new: 0.019s
== 10000 ==
original: 0.163s
new: 0.151s
== 100000 ==
original: 1.555s
new: 1.497s
No functional changes.
Signed-off-by: Felipe Contreras <redacted>
---
contrib/completion/git-completion.bash | 26 +++++++++++---------------
1 file changed, 11 insertions(+), 15 deletions(-)
@@ -53,19 +53,6 @@ __gitdir ()fi}-__gitcomp_1()-{-localcIFS=$' \t\n'-forcin$1;do-c="$c$2"-case$cin---*=*|*.);;-*)c="$c ";;-esac-printf'%s\n'"$c"-done-}-# The following function is based on code from:## bash_completion - programmable completion functions for bash 3.2+
From: Felipe Contreras <hidden> Date: 2016-06-15 22:56:45
No need to calculate a new $c with a space if we are not going to do
anything it with it.
There should be no functional changes, except that a word "foo " with no
suffixes can't be matched. But $cur cannot have a space at the end
anyway. So it's safe.
Based on the code from SZEDER Gábor.
Signed-off-by: Felipe Contreras <redacted>
---
contrib/completion/git-completion.bash | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
From: Eric Sunshine <hidden> Date: 2016-06-15 22:56:45
On Wed, Apr 10, 2013 at 2:57 AM, Felipe Contreras
[off-list ref] wrote:
There's no functional reason for those, the only purpose they are
supposed to serve is to say "we don't provide any words here", but even
for that it's not used consitently.
From: Eric Sunshine <hidden> Date: 2016-06-15 22:56:45
On Wed, Apr 10, 2013 at 2:57 AM, Felipe Contreras
[off-list ref] wrote:
The functionality we use from compgen is not much, we can do the same
manually, with drastical improvements in speed, specially when dealing
s/drastical/drastic/
s/specially/especially/
with only a few words.
This patch also has the sideffect that brekage reported by Jeroen Meijer
s/sideffect/side effect/
s/brekage/breakage/
and SZEDER Gábor gets fixed because we no longer expand the resulting
words.
So, unless 'git checkout <tab>' usually gives you more than 100000
results, you'll get an improvement :)
Other possible solutions perform better after 1000 words, but worst if
s/worst/worse/
less than that:
COMPREPLY=($(awk -v cur="$3" -v pre="$2" -v suf="$4"
'$0 ~ cur { print pre$0suf }' <<< "$1" ))
COMPREPLY=($(printf -- "$2%s$4\n" $1 | grep "^$2$3"))
Signed-off-by: Felipe Contreras <redacted>
From: John Keeping <hidden> Date: 2016-06-15 22:56:45
On Wed, Apr 10, 2013 at 06:13:06AM -0400, Eric Sunshine wrote:
On Wed, Apr 10, 2013 at 2:57 AM, Felipe Contreras
[off-list ref] wrote:
quoted
we traverse the words ourselves both in __gitcompadd, and __gitcomp_1.
s/ourselves/ourself/
Huh? "we traverse ... ourselves" is correct since "ourselves" is
associated with the "we". I don't think "ourself" is ever correct in
normal usage - the dictionary notes that it applies only to the "royal
we".
From: Eric Sunshine <hidden> Date: 2016-06-15 22:56:45
On Wed, Apr 10, 2013 at 7:35 AM, John Keeping [off-list ref] wrote:
On Wed, Apr 10, 2013 at 06:13:06AM -0400, Eric Sunshine wrote:
quoted
On Wed, Apr 10, 2013 at 2:57 AM, Felipe Contreras
[off-list ref] wrote:
quoted
we traverse the words ourselves both in __gitcompadd, and __gitcomp_1.
s/ourselves/ourself/
Huh? "we traverse ... ourselves" is correct since "ourselves" is
associated with the "we". I don't think "ourself" is ever correct in
normal usage - the dictionary notes that it applies only to the "royal
we".