__gitcomp_nl() iterates over all the possible completion words it gets as argument - filtering matching words, - appending a trailing space to each matching word (in all but two cases), - prepending a prefix to each matching word (when completing words after e.g. '--option=<TAB>' or 'master..<TAB>'), and - adding each matching word to the COMPREPLY array. This takes a while when a lot of refs are passed to __gitcomp_nl(). The previous changes in this series ensure that __git_refs() lists only refs matching the current word to be completed, making a second filtering in __gitcomp_nl() redundant. Adding the necessary prefix and suffix could be done in __git_refs() as well: - When refs come from 'git for-each-ref', then that prefix and suffix could be added much more efficiently using a 'git for-each-ref' format containing said prefix and suffix. - When refs come from 'git ls-remote', then that prefix and suffix can be added in the shell loop that has to process 'git ls-remote's output anyway. - Finally, the prefix and suffix can be added to that handful of potentially matching symbolic and pseudo refs right away in the shell loop listing them. And then all what is still left to do is to assign a bunch of newline-separated words to a shell array, which can be done without a shell loop iterating over each word, basically making all of __gitcomp_nl() unnecessary for refs completion. Add the helper function __gitcomp_direct() to fill the COMPREPLY array with prefiltered and preprocessed words without any additional processing, without a shell loop, with just one single compound assignment. Modify __git_refs() to accept prefix and suffix parameters and add them to each and every listed ref. Modify __git_complete_refs() to pass the prefix and suffix parameters to __git_refs() and to feed __git_refs()'s output to __gitcomp_direct() instead of __gitcomp_nl(). This speeds up refs completion when there are a lot of refs matching the current word to be completed. Listing all branches for completion in a repo with 100k local branches, all packed, best of five: On Linux, near the beginning of this series, for reference: $ time __git_complete_refs real 0m2.028s user 0m1.692s sys 0m0.344s Before this patch: real 0m1.135s user 0m1.112s sys 0m0.024s After: real 0m0.367s user 0m0.352s sys 0m0.020s On Windows, near the beginning: real 0m13.078s user 0m1.609s sys 0m0.060s Before this patch: real 0m2.093s user 0m1.641s sys 0m0.060s After: real 0m0.683s user 0m0.203s sys 0m0.076s Signed-off-by: SZEDER Gábor <szeder.dev@xxxxxxxxx> --- contrib/completion/git-completion.bash | 54 ++++++++++++++++++++++++---------- contrib/completion/git-completion.zsh | 9 ++++++ t/t9902-completion.sh | 16 ++++++++++ 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 0ad02cec6..dbbb62f5f 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -213,6 +213,20 @@ _get_comp_words_by_ref () } fi +# Fills the COMPREPLY array with prefiltered words without any additional +# processing. +# Callers must take care of providing only words that match the current word +# to be completed and adding any prefix and/or suffix (trailing space!), if +# necessary. +# 1: List of newline-separated matching completion words, complete with +# prefix and suffix. +__gitcomp_direct () +{ + local IFS=$'\n' + + COMPREPLY=($1) +} + __gitcompappend () { local x i=${#COMPREPLY[@]} @@ -354,17 +368,19 @@ __git_tags () # Can be the name of a configured remote, a path, or a URL. # 2: In addition to local refs, list unique branches from refs/remotes/ for # 'git checkout's tracking DWIMery (optional; ignored, if set but empty). -# 3: Currently ignored. +# 3: A prefix to be added to each listed ref (optional). # 4: List only refs matching this word instead of the current word being -# completed (optional). +# completed (optional; NOT ignored, if empty, but lists all refs). +# 5: A suffix to be appended to each listed ref (optional; ignored, if set +# but empty). # # Use __git_complete_refs() instead. __git_refs () { local i hash dir track="${2-}" local list_refs_from=path remote="${1-}" - local format refs pfx - local cur_="${4-$cur}" + local format refs + local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}" __git_find_repo_path dir="$__git_repo_path" @@ -389,7 +405,7 @@ __git_refs () if [ "$list_refs_from" = path ]; then if [[ "$cur_" == ^* ]]; then - pfx="^" + pfx="$pfx^" cur_=${cur_#^} fi case "$cur_" in @@ -402,7 +418,7 @@ __git_refs () for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do case "$i" in $cur_*) if [ -e "$dir/$i" ]; then - echo $pfx$i + echo "$pfx$i$sfx" fi ;; esac @@ -413,13 +429,13 @@ __git_refs () "refs/remotes/$cur_*" "refs/remotes/$cur_*/**") ;; esac - __git_dir="$dir" __git for-each-ref --format="$pfx%($format)" \ + __git_dir="$dir" __git for-each-ref --format="$pfx%($format)$sfx" \ "${refs[@]}" if [ -n "$track" ]; then # employ the heuristic used by git checkout # Try to find a remote branch that matches the completion word # but only output if the branch name is unique - __git for-each-ref --format="%(refname:strip=3)" \ + __git for-each-ref --format="$pfx%(refname:strip=3)$sfx" \ --sort="refname:strip=3" \ "refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \ uniq -u @@ -432,16 +448,16 @@ __git_refs () while read -r hash i; do case "$i" in *^{}) ;; - *) echo "$i" ;; + *) echo "$pfx$i$sfx" ;; esac done ;; *) if [ "$list_refs_from" = remote ]; then case "HEAD" in - $cur_*) echo "HEAD" ;; + $cur_*) echo "${pfx}HEAD$sfx" ;; esac - __git for-each-ref --format="%(refname:strip=3)" \ + __git for-each-ref --format="$pfx%(refname:strip=3)$sfx" \ "refs/remotes/$remote/$cur_*" \ "refs/remotes/$remote/$cur_*/**" else @@ -455,8 +471,8 @@ __git_refs () while read -r hash i; do case "$i" in *^{}) ;; - refs/*) echo "${i#refs/*/}" ;; - *) echo "$i" ;; # symbolic refs + refs/*) echo "$pfx${i#refs/*/}$sfx" ;; + *) echo "$pfx$i$sfx" ;; # symbolic refs esac done fi @@ -491,8 +507,7 @@ __git_complete_refs () shift done - __gitcomp_nl "$(__git_refs "$remote" "$track" "" "$cur_")" \ - "$pfx" "$cur_" "$sfx" + __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")" } # __git_refs2 requires 1 argument (to pass to __git_refs) @@ -2975,6 +2990,15 @@ if [[ -n ${ZSH_VERSION-} ]]; then esac } + __gitcomp_direct () + { + emulate -L zsh + + local IFS=$'\n' + compset -P '*[=:]' + compadd -Q -- ${=1} && _ret=0 + } + __gitcomp_nl () { emulate -L zsh diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh index e25541308..c3521fbfc 100644 --- a/contrib/completion/git-completion.zsh +++ b/contrib/completion/git-completion.zsh @@ -67,6 +67,15 @@ __gitcomp () esac } +__gitcomp_direct () +{ + emulate -L zsh + + local IFS=$'\n' + compset -P '*[=:]' + compadd -Q -- ${=1} && _ret=0 +} + __gitcomp_nl () { emulate -L zsh diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 5e06acc17..1206a38ed 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -400,6 +400,22 @@ test_expect_success '__gitdir - remote as argument' ' test_cmp expected "$actual" ' +test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' ' + sed -e "s/Z$//g" >expected <<-EOF && + with-trailing-space Z + without-trailing-spaceZ + --option Z + --option=Z + $invalid_variable_name Z + EOF + ( + cur=should_be_ignored && + __gitcomp_direct "$(cat expected)" && + print_comp + ) && + test_cmp expected out +' + test_expect_success '__gitcomp - trailing space - options' ' test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message= --reset-author" <<-EOF -- 2.11.0.555.g967c1bcb3