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

Re: [PATCH v2/RFC] Make git-completion Bash 4 compatible.

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Oct 27, 2010, 22:39 UTC
Message-ID
<20101027223918.GA1877@neumann>
In-Reply-To
<20101027131506.4da06c6d@MonteCarlo>
Hi,
On Wed, Oct 27, 2010 at 01:15:06PM -0400, Peter van der Does wrote:
> The completion script does not work as expected under Bash 4.

Thank you. I applied your patch to play around a bit, and it seems to work (but, of course, I have not tried all the affected commands).

I agree with the comments and suggestiong of Brian and Jonathan. The current commit message covers only the symptoms of a bug this patch attempts to fix, but it should also explain its cause and how the patch is supposed to fix it. Unfortunately, I can only comment on the latter: if these new functions are good enough for the folks over at bash-completion project, then they should be good enough for us (;

I'm still puzzled that the only relevant entry I could find in the bash NEWS file is:

i.  The programmable completion code now uses the same set of characters as
    readline when breaking the command line into a list of words.

Yet, as I mentioned in one of the previous threads, I have two machines with different bash versions (3.2 and 4.1) but with the exact same set of characters in COMP_WORDBREAKS, and they show different behavior.

Show 14 quoted lines
> Bash: 3
> output:
> $ git log --pretty=<tab><tab>
> email     full      medium    raw
> format:   fuller    oneline   short
> 
> Bash: 4
> output:
> $ git log --pretty=<tab><tab>
> .bash_logout         .local/
> .bash_profile        Music/
> --More--
> 
> Signed-off-by: Peter van der Does <peter@avirtualhome.com>
Show 8 quoted lines
> @@ -556,11 +799,14 @@ __git_complete_revlist ()
>  
>  __git_complete_remote_or_refspec ()
>  {
> -	local cmd="${COMP_WORDS[1]}"
> -	local cur="${COMP_WORDS[COMP_CWORD]}"
> +	local git_comp_words git_comp_cword
> +	__reassemble_comp_words_by_ref : git_comp_words git_comp_cword

I suggest using '_get_comp_words_by_ref -n ":" words cword' here. First, for consistency's sake, because you use _get_comp_words_by_ref() everywhere else, too (almost). Second, I'm worried about the double underscore prefix in the function name, because it usually indicates something that is not supposed to be used directly from outside. Even the bash-completion project's scripts prefer _get_comp_words_by_ref() to __reassemble_comp_words_by_ref(). And third, ...

Show 6 quoted lines
> +	local cmd="${git_comp_words[1]}"
> +	local cur
>  	local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
> -	while [ $c -lt $COMP_CWORD ]; do
> -		i="${COMP_WORDS[c]}"
> +	_get_comp_words_by_ref -n ":" cur

... you could then join this _get_comp_words_by_ref() invocation with the one above.

Show 5 quoted lines
> +	while [ $c -lt $git_comp_cword ]; do
> +		i="${git_comp_words[c]}"
>  		case "$i" in
>  		--mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
>  		--all)
Show 7 quoted lines
> @@ -824,7 +1072,10 @@ __git_whitespacelist="nowarn warn error error-all fix"
>  
>  _git_am ()
>  {
> -	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
> +	local cur
> +	local dir="$(__gitdir)"
You could keep the two declarations in a single line.
Show 5 quoted lines
> +
> +	_get_comp_words_by_ref -n "=" cur
>  	if [ -d "$dir"/rebase-apply ]; then
>  		__gitcomp "--skip --continue --resolved --abort"
>  		return
Show 6 quoted lines
> @@ -929,7 +1183,8 @@ _git_bisect ()
>  
>  _git_branch ()
>  {
> -	local i c=1 only_local_ref="n" has_r="n"
> +	local i c=1 only_local_ref="n" has_r="n", cur
The comma before "cur" shouldn't be there.
> +	_get_comp_words_by_ref cur
>  
>  	while [ $c -lt $COMP_CWORD ]; do
>  		i="${COMP_WORDS[c]}"
Hmph.

I don't think there is anything that could go wrong here with respect to word breaking, but at least for consistency's sake you could query for words and cword, too, and use ${words[cword]} here.

Show 7 quoted lines
> @@ -1006,7 +1262,8 @@ _git_cherry ()
>  
>  _git_cherry_pick ()
>  {
> -	local cur="${COMP_WORDS[COMP_CWORD]}"
> +	local cur
> +_get_comp_words_by_ref cur
Missing indentation.
>  	case "$cur" in
>  	--*)
>  		__gitcomp "--edit --no-commit"
Show 8 quoted lines
> @@ -1606,9 +1886,11 @@ _git_stage ()
>  
>  __git_config_get_set_variables ()
>  {
> -	local prevword word config_file= c=$COMP_CWORD
> +	local prevword word config_file= c
> +	local git_comp_words
> +	__reassemble_comp_words_by_ref = git_comp_words c
Use _get_comp_words_by_ref() instead.
Show 6 quoted lines
>  	while [ $c -gt 1 ]; do
> -		word="${COMP_WORDS[c]}"
> +		word="${git_comp_words[c]}"
>  		case "$word" in
>  		--global|--system|--file=*)
>  			config_file="$word"
Show 7 quoted lines
> @@ -2045,7 +2327,8 @@ _git_reset ()
>  {
>  	__git_has_doubledash && return
>  
> -	local cur="${COMP_WORDS[COMP_CWORD]}"
> +	local cur
> +_get_comp_words_by_ref cur
Missing indentation.
>  	case "$cur" in
>  	--*)
>  		__gitcomp "--merge --mixed --hard --soft --patch"
Show 15 quoted lines
> @@ -2343,15 +2634,16 @@ _git_whatchanged ()
>  
>  _git ()
>  {
> -	local i c=1 command __git_dir
> +	local i c=1 command __git_dir git_comp_words git_comp_cword
>  
>  	if [[ -n $ZSH_VERSION ]]; then
>  		emulate -L bash
>  		setopt KSH_TYPESET
>  	fi
>  
> -	while [ $c -lt $COMP_CWORD ]; do
> -		i="${COMP_WORDS[c]}"
> +	__reassemble_comp_words_by_ref = git_comp_words git_comp_cword
Use _get_comp_words_by_ref() instead.
Show 5 quoted lines
> +	while [ $c -lt $git_comp_cword ]; do
> +		i="${git_comp_words[c]}"
>  		case "$i" in
>  		--git-dir=*) __git_dir="${i#--git-dir=}" ;;
>  		--bare)      __git_dir="." ;;
Previous: Peter van der DoesNext: Jonathan Nieder
Message 10 of 11 in “Make git-completion Bash 4 compatible.”
  1. Make git-completion Bash 4 compatible.Peter van der Does, Oct 27, 2010
  2. Brian GernhardtOct 27, 2010
  3. Jonathan NiederOct 27, 2010
  4. SZEDER GáborOct 27, 2010
  5. Peter van der DoesOct 28, 2010
  6. Jonathan NiederOct 28, 2010
  7. Peter van der DoesOct 28, 2010
  8. Jakub NarebskiOct 28, 2010
  9. Peter van der DoesOct 28, 2010
  10. SZEDER GáborOct 27, 2010
  11. Jonathan NiederOct 28, 2010

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.