Skip to content

Commit e8cb023

Browse files
szedergitster
authored andcommitted
completion: let 'for-each-ref' strip the remote name from remote branches
The code listing unique remote branches for 'git checkout's tracking DWIMery uses a shell parameter expansion in a loop iterating over each listed ref to remove the remote's name from the remote branches, i.e. the leading path component from the short ref. When listing refs from a configured remote repository, '| sed s///' is used for the same purpose. Let 'git for-each-ref' strip one more leading path component from the refs, i.e. use the format 'refname:strip=3' instead of '=2', making that parameter expansion and 'sed' execution unnecessary. This speeds up refs completion for 'git checkout'. Uniquely completing a branch for 'git checkout maste<TAB>' in a repo with 100k remote branches, all packed, best of five: On Linux, near the beginning of this series, for reference: $ time __git_complete_refs --cur=maste --track real 0m8.185s user 0m6.896s sys 0m1.616s Before this patch: real 0m2.714s user 0m2.344s sys 0m0.436s After: real 0m1.993s user 0m1.740s sys 0m0.304s On Windows, near the beginning: real 1m8.421s user 0m7.591s sys 0m3.557s Before this patch: real 0m8.191s user 0m4.638s sys 0m2.918s After: real 0m6.187s user 0m3.358s sys 0m2.121s Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e896369 commit e8cb023

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

contrib/completion/git-completion.bash

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,10 @@ __git_refs ()
423423
# Try to find a remote branch that matches the completion word
424424
# but only output if the branch name is unique
425425
local ref entry
426-
__git for-each-ref --shell --format="ref=%(refname:strip=2)" \
426+
__git for-each-ref --shell --format="ref=%(refname:strip=3)" \
427427
"refs/remotes/" | \
428428
while read -r entry; do
429429
eval "$entry"
430-
ref="${ref#*/}"
431430
if [[ "$ref" == "$match"* ]]; then
432431
echo "$ref"
433432
fi
@@ -450,9 +449,9 @@ __git_refs ()
450449
case "HEAD" in
451450
$match*) echo "HEAD" ;;
452451
esac
453-
__git for-each-ref --format="%(refname:strip=2)" \
452+
__git for-each-ref --format="%(refname:strip=3)" \
454453
"refs/remotes/$remote/$match*" \
455-
"refs/remotes/$remote/$match*/**" | sed -e "s#^$remote/##"
454+
"refs/remotes/$remote/$match*/**"
456455
else
457456
local query_symref
458457
case "HEAD" in

0 commit comments

Comments
 (0)