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

[PATCH 4/9] completion: improve ls-remote output filtering in __git_refs()

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Oct 8, 2011, 14:54 UTC
Message-ID
<1318085683-29830-5-git-send-email-szeder@ira.uka.de>
In-Reply-To
<1318085683-29830-1-git-send-email-szeder@ira.uka.de>

The remote-handling part of __git_refs() has a nice for loop and state machine case statement to iterate over all words from the output of 'git ls-remote' to identify object names and ref names. Since each line in the output of 'git ls-remote' consists of an object name and a ref name, we can do more effective filtering by using a while-read loop and letting bash's word splitting take care of object names. This way the code is easier to understand and the loop will need only half the number of iterations than before.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6b5dc5cd..c6ab742d 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -579,7 +579,7 @@ __git_tags ()
 # by checkout for tracking branches
 __git_refs ()
 {
-	local i is_hash=y dir="$(__gitdir "${1-}")" track="${2-}"
+	local i hash dir="$(__gitdir "${1-}")" track="${2-}"
 	local format refs
 	if [ -d "$dir" ]; then
 		case "$cur" in
@@ -615,12 +615,12 @@ __git_refs ()
 		fi
 		return
 	fi
-	for i in $(git ls-remote "$dir" HEAD ORIG_HEAD 'refs/tags/*' 'refs/heads/*' 'refs/remotes/*' 2>/dev/null); do
-		case "$is_hash,$i" in
-		y,*) is_hash=n ;;
-		n,*^{}) is_hash=y ;;
-		n,refs/*) is_hash=y; echo "${i#refs/*/}" ;;
-		n,*) is_hash=y; echo "$i" ;;
+	git ls-remote "$dir" HEAD ORIG_HEAD 'refs/tags/*' 'refs/heads/*' 'refs/remotes/*' 2>/dev/null | \
+	while read hash i; do
+		case "$i" in
+		*^{}) ;;
+		refs/*) echo "${i#refs/*/}" ;;
+		*) echo "$i" ;;
 		esac
 	done
 }
-- 
1.7.7.187.ga41de
Previous: SZEDER GáborNext: SZEDER Gábor
Message 11 of 16 in “ref completion optimizations, fixes, and cleanups”
  1. 0/9 ref completion optimizations, fixes, and cleanupsSZEDER Gábor, Oct 8, 2011
  2. 1/9 completion: document __gitcomp()SZEDER Gábor, Oct 8, 2011
  3. 2/9 completion: optimize refs completionSZEDER Gábor, Oct 8, 2011
  4. Junio C HamanoOct 13, 2011
  5. SZEDER GáborOct 13, 2011
  6. Junio C HamanoOct 13, 2011
  7. SZEDER GáborOct 14, 2011
  8. 2/9 completion: optimize refs completionSZEDER Gábor, Oct 15, 2011
  9. Junio C HamanoOct 16, 2011
  10. 3/9 completion: make refs completion consistent for local and remote reposSZEDER Gábor, Oct 8, 2011
  11. 4/9 completion: improve ls-remote output filtering in __git_refs()SZEDER Gábor, Oct 8, 2011
  12. 5/9 completion: support full refs from remote repositoriesSZEDER Gábor, Oct 8, 2011
  13. 6/9 completion: query only refs/heads/ in __git_refs_remotes()SZEDER Gábor, Oct 8, 2011
  14. 7/9 completion: improve ls-remote output filtering in __git_refs_remotes()SZEDER Gábor, Oct 8, 2011
  15. 8/9 completion: fast initial completion for config 'remote.*.fetch' valueSZEDER Gábor, Oct 8, 2011
  16. 9/9 completion: remove broken dead code from __git_heads() and __git_tags()SZEDER Gábor, Oct 8, 2011

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.