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

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

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Nov 10, 2011, 15:14 UTC
Message-ID
<20111110151412.GA11479@goldbirke>
In-Reply-To
<CAPXHQbP4yCzZ96WEKuHsV_8Pny0MRzcLOY7qi5W3_L_5CnY0vA@mail.gmail.com>
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

Previous: Nathan BroadbentNext: Scott Bronson
Message 5 of 9 in “Bash tab completion for _git_fetch alias is broken on Git 1.7.7.1”
  1. Nathan BroadbentNov 10, 2011
  2. Junio C HamanoNov 10, 2011
  3. Johannes SixtNov 10, 2011
  4. Nathan BroadbentNov 10, 2011
  5. SZEDER GáborNov 10, 2011
  6. Scott BronsonNov 12, 2011
  7. Scott BronsonNov 12, 2011
  8. Nathan BroadbentNov 12, 2011
  9. Nathan BroadbentNov 10, 2011

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.