completion: fill COMPREPLY directly when completing refs

__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. Care should be taken, though, because that prefix might contain 'for-each-ref' format specifiers as part of the left hand side of a '..' range or '...' symmetric difference notation or fetch/push/etc. refspec, e.g. 'git log "evil-%(refname)..br<TAB>'. Doubling every '%' in the prefix will prevent 'git for-each-ref' from interpolating any of those contained specifiers. - 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 as described above. 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@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Mar 23, 2017 at 16:29 UTC fef56eb0063dcf61796bf812ba980c99ce85f6b0
3 files changed +76 -14
contrib/completion/git-completion.bash
+40 -14
@@ -213,6 +213,20 @@ _get_comp_words_by_ref ()
213 }
214 fi
215
216 +# Fills the COMPREPLY array with prefiltered words without any additional
217 +# processing.
218 +# Callers must take care of providing only words that match the current word
219 +# to be completed and adding any prefix and/or suffix (trailing space!), if
220 +# necessary.
221 +# 1: List of newline-separated matching completion words, complete with
222 +# prefix and suffix.
223 +__gitcomp_direct ()
224 +{
225 + local IFS=$'\n'
226 +
227 + COMPREPLY=($1)
228 +}
229 +
230 __gitcompappend ()
231 {
232 local x i=${#COMPREPLY[@]}
@@ -354,18 +368,21 @@ __git_tags ()
368 # Can be the name of a configured remote, a path, or a URL.
369 # 2: In addition to local refs, list unique branches from refs/remotes/ for
370 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty).
357 -# 3: Currently ignored.
371 +# 3: A prefix to be added to each listed ref (optional).
372 # 4: List only refs matching this word (optional; list all refs if unset or
373 # empty).
374 +# 5: A suffix to be appended to each listed ref (optional; ignored, if set
375 +# but empty).
376 #
377 # Use __git_complete_refs() instead.
378 __git_refs ()
379 {
380 local i hash dir track="${2-}"
381 local list_refs_from=path remote="${1-}"
366 - local format refs pfx
367 - local cur_="${4-$cur}"
382 + local format refs
383 + local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
384 local match="${4-}"
385 + local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
386
387 __git_find_repo_path
388 dir="$__git_repo_path"
@@ -390,7 +407,8 @@ __git_refs ()
407
408 if [ "$list_refs_from" = path ]; then
409 if [[ "$cur_" == ^* ]]; then
393 - pfx="^"
410 + pfx="$pfx^"
411 + fer_pfx="$fer_pfx^"
412 cur_=${cur_#^}
413 match=${match#^}
414 fi
@@ -405,7 +423,7 @@ __git_refs ()
423 case "$i" in
424 $match*)
425 if [ -e "$dir/$i" ]; then
408 - echo $pfx$i
426 + echo "$pfx$i$sfx"
427 fi
428 ;;
429 esac
@@ -416,13 +434,13 @@ __git_refs ()
434 "refs/remotes/$match*" "refs/remotes/$match*/**")
435 ;;
436 esac
419 - __git_dir="$dir" __git for-each-ref --format="$pfx%($format)" \
437 + __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
438 "${refs[@]}"
439 if [ -n "$track" ]; then
440 # employ the heuristic used by git checkout
441 # Try to find a remote branch that matches the completion word
442 # but only output if the branch name is unique
425 - __git for-each-ref --format="%(refname:strip=3)" \
443 + __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
444 --sort="refname:strip=3" \
445 "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
446 uniq -u
@@ -435,16 +453,16 @@ __git_refs ()
453 while read -r hash i; do
454 case "$i" in
455 *^{}) ;;
438 - *) echo "$i" ;;
456 + *) echo "$pfx$i$sfx" ;;
457 esac
458 done
459 ;;
460 *)
461 if [ "$list_refs_from" = remote ]; then
462 case "HEAD" in
445 - $match*) echo "HEAD" ;;
463 + $match*) echo "${pfx}HEAD$sfx" ;;
464 esac
447 - __git for-each-ref --format="%(refname:strip=3)" \
465 + __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
466 "refs/remotes/$remote/$match*" \
467 "refs/remotes/$remote/$match*/**"
468 else
@@ -458,8 +476,8 @@ __git_refs ()
476 while read -r hash i; do
477 case "$i" in
478 *^{}) ;;
461 - refs/*) echo "${i#refs/*/}" ;;
462 - *) echo "$i" ;; # symbolic refs
479 + refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
480 + *) echo "$pfx$i$sfx" ;; # symbolic refs
481 esac
482 done
483 fi
@@ -494,8 +512,7 @@ __git_complete_refs ()
512 shift
513 done
514
497 - __gitcomp_nl "$(__git_refs "$remote" "$track" "" "$cur_")" \
498 - "$pfx" "$cur_" "$sfx"
515 + __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
516 }
517
518 # __git_refs2 requires 1 argument (to pass to __git_refs)
@@ -2997,6 +3014,15 @@ if [[ -n ${ZSH_VERSION-} ]]; then
3014 esac
3015 }
3016
3017 + __gitcomp_direct ()
3018 + {
3019 + emulate -L zsh
3020 +
3021 + local IFS=$'\n'
3022 + compset -P '*[=:]'
3023 + compadd -Q -- ${=1} && _ret=0
3024 + }
3025 +
3026 __gitcomp_nl ()
3027 {
3028 emulate -L zsh
contrib/completion/git-completion.zsh
+9
@@ -67,6 +67,15 @@ __gitcomp ()
67 esac
68 }
69
70 +__gitcomp_direct ()
71 +{
72 + emulate -L zsh
73 +
74 + local IFS=$'\n'
75 + compset -P '*[=:]'
76 + compadd -Q -- ${=1} && _ret=0
77 +}
78 +
79 __gitcomp_nl ()
80 {
81 emulate -L zsh
t/t9902-completion.sh
+27
@@ -400,6 +400,22 @@ test_expect_success '__gitdir - remote as argument' '
400 test_cmp expected "$actual"
401 '
402
403 +test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
404 + sed -e "s/Z$//g" >expected <<-EOF &&
405 + with-trailing-space Z
406 + without-trailing-spaceZ
407 + --option Z
408 + --option=Z
409 + $invalid_variable_name Z
410 + EOF
411 + (
412 + cur=should_be_ignored &&
413 + __gitcomp_direct "$(cat expected)" &&
414 + print_comp
415 + ) &&
416 + test_cmp expected out
417 +'
418 +
419 test_expect_success '__gitcomp - trailing space - options' '
420 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
421 --reset-author" <<-EOF
@@ -961,6 +977,17 @@ test_expect_success 'teardown after filtering matching refs' '
977 git -C otherrepo branch -D matching/branch-in-other
978 '
979
980 +test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
981 + cat >expected <<-EOF &&
982 + evil-%%-%42-%(refname)..master
983 + EOF
984 + (
985 + cur="evil-%%-%42-%(refname)..mas" &&
986 + __git_refs "" "" "evil-%%-%42-%(refname).." mas >"$actual"
987 + ) &&
988 + test_cmp expected "$actual"
989 +'
990 +
991 test_expect_success '__git_complete_refs - simple' '
992 sed -e "s/Z$//" >expected <<-EOF &&
993 HEAD Z