threads / discuss / 28900

Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

Subject: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

## tl;dr

9 messages between Nov 10, 2011 and Nov 12, 2011.

replies: 8people: 5as markdown or json

Nathan Broadbent· Nov 10, 2011, 02:46 UTC · lore
Dear git mailing list,

I'm assigning the `_git_fetch` bash tab completion to the alias `gf`, with the following command:

    complete -o default -o nospace -F _git_fetch gf

The tab completion then works fine in git 1.7.0.4, but breaks on git 1.7.7.1, with the following error:

    -bash: [: 2: unary operator expected

Here is the related issue on github (the project is a set of git shortcuts): https://github.com/ndbroadbent/scm_breeze/issues/11

Thanks for your time, I'd really appreciate some help!

Regards, Nathan B

Junio C Hamano· Nov 10, 2011, 03:21 UTC · re: Nathan Broadbent · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

Nathan Broadbent <nathan.f77@gmail.com> writes:
Show 9 quoted lines
> Dear git mailing list,
>
> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
> with the following command:
>
>     complete -o default -o nospace -F _git_fetch gf
>
> The tab completion then works fine in git 1.7.0.4, but breaks on git
> 1.7.7.1, with the following error:

We have been cooking for 1.7.8 and have the first release candidate 1.7.8-rc1; could you try it and report what you find out?

Johannes Sixt· Nov 10, 2011, 07:09 UTC · re: Junio C Hamano · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

Am 11/10/2011 4:21, schrieb Junio C Hamano:
Show 14 quoted lines
> Nathan Broadbent <nathan.f77@gmail.com> writes:
> 
>> Dear git mailing list,
>>
>> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
>> with the following command:
>>
>>     complete -o default -o nospace -F _git_fetch gf
>>
>> The tab completion then works fine in git 1.7.0.4, but breaks on git
>> 1.7.7.1, with the following error:
> 
> We have been cooking for 1.7.8 and have the first release candidate
> 1.7.8-rc1; could you try it and report what you find out?

It looks like _git_fetch is not meant to be called directly. All git completions must go through _git.

See also this post:
http://thread.gmane.org/gmane.comp.version-control.msysgit/13310/focus=13335
-- Hannes
Nathan Broadbent· Nov 10, 2011, 08:52 UTC · re: Johannes Sixt · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

So, this is a feature, not a bug... Tab completion for aliases is really useful. It's important enough to me that I won't stop until I've found a solution. I can appreciate that _git_fetch is not currently meant to be called directly, but we found a way to utilize it when it previously worked. Perhaps the scope of these completion functions could be expanded to allow for aliases? I'll attempt to submit a patch if someone can give me approval.

Thanks, Nathan

On Thu, Nov 10, 2011 at 3:09 PM, Johannes Sixt <j.sixt@viscovery.net> wrote:
Show 25 quoted lines
>
> Am 11/10/2011 4:21, schrieb Junio C Hamano:
> > Nathan Broadbent <nathan.f77@gmail.com> writes:
> >
> >> Dear git mailing list,
> >>
> >> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
> >> with the following command:
> >>
> >>     complete -o default -o nospace -F _git_fetch gf
> >>
> >> The tab completion then works fine in git 1.7.0.4, but breaks on git
> >> 1.7.7.1, with the following error:
> >
> > We have been cooking for 1.7.8 and have the first release candidate
> > 1.7.8-rc1; could you try it and report what you find out?
>
> It looks like _git_fetch is not meant to be called directly. All git
> completions must go through _git.
>
> See also this post:
>
> http://thread.gmane.org/gmane.comp.version-control.msysgit/13310/focus=13335
>
> -- Hannes
SZEDER Gábor· Nov 10, 2011, 15:14 UTC · re: Nathan Broadbent · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

Hi,
 
[Please don't top-post.]
Show 11 quoted lines
> On Thu, Nov 10, 2011 at 3:09 PM, Johannes Sixt <j.sixt@viscovery.net> wrote:
> >
> > Am 11/10/2011 4:21, schrieb Junio C Hamano:
> > > Nathan Broadbent <nathan.f77@gmail.com> writes:
> > >
> > >> Dear git mailing list,
> > >>
> > >> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
> > >> with the following command:
> > >>
> > >>     complete -o default -o nospace -F _git_fetch gf
I assume you have an
  alias gf="git fetch"
somewhere, right?
> > >> The tab completion then works fine in git 1.7.0.4, but breaks on git
> > >> 1.7.7.1, with the following error:

I didn't actually tried, but I guess this is a side-effect of da4902a7 (completion: remove unnecessary _get_comp_words_by_ref() invocations, 2011-04-28), which was in v1.7.6. Since the clean-up in that commit we only call _get_comp_words_by_ref() in the top-level completion functions _git() and _gitk() to populate completion-related variables ($cur, $prev, $words, $cword), so invoking any _git_<cmd>() completion function directly causes an error or wrong behavior, because all those variables are empty.

Calling a completion function directly was not an issue earlier, because every _git_<cmd>() completion function invoked _get_comp_words_by_ref() to populate those variables, or in the pre-_get_comp_words_by_ref() times they just accessed the completion-related bash variables $COMP_WORDS and $COMP_CWORD directly.

On Thu, Nov 10, 2011 at 04:52:33PM +0800, Nathan Broadbent wrote:
Show 8 quoted lines
> So, this is a feature, not a bug... Tab completion for aliases is
> really useful. It's important enough to me that I won't stop until
> I've found a solution.
> I can appreciate that _git_fetch is not currently meant to be called
> directly, but we found a way to utilize it when it previously worked.
> Perhaps the scope of these completion functions could be expanded to
> allow for aliases? I'll attempt to submit a patch if someone can give
> me approval.

The quickest way would be to just revert da4902a7, but it would be the dirtiest, too: it would bring back a lot of redundant calls to _get_comp_words_by_ref() and it might have side-effects under zsh (but I didn't think this through).

It would be a bit more clever to revert only parts of da4902a7, i.e. to bring back _get_comp_words_by_ref() calls in _git_<cmd> completion functions but not in __git_<whatever>() helper functions. This way _git_<cmd>() functions would have their completion-related variables initialized even when called directly instead through _git(), and _get_comp_words_by_ref() would be called "only" twice during a single completion. But that's still one too many, and again: there can be issues with zsh.

Alternatively, you could easily create your own wrapper function around _git_fetch(), like this:

_gf () {
	local cur prev words cword
	_get_comp_words_by_ref -n =: cur prev words cword
	_git_fetch
}
However.

Having said all that, I'd like to point out that even if _git_fetch() didn't error out when called for the 'gf' alias, it still wouldn't work properly. After 'gf origin <TAB>' it offers the list of remotes again and it never offers refspecs, because it calls __git_complete_remote_or_refspec(), which

 - depends on the fact that there must be at least two words ('git'
   and 'fetch') on the command line before the remote, and
 - needs to know the git command (i.e. fetch, pull, or push) to offer
   the proper refspecs, but it can't find that out from your alias.

Best, Gábor

Scott Bronson· Nov 12, 2011, 08:08 UTC · re: SZEDER Gábor · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

2011/11/10 SZEDER Gábor <szeder@ira.uka.de>
Show 12 quoted lines
> > On Thu, Nov 10, 2011 at 3:09 PM, Johannes Sixt <j.sixt@viscovery.net> wrote:
> > > Am 11/10/2011 4:21, schrieb Junio C Hamano:
> > > > Nathan Broadbent <nathan.f77@gmail.com> writes:
> > > >> Dear git mailing list,
> > > >> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
> > > >> with the following command:
> > > >>     complete -o default -o nospace -F _git_fetch gf
> > > >> The tab completion then works fine in git 1.7.0.4, but breaks on git
> > > >> 1.7.7.1, with the following error:
>
> I didn't actually tried, but I guess this is a side-effect of da4902a7
> (completion: remove unnecessary _get_comp_words_by_ref() invocations,
I looked into it and this is exactly right.
Show 23 quoted lines
> Alternatively, you could easily create your own wrapper function
> around _git_fetch(), like this:
>
> _gf () {
>        local cur prev words cword
>        _get_comp_words_by_ref -n =: cur prev words cword
>        _git_fetch
> }
>
>
> However.
>
> Having said all that, I'd like to point out that even if _git_fetch()
> didn't error out when called for the 'gf' alias, it still wouldn't
> work properly.  After 'gf origin <TAB>' it offers the list of remotes
> again and it never offers refspecs, because it calls
> __git_complete_remote_or_refspec(), which
>
>  - depends on the fact that there must be at least two words ('git'
>   and 'fetch') on the command line before the remote, and
>
>  - needs to know the git command (i.e. fetch, pull, or push) to offer
>   the proper refspecs, but it can't find that out from your alias.

Very true. But if you tweak the completion variables, you can fool _git_fetch into working perfectly:

  _gf () {
      COMP_LINE="git fetch${COMP_LINE#gf}"
      let COMP_POINT+=7  # strlen('git fetch') - strlen('gf')
      COMP_WORDS=(git fetch "${COMP_WORDS[@]:1}")
      let COMP_CWORD+=1
      local cur words cword prev
      _get_comp_words_by_ref -n =: cur words cword prev
      _git_fetch
  }
Can anyone find a place where this would fail?

It would be pretty easy to write similar wrappers for _git_add, _git_branch, and all the rest. [*]

Is there any possibility for a full set of wrappers (with better
names) to be merged into the git completions?  A number of
peopl are disappointed that abbreviation completion doesn't
work anymore, myself included:
  https://github.com/bobthecow/git-flow-completion/issues/2
  https://github.com/ndbroadbent/scm_breeze/issues/11

I'm happy to write them if there's a chance they'd be merged. Thank you for all the work you've done on the completions Gábor!

    - Scott
* If I had more time, I'd be tempted to write a function that
would define all the wrapper functions.
    define_wrapper add, ga
    define_wrapper branch, gb
    define_wrapper fetch, gf
    ...
Nothing a little eval metaprogramming can't solve.  :)
Scott Bronson· Nov 12, 2011, 17:50 UTC · re: Scott Bronson · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

2011/11/12 Scott Bronson <bronson@rinspin.com>:
Show 19 quoted lines
> 2011/11/10 SZEDER Gábor <szeder@ira.uka.de>
>> > On Thu, Nov 10, 2011 at 3:09 PM, Johannes Sixt <j.sixt@viscovery.net> wrote:
>> > > Am 11/10/2011 4:21, schrieb Junio C Hamano:
>> > > > Nathan Broadbent <nathan.f77@gmail.com> writes:
>> > > >> Dear git mailing list,
>> > > >> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
>> > > >> with the following command:
>> > > >>     complete -o default -o nospace -F _git_fetch gf
>> > > >> The tab completion then works fine in git 1.7.0.4, but breaks on git
>> > > >> 1.7.7.1, with the following error:
>> I didn't actually tried, but I guess this is a side-effect of da4902a7
>> (completion: remove unnecessary _get_comp_words_by_ref() invocations,
>> ...
>> Alternatively, you could easily create your own wrapper function
>> around _git_fetch(), like this:
> Very true.  But if you tweak the completion variables, you can fool
> _git_fetch into working perfectly:
> * If I had more time, I'd be tempted to write a function that
> would define all the wrapper functions.

I couldn't stop thinking about it last night, I had to try it. Here's the result, seems to work great:

    __define_git_completion () {
    eval "
        _git_$2_shortcut () {
            COMP_LINE=\"git $2\${COMP_LINE#$1}\"
            let COMP_POINT+=$((4+${#2}-${#1}))
            COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
            let COMP_CWORD+=1
            local cur words cword prev
            _get_comp_words_by_ref -n =: cur words cword prev
            _git_$2
        }
    "
    }
    __git_shortcut () {
        type _git_$2_shortcut &>/dev/null || __define_git_completion $1 $2
        alias $1="git $2 $3"
        complete -o default -o nospace -F _git_$2_shortcut $1
    }
    __git_shortcut  ga    add
    __git_shortcut  gb    branch
    __git_shortcut  gba   branch -a
    __git_shortcut  gco   checkout
    __git_shortcut  gci   commit -v
    __git_shortcut  gcia  commit '-a -v'
    __git_shortcut  gd    diff
    __git_shortcut  gdc   diff --cached
    __git_shortcut  gds   diff --stat
    __git_shortcut  gf    fetch
    __git_shortcut  gl    log
    __git_shortcut  glp   log -p
    __git_shortcut  gls   log --stat

On Github: https://github.com/bronson/dotfiles/blob/731bfd951be68f395247982ba1fb745fbed2455c/.bashrc#L81

It would be nice to see the __define_git_completion function merged upstram. Possible?

    - Scott
Nathan Broadbent· Nov 12, 2011, 17:53 UTC · re: Scott Bronson · lore

Re: Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1

2011/11/13 Scott Bronson <bronson@rinspin.com>
Show 69 quoted lines
>
> 2011/11/12 Scott Bronson <bronson@rinspin.com>:
> > 2011/11/10 SZEDER Gábor <szeder@ira.uka.de>
> >> > On Thu, Nov 10, 2011 at 3:09 PM, Johannes Sixt <j.sixt@viscovery.net> wrote:
> >> > > Am 11/10/2011 4:21, schrieb Junio C Hamano:
> >> > > > Nathan Broadbent <nathan.f77@gmail.com> writes:
> >> > > >> Dear git mailing list,
> >> > > >> I'm assigning the `_git_fetch` bash tab completion to the alias `gf`,
> >> > > >> with the following command:
> >> > > >>     complete -o default -o nospace -F _git_fetch gf
> >> > > >> The tab completion then works fine in git 1.7.0.4, but breaks on git
> >> > > >> 1.7.7.1, with the following error:
> >> I didn't actually tried, but I guess this is a side-effect of da4902a7
> >> (completion: remove unnecessary _get_comp_words_by_ref() invocations,
> >> ...
> >> Alternatively, you could easily create your own wrapper function
> >> around _git_fetch(), like this:
> > Very true.  But if you tweak the completion variables, you can fool
> > _git_fetch into working perfectly:
> > * If I had more time, I'd be tempted to write a function that
> > would define all the wrapper functions.
>
> I couldn't stop thinking about it last night, I had to try it.  Here's the
> result, seems to work great:
>
>
>    __define_git_completion () {
>    eval "
>        _git_$2_shortcut () {
>            COMP_LINE=\"git $2\${COMP_LINE#$1}\"
>            let COMP_POINT+=$((4+${#2}-${#1}))
>            COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
>            let COMP_CWORD+=1
>
>            local cur words cword prev
>            _get_comp_words_by_ref -n =: cur words cword prev
>            _git_$2
>        }
>    "
>    }
>
>    __git_shortcut () {
>        type _git_$2_shortcut &>/dev/null || __define_git_completion $1 $2
>        alias $1="git $2 $3"
>        complete -o default -o nospace -F _git_$2_shortcut $1
>    }
>
>    __git_shortcut  ga    add
>    __git_shortcut  gb    branch
>    __git_shortcut  gba   branch -a
>    __git_shortcut  gco   checkout
>    __git_shortcut  gci   commit -v
>    __git_shortcut  gcia  commit '-a -v'
>    __git_shortcut  gd    diff
>    __git_shortcut  gdc   diff --cached
>    __git_shortcut  gds   diff --stat
>    __git_shortcut  gf    fetch
>    __git_shortcut  gl    log
>    __git_shortcut  glp   log -p
>    __git_shortcut  gls   log --stat
>
>
> On Github:
> https://github.com/bronson/dotfiles/blob/731bfd951be68f395247982ba1fb745fbed2455c/.bashrc#L81
>
> It would be nice to see the __define_git_completion function merged
> upstram. Possible?
>
>    - Scott
You are amazing!!! Thanks so much for this!
Nathan

← back to recent threads