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

Re: [PATCH 10/12] completion: add new git_complete helper

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Apr 11, 2012, 22:50 UTC
Message-ID
<20120411225046.GU2289@goldbirke>
In-Reply-To
<1333854479-23260-11-git-send-email-felipe.contreras@gmail.com>
Hi,
On Sun, Apr 08, 2012 at 06:07:57AM +0300, Felipe Contreras wrote:
Show 56 quoted lines
> This simplifies the completions, and makes it easier to define aliases:
> 
>  git_complete gf git_fetch
> 
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
>  contrib/completion/git-completion.bash |   69 +++++++++++++++-----------------
>  t/t9902-completion.sh                  |    2 +-
>  2 files changed, 33 insertions(+), 38 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 60ea224..6cf1d98 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -2616,21 +2616,6 @@ _git ()
>  {
>  	local i c=1 cmd cmd_pos __git_dir
>  
> -	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
>  	while [ $c -lt $cword ]; do
>  		i="${words[c]}"
>  		case "$i" in
> @@ -2683,22 +2668,6 @@ _git ()
>  
>  _gitk ()
>  {
> -	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
> -
Eliminating code duplication, great!
Show 11 quoted lines
>  	__git_has_doubledash && return
>  
>  	local g="$(__gitdir)"
> @@ -2719,16 +2688,42 @@ _gitk ()
>  	__git_complete_revlist
>  }
>  
> -complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
> -	|| complete -o default -o nospace -F _git git
> -complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
> -	|| complete -o default -o nospace -F _gitk gitk
More greatness.
> +foo_wrap ()

This git-completion-specific internal helper function will pollute the user's environment. It should get a __git_ prefix.

Show 20 quoted lines
> +{
> +	local cmd=foo_cmd cmd_pos=1
> +	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
> +	foo "$@"
> +}
> +
> +git_complete ()

Maybe this should also get a prefix, but I'm not sure, because this is, strictly speaking, not a bash completion function.

Show 10 quoted lines
> +{
> +	local name="${2-$1}"
> +	local cmd="${name#git_}"
> +	eval "$(typeset -f foo_wrap | sed -e "s/foo_cmd/$cmd/" -e "s/foo/_$name/")"
> +	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
Clever ;)

But it needs a subshell and a sed invocation, so Windows people might not be all too happy about this. Perhaps it's not a big deal in the default case, because it will be done only twice at startup time. However, if someone wants several aliases on Windows, it can cause noticeable delay. It's a startup speed vs. convenience trade-off. Anyway, I don't have any ideas how those fork()s and exec() could be avoided.

Best, Gábor

Previous: Felipe ContrerasNext: Felipe Contreras
Message 44 of 48 in “Bash completion rework”
  1. 00/12 Bash completion reworkFelipe Contreras, Apr 8, 2012
  2. 01/12 tests: add initial bash completion testsFelipe Contreras, Apr 8, 2012
  3. Junio C HamanoApr 8, 2012
  4. Jeff KingApr 8, 2012
  5. Junio C HamanoApr 8, 2012
  6. Jeff KingApr 8, 2012
  7. Jeff KingApr 8, 2012
  8. Andreas SchwabApr 8, 2012
  9. Jeff KingApr 8, 2012
  10. Felipe ContrerasApr 11, 2012
  11. Junio C HamanoApr 11, 2012
  12. Jeff KingApr 8, 2012
  13. Jonathan NiederApr 8, 2012
  14. Jeff KingApr 8, 2012
  15. Junio C HamanoApr 9, 2012
  16. John KeepingApr 8, 2012
  17. 02/12 completion: simplify __gitcompFelipe Contreras, Apr 8, 2012
  18. SZEDER GáborApr 8, 2012
  19. Felipe ContrerasApr 8, 2012
  20. Jonathan NiederApr 8, 2012
  21. Felipe ContrerasApr 8, 2012
  22. Jonathan NiederApr 8, 2012
  23. Felipe ContrerasApr 8, 2012
  24. Johannes SixtApr 9, 2012
  25. Felipe ContrerasApr 9, 2012
  26. Felipe ContrerasApr 8, 2012
  27. Jonathan NiederApr 8, 2012
  28. Junio C HamanoApr 9, 2012
  29. 03/12 completion: simplify __gitcomp_1Felipe Contreras, Apr 8, 2012
  30. 04/12 completion: trivial simplificationFelipe Contreras, Apr 8, 2012
  31. 05/12 completion: add missing global optionsFelipe Contreras, Apr 8, 2012
  32. John KeepingApr 8, 2012
  33. SZEDER GáborApr 8, 2012
  34. 06/12 tests: add more bash completion testsFelipe Contreras, Apr 8, 2012
  35. 07/12 completion: simplify command stuffFelipe Contreras, Apr 8, 2012
  36. SZEDER GáborApr 11, 2012
  37. Felipe ContrerasApr 11, 2012
  38. SZEDER GáborApr 11, 2012
  39. Felipe ContrerasApr 11, 2012
  40. Felipe ContrerasApr 12, 2012
  41. 08/12 completion: simplify _git_bundleFelipe Contreras, Apr 8, 2012
  42. 09/12 completion: calculate argument position properlyFelipe Contreras, Apr 8, 2012
  43. 10/12 completion: add new git_complete helperFelipe Contreras, Apr 8, 2012
  44. SZEDER GáborApr 11, 2012
  45. Felipe ContrerasApr 11, 2012
  46. 11/12 test: add tests for aliases in bash completionFelipe Contreras, Apr 8, 2012
  47. Felipe ContrerasApr 8, 2012
  48. 12/12 completion: rename _git and _gitkFelipe Contreras, Apr 8, 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.