git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 06/12] completion: don't disambiguate short refs

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Feb 3, 2017, 02:53 UTC
Message-ID
<20170203025405.8242-7-szeder.dev@gmail.com>
In-Reply-To
<20170203025405.8242-1-szeder.dev@gmail.com>

When the completion script lists short refs it does so using the 'git for-each-ref' format 'refname:short', which makes sure that all listed refs are unambiguous. While disambiguating refs is technically correct in this case, as opposed to the cases discussed in the previous patch, this disambiguation involves several stat() syscalls for each ref, thus, unfortunately, comes at a steep cost especially on Windows and/or when there are a lot of refs to be listed. A user of Git for Windows reported[1] 'git checkout <TAB>' taking ~11 seconds in a repository with just about 4000 refs.

However, it's questionable whether ambiguous refs are really that bad to justify that much extra cost:

  - Ambiguous refs are not that common,
  - even if a repository contains ambiguous refs, they only hurt when
    the user actually happens to want to do something with one of the
    ambiguous refs, and
  - the issue can be easily circumvented by renaming those ambiguous
    refs.
  - On the other hand, apparently not that many refs are needed to
    make refs completion unacceptably slow on Windows,
  - and this slowness bites each and every time the user attempts refs
    completion, even when the repository doesn't contain any ambiguous
    refs.
  - Furthermore, circumventing the issue might not be possible or
    might be considerably more difficult and requires various
    trade-offs (e.g. working in a repository with only a few selected
    important refs while keeping a separate repository with all refs
    for reference).

Arguably, in this case the benefits of technical correctness are rather minor compared to the price we pay for it, and we are better off opting for performance over correctness.

Use the 'git for-each-ref' format 'refname:strip=2' to list short refs to spare the substantial cost of disambiguating.

This speeds up refs completion considerably. Uniquely completing a branch in a repository with 100k local branches, all packed, best of five:

  On Linux, before:
    $ time __git_complete_refs --cur=maste
    real    0m1.662s
    user    0m1.368s
    sys     0m0.296s
  After:
    real    0m0.831s
    user    0m0.808s
    sys     0m0.028s
  On Windows, before:
    real    0m12.457s
    user    0m1.016s
    sys     0m0.092s
  After:
    real    0m1.480s
    user    0m1.031s
    sys     0m0.060s
[1] - https://github.com/git-for-windows/git/issues/524
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 19799e3ba..d55eadfd1 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -401,7 +401,7 @@ __git_refs ()
 			for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
 				if [ -e "$dir/$i" ]; then echo $pfx$i; fi
 			done
-			format="refname:short"
+			format="refname:strip=2"
 			refs="refs/tags refs/heads refs/remotes"
 			;;
 		esac
@@ -412,7 +412,7 @@ __git_refs ()
 			# Try to find a remote branch that matches the completion word
 			# but only output if the branch name is unique
 			local ref entry
-			__git for-each-ref --shell --format="ref=%(refname:short)" \
+			__git for-each-ref --shell --format="ref=%(refname:strip=2)" \
 				"refs/remotes/" | \
 			while read -r entry; do
 				eval "$entry"
@@ -437,7 +437,7 @@ __git_refs ()
 	*)
 		if [ "$list_refs_from" = remote ]; then
 			echo "HEAD"
-			__git for-each-ref --format="%(refname:short)" \
+			__git for-each-ref --format="%(refname:strip=2)" \
 				"refs/remotes/$remote/" | sed -e "s#^$remote/##"
 		else
 			__git ls-remote "$remote" HEAD \
-- 
2.11.0.555.g967c1bcb3
Previous: SZEDER GáborNext: SZEDER Gábor
Message 14 of 23 in “completion: speed up refs completion”
  1. 00/12 completion: speed up refs completionSZEDER Gábor, Feb 3, 2017
  2. 01/12 completion: remove redundant __gitcomp_nl() options from _git_commit()SZEDER Gábor, Feb 3, 2017
  3. 02/12 completion: wrap __git_refs() for better option parsingSZEDER Gábor, Feb 3, 2017
  4. 07/12 completion: let 'for-each-ref' and 'ls-remote' filter matching refsSZEDER Gábor, Feb 3, 2017
  5. 03/12 completion: support completing full refs after '--option=refs/<TAB>'SZEDER Gábor, Feb 3, 2017
  6. 08/12 completion: let 'for-each-ref' strip the remote name from remote branchesSZEDER Gábor, Feb 3, 2017
  7. 12/12 completion: fill COMPREPLY directly when completing refsSZEDER Gábor, Feb 3, 2017
  8. squash! completion: fill COMPREPLY directly when completing refsSZEDER Gábor, Feb 6, 2017
  9. Junio C HamanoFeb 10, 2017
  10. SZEDER GáborFeb 13, 2017
  11. Junio C HamanoFeb 13, 2017
  12. 10/12 completion: let 'for-each-ref' sort remote branches for 'checkout' DWIMerySZEDER Gábor, Feb 3, 2017
  13. 11/12 completion: list only matching symbolic and pseudorefs when completing refsSZEDER Gábor, Feb 3, 2017
  14. 06/12 completion: don't disambiguate short refsSZEDER Gábor, Feb 3, 2017
  15. 04/12 completion: support excluding full refsSZEDER Gábor, Feb 3, 2017
  16. 05/12 completion: don't disambiguate tags and branchesSZEDER Gábor, Feb 3, 2017
  17. 09/12 completion: let 'for-each-ref' filter remote branches for 'checkout' DWIMerySZEDER Gábor, Feb 3, 2017
  18. Jacob KellerFeb 3, 2017
  19. Jacob KellerFeb 4, 2017
  20. Junio C HamanoFeb 4, 2017
  21. Jacob KellerFeb 6, 2017
  22. SZEDER GáborFeb 6, 2017
  23. Jacob KellerFeb 6, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.