Skip to content

Commit fef56eb

Browse files
szedergitster
authored andcommitted
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>
1 parent 400a755 commit fef56eb

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed

contrib/completion/git-completion.bash

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ _get_comp_words_by_ref ()
213213
}
214214
fi
215215

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+
216230
__gitcompappend ()
217231
{
218232
local x i=${#COMPREPLY[@]}
@@ -354,18 +368,21 @@ __git_tags ()
354368
# Can be the name of a configured remote, a path, or a URL.
355369
# 2: In addition to local refs, list unique branches from refs/remotes/ for
356370
# '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).
358372
# 4: List only refs matching this word (optional; list all refs if unset or
359373
# empty).
374+
# 5: A suffix to be appended to each listed ref (optional; ignored, if set
375+
# but empty).
360376
#
361377
# Use __git_complete_refs() instead.
362378
__git_refs ()
363379
{
364380
local i hash dir track="${2-}"
365381
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-}"
368384
local match="${4-}"
385+
local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
369386

370387
__git_find_repo_path
371388
dir="$__git_repo_path"
@@ -390,7 +407,8 @@ __git_refs ()
390407

391408
if [ "$list_refs_from" = path ]; then
392409
if [[ "$cur_" == ^* ]]; then
393-
pfx="^"
410+
pfx="$pfx^"
411+
fer_pfx="$fer_pfx^"
394412
cur_=${cur_#^}
395413
match=${match#^}
396414
fi
@@ -405,7 +423,7 @@ __git_refs ()
405423
case "$i" in
406424
$match*)
407425
if [ -e "$dir/$i" ]; then
408-
echo $pfx$i
426+
echo "$pfx$i$sfx"
409427
fi
410428
;;
411429
esac
@@ -416,13 +434,13 @@ __git_refs ()
416434
"refs/remotes/$match*" "refs/remotes/$match*/**")
417435
;;
418436
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" \
420438
"${refs[@]}"
421439
if [ -n "$track" ]; then
422440
# employ the heuristic used by git checkout
423441
# Try to find a remote branch that matches the completion word
424442
# 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" \
426444
--sort="refname:strip=3" \
427445
"refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
428446
uniq -u
@@ -435,16 +453,16 @@ __git_refs ()
435453
while read -r hash i; do
436454
case "$i" in
437455
*^{}) ;;
438-
*) echo "$i" ;;
456+
*) echo "$pfx$i$sfx" ;;
439457
esac
440458
done
441459
;;
442460
*)
443461
if [ "$list_refs_from" = remote ]; then
444462
case "HEAD" in
445-
$match*) echo "HEAD" ;;
463+
$match*) echo "${pfx}HEAD$sfx" ;;
446464
esac
447-
__git for-each-ref --format="%(refname:strip=3)" \
465+
__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
448466
"refs/remotes/$remote/$match*" \
449467
"refs/remotes/$remote/$match*/**"
450468
else
@@ -458,8 +476,8 @@ __git_refs ()
458476
while read -r hash i; do
459477
case "$i" in
460478
*^{}) ;;
461-
refs/*) echo "${i#refs/*/}" ;;
462-
*) echo "$i" ;; # symbolic refs
479+
refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
480+
*) echo "$pfx$i$sfx" ;; # symbolic refs
463481
esac
464482
done
465483
fi
@@ -494,8 +512,7 @@ __git_complete_refs ()
494512
shift
495513
done
496514

497-
__gitcomp_nl "$(__git_refs "$remote" "$track" "" "$cur_")" \
498-
"$pfx" "$cur_" "$sfx"
515+
__gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
499516
}
500517

501518
# __git_refs2 requires 1 argument (to pass to __git_refs)
@@ -2997,6 +3014,15 @@ if [[ -n ${ZSH_VERSION-} ]]; then
29973014
esac
29983015
}
29993016

3017+
__gitcomp_direct ()
3018+
{
3019+
emulate -L zsh
3020+
3021+
local IFS=$'\n'
3022+
compset -P '*[=:]'
3023+
compadd -Q -- ${=1} && _ret=0
3024+
}
3025+
30003026
__gitcomp_nl ()
30013027
{
30023028
emulate -L zsh

contrib/completion/git-completion.zsh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ __gitcomp ()
6767
esac
6868
}
6969

70+
__gitcomp_direct ()
71+
{
72+
emulate -L zsh
73+
74+
local IFS=$'\n'
75+
compset -P '*[=:]'
76+
compadd -Q -- ${=1} && _ret=0
77+
}
78+
7079
__gitcomp_nl ()
7180
{
7281
emulate -L zsh

t/t9902-completion.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,22 @@ test_expect_success '__gitdir - remote as argument' '
400400
test_cmp expected "$actual"
401401
'
402402

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+
403419
test_expect_success '__gitcomp - trailing space - options' '
404420
test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
405421
--reset-author" <<-EOF
@@ -961,6 +977,17 @@ test_expect_success 'teardown after filtering matching refs' '
961977
git -C otherrepo branch -D matching/branch-in-other
962978
'
963979

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+
964991
test_expect_success '__git_complete_refs - simple' '
965992
sed -e "s/Z$//" >expected <<-EOF &&
966993
HEAD Z

0 commit comments

Comments
 (0)