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

[PATCH 11/12] completion: list only matching symbolic and pseudorefs when completing refs

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

The previous changes in this series ensure that __git_refs() lists only refs that match the current word to be completed, but non-matching symbolic and pseudo refs still show up in its output.

Filter out non-matching symbolic and pseudo refs as well, so anything __git_refs() outputs matches the current word to be completed, as it will allow us to optimize how refs are placed into the COMPREPLY array.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 20 ++++++++++++++++----
 t/t9902-completion.sh                  |  4 ----
 2 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 9f5a6f44e..0ad02cec6 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -355,7 +355,8 @@ __git_tags ()
 # 2: In addition to local refs, list unique branches from refs/remotes/ for
 #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
 # 3: Currently ignored.
-# 4: The current ref to be completed (optional).
+# 4: List only refs matching this word instead of the current word being
+#    completed (optional).
 #
 # Use __git_complete_refs() instead.
 __git_refs ()
@@ -399,7 +400,12 @@ __git_refs ()
 			;;
 		*)
 			for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
-				if [ -e "$dir/$i" ]; then echo $pfx$i; fi
+				case "$i" in
+				$cur_*)	if [ -e "$dir/$i" ]; then
+						echo $pfx$i
+					fi
+					;;
+				esac
 			done
 			format="refname:strip=2"
 			refs=("refs/tags/$cur_*" "refs/tags/$cur_*/**"
@@ -432,12 +438,18 @@ __git_refs ()
 		;;
 	*)
 		if [ "$list_refs_from" = remote ]; then
-			echo "HEAD"
+			case "HEAD" in
+			$cur_*)	echo "HEAD" ;;
+			esac
 			__git for-each-ref --format="%(refname:strip=3)" \
 				"refs/remotes/$remote/$cur_*" \
 				"refs/remotes/$remote/$cur_*/**"
 		else
-			__git ls-remote "$remote" HEAD \
+			local query_symref
+			case "HEAD" in
+			$cur_*)	query_symref="HEAD" ;;
+			esac
+			__git ls-remote "$remote" $query_symref \
 				"refs/tags/$cur_*" "refs/heads/$cur_*" \
 				"refs/remotes/$cur_*" |
 			while read -r hash i; do
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 499be5879..5e06acc17 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -847,7 +847,6 @@ test_expect_success 'setup for filtering matching refs' '
 
 test_expect_success '__git_refs - only matching refs' '
 	cat >expected <<-EOF &&
-	HEAD
 	matching-branch
 	matching/branch
 	matching-tag
@@ -874,7 +873,6 @@ test_expect_success '__git_refs - only matching refs - full refs' '
 
 test_expect_success '__git_refs - only matching refs - remote on local file system' '
 	cat >expected <<-EOF &&
-	HEAD
 	master-in-other
 	matching/branch-in-other
 	EOF
@@ -887,7 +885,6 @@ test_expect_success '__git_refs - only matching refs - remote on local file syst
 
 test_expect_success '__git_refs - only matching refs - configured remote' '
 	cat >expected <<-EOF &&
-	HEAD
 	master-in-other
 	matching/branch-in-other
 	EOF
@@ -912,7 +909,6 @@ test_expect_success '__git_refs - only matching refs - remote - full refs' '
 
 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
 	cat >expected <<-EOF &&
-	HEAD
 	matching-branch
 	matching/branch
 	matching-tag
-- 
2.11.0.555.g967c1bcb3
Previous: SZEDER GáborNext: SZEDER Gábor
Message 13 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.