Skip to content

Commit 824388d

Browse files
szedergitster
authored andcommitted
completion: let 'for-each-ref' filter remote branches for 'checkout' DWIMery
The code listing unique remote branches for 'git checkout's tracking DWIMery outputs only remote branches that match the current word to be completed, but the filtering is done in a shell loop iterating over all remote refs. Let 'git for-each-ref' do the filtering, as it can do so much more efficiently and we can remove that shell loop entirely. This speeds up refs completion for 'git checkout' considerably when there are a lot of non-matching remote refs to be filtered out. Uniquely completing a branch in a repository with 100k remote branches, all packed, best of five: On Linux, before: $ time __git_complete_refs --cur=maste --track real 0m1.993s user 0m1.740s sys 0m0.304s After: real 0m0.266s user 0m0.248s sys 0m0.012s On Windows, before: real 0m6.187s user 0m3.358s sys 0m2.121s After: real 0m0.750s user 0m0.015s sys 0m0.090s Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e8cb023 commit 824388d

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

contrib/completion/git-completion.bash

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,9 @@ __git_refs ()
422422
# employ the heuristic used by git checkout
423423
# Try to find a remote branch that matches the completion word
424424
# but only output if the branch name is unique
425-
local ref entry
426-
__git for-each-ref --shell --format="ref=%(refname:strip=3)" \
427-
"refs/remotes/" | \
428-
while read -r entry; do
429-
eval "$entry"
430-
if [[ "$ref" == "$match"* ]]; then
431-
echo "$ref"
432-
fi
433-
done | sort | uniq -u
425+
__git for-each-ref --format="%(refname:strip=3)" \
426+
"refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
427+
sort | uniq -u
434428
fi
435429
return
436430
fi

0 commit comments

Comments
 (0)