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

[PATCH 08/11] completion: perform DWIM logic directly in __git_complete_refs

From
Jacob Keller <jacob.e.keller@intel.com>
Date
Apr 25, 2020, 02:20 UTC
Message-ID
<20200425022045.1089291-10-jacob.e.keller@intel.com>
In-Reply-To
<20200425022045.1089291-1-jacob.e.keller@intel.com>
From: Jacob Keller <jacob.keller@gmail.com>

__git_complete_refs is the main function used for completing references. It is primarily used as a wrapper around __git_refs, and is easier to extend since its arguments are option-like.

One major downside of __git_complete_refs and __git_refs currently, is the lack of ability to complete only a subset of refs such as branches (refs/heads) or tags (refs/tags).

Normally, a caller might just decide to use __git_heads() or __git_tags(). However, in the case of git-switch, it is useful to complete both branches *and* DWIM remote branch names.

Due to the complexity and implementation of __git_refs, it is not easy to extend it to support listing only a subset of references.

Instead, we can extend __git_complete_refs to do this. For this to be done, we must first ensure that "--dwim" support is not tied to calling __git_refs.

Instead of passing $dwim into __git_refs, we can implement a __gitcomp_direct_append function which can append to COMPREPLY after a call to __gitcomp_direct.

If --dwim is passed to __git_complete_refs, use __gitcomp_direct_append to add the output of __git_dwim_remote_heads to the completion list.

In this way, --dwim support is now independent of calling __git_refs.

A future change will add an additional option to control what set of references __git_complete_refs will output.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---
 contrib/completion/git-completion.bash | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f1ee25f05690..c582e070711f 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -301,6 +301,19 @@ __gitcomp_direct ()
 	COMPREPLY=($1)
 }
 
+# Similar to __gitcomp_direct, but appends to COMPREPLY instead.
+# Callers must take care of providing only words that match the current word
+# to be completed and adding any prefix and/or suffix (trailing space!), if
+# necessary.
+# 1: List of newline-separated matching completion words, complete with
+#    prefix and suffix.
+__gitcomp_direct_append ()
+{
+	local IFS=$'\n'
+
+	COMPREPLY+=($1)
+}
+
 __gitcompappend ()
 {
 	local x i=${#COMPREPLY[@]}
@@ -787,7 +800,11 @@ __git_complete_refs ()
 		shift
 	done
 
-	__gitcomp_direct "$(__git_refs "$remote" "$dwim" "$pfx" "$cur_" "$sfx")"
+	__gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")"
+
+	if [ "$dwim" = "yes" ]; then
+		__gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
+	fi
 }
 
 # __git_refs2 requires 1 argument (to pass to __git_refs)
-- 
2.25.2
Previous: Jacob KellerNext: Jacob Keller
Message 8 of 26 in “refactor git switch completion”
  1. 00/11 refactor git switch completionJacob Keller, Apr 25, 2020
  2. 01/11 completion: add some simple test cases for git switch completionJacob Keller, Apr 25, 2020
  3. 02/11 completion: add test showing subpar git switch completionJacob Keller, Apr 25, 2020
  4. 03/11 completion: add test highlighting subpar git switch --track completionJacob Keller, Apr 25, 2020
  5. 04/11 completion: add tests showing lack of support for git switch -c/-CJacob Keller, Apr 25, 2020
  6. 09/11 completion: fix completion for git switch with no optionsJacob Keller, Apr 25, 2020
  7. 11/11 completion: complete remote branches for git switch --trackJacob Keller, Apr 25, 2020
  8. 08/11 completion: perform DWIM logic directly in __git_complete_refsJacob Keller, Apr 25, 2020
  9. 10/11 completion: recognize -c/-C when completing for git switchJacob Keller, Apr 25, 2020
  10. 07/11 completion: extract function __git_dwim_remote_headsJacob Keller, Apr 25, 2020
  11. 06/11 completion: rename --track option of __git_complete_refsJacob Keller, Apr 25, 2020
  12. 05/11 completion: remove completion for git switch --orphanJacob Keller, Apr 25, 2020
  13. 05/11 completion: add test showing subpar completion for git switch --orphanJacob Keller, Apr 25, 2020
  14. Junio C HamanoApr 27, 2020
  15. Jacob KellerApr 28, 2020
  16. Jacob KellerApr 28, 2020
  17. Junio C HamanoApr 28, 2020
  18. Jacob KellerApr 28, 2020
  19. Junio C HamanoApr 28, 2020
  20. Jacob KellerApr 28, 2020
  21. Junio C HamanoApr 28, 2020
  22. Jacob KellerApr 28, 2020
  23. Junio C HamanoApr 28, 2020
  24. Jacob KellerApr 25, 2020
  25. Junio C HamanoApr 30, 2020
  26. Jacob KellerMay 1, 2020

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.