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

Re: [PATCH v3] completion: add new _GIT_complete helper

From
SZEDER Gábor <szeder@ira.uka.de>
Date
May 6, 2012, 12:12 UTC
Message-ID
<20120506121204.GA5799@goldbirke>
In-Reply-To
<20120506111425.GJ2164@goldbirke>
Hi,
On Sun, May 06, 2012 at 01:14:25PM +0200, SZEDER Gábor wrote:
Show 36 quoted lines
> On Sat, May 05, 2012 at 05:23:20PM +0200, Felipe Contreras wrote:
> > +__git_func_wrap ()
> > +{
> > +	if [[ -n ${ZSH_VERSION-} ]]; then
> > +		emulate -L bash
> > +		setopt KSH_TYPESET
> > +
> > +		# workaround zsh's bug that leaves 'words' as a special
> > +		# variable in versions < 4.3.12
> > +		typeset -h words
> > +
> > +		# workaround zsh's bug that quotes spaces in the COMPREPLY
> > +		# array if IFS doesn't contain spaces.
> > +		typeset -h IFS
> > +	fi
> > +	local cur words cword prev
> > +	_get_comp_words_by_ref -n =: cur words cword prev
> > +	__git_func "$@"
> > +}
> > +
> > +_GIT_complete ()
> > +{
> > +	local name="${2-$1}"
> > +	eval "$(typeset -f __git_func_wrap | sed -e "s/__git_func/_$name/")"
> 
> Still don't like the subshell and sed here ...
> 
> > +	complete -o bashdefault -o default -o nospace -F _${name}_wrap $1 2>/dev/null \
> > +		|| complete -o default -o nospace -F _${name}_wrap $1
> > +}
> > +
> > +_GIT_complete git
> > +_GIT_complete gitk
> 
> ... because it adds delay when the completion script is loaded.  But I
> still don't have ideas how to avoid them.
Ok, I think I got it.  How about this on top of Felipe's patch?
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f300b87d..8c18db92 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2688,19 +2688,19 @@ __git_func_wrap ()
 	fi
 	local cur words cword prev
 	_get_comp_words_by_ref -n =: cur words cword prev
-	__git_func "$@"
+	$1
 }
 
 _GIT_complete ()
 {
-	local name="${2-$1}"
-	eval "$(typeset -f __git_func_wrap | sed -e "s/__git_func/_$name/")"
-	complete -o bashdefault -o default -o nospace -F _${name}_wrap $1 2>/dev/null \
-		|| complete -o default -o nospace -F _${name}_wrap $1
+	local wrapper="__git_wrap_$1"
+	eval "$wrapper () { __git_func_wrap $2 ; }"
+	complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
+		|| complete -o default -o nospace -F $wrapper $1
 }
 
-_GIT_complete git
-_GIT_complete gitk
+_GIT_complete git _git
+_GIT_complete gitk _gitk
 
 # The following are necessary only for Cygwin, and only are needed
 # when the user has tab-completed the executable name and consequently


The point is that __git_func_wrap() is not a template function
processed by _GIT_complete() (or whatever we'll end up calling it)
anymore, but simply a wrapper function around existing completion
functions.  The name of the completion function to be invoked should
be given as argument.  _GIT_complete() then uses 'eval' to create
another wrapper function to invoke __git_func_wrap() with the name of
the desired completion function.  The name of this dynamically created
wrapper function is derived from the name of the command or alias,
i.e. for 'gf' it will be __git_wrap_gf().

The overhead of the additional function call is not even measureable,
while it would spare the overhead of fork()ing a subshell and
fork()+exec()ing 'sed' twice when loading the completion script and
then for each subsequent alias.


Best,
Gábor
Previous: SZEDER GáborNext: Felipe Contreras
Message 19 of 24 in “completion: add new _GIT_complete helper”
  1. completion: add new _GIT_complete helperFelipe Contreras, May 5, 2012
  2. Jonathan NiederMay 5, 2012
  3. Felipe ContrerasMay 5, 2012
  4. Jonathan NiederMay 5, 2012
  5. Jonathan NiederMay 5, 2012
  6. Felipe ContrerasMay 5, 2012
  7. Jonathan gives feedback --> flamewars inevitable? (Re: [PATCH v3] completion: add new _GIT_complete helper)Jonathan Nieder, May 5, 2012
  8. Felipe ContrerasMay 5, 2012
  9. Jonathan NiederMay 5, 2012
  10. Jonathan NiederMay 5, 2012
  11. Tay Ray ChuanMay 6, 2012
  12. Felipe ContrerasMay 14, 2012
  13. Jonathan NiederMay 5, 2012
  14. SZEDER GáborMay 6, 2012
  15. Jonathan NiederMay 6, 2012
  16. SZEDER GáborMay 6, 2012
  17. SZEDER GáborMay 6, 2012
  18. SZEDER GáborMay 6, 2012
  19. SZEDER GáborMay 6, 2012
  20. Felipe ContrerasMay 6, 2012
  21. Felipe ContrerasMay 6, 2012
  22. SZEDER GáborMay 6, 2012
  23. Felipe ContrerasMay 7, 2012
  24. SZEDER GáborMay 7, 2012

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.