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

Autocompletion - commands no longer work as stand alone

From
Nathan Bullock <nathanbullock@gmail.com>
Date
Jan 20, 2012, 16:32 UTC
Message-ID
<CAPx=Vfqj3UZuFv3Xmupy7k9arUxyZJyprm628p9QVKabdOz8cw@mail.gmail.com>
In-Reply-To
<CAPx=Vfp_HVr5W1fFic_1k+JsKr2RAKd-RK=VkfSgo7qkb5GsAw@mail.gmail.com>
I have for a number of years had the following in my .bashrc

alias br="git branch" complete -F _git_branch br

As well as similar commands for co and log.

Recently though this broke, now when I type something like "br mas<command completion>" it will occasionally complain with messages like: bash: [: 1: unary operator expected

From digging through the source it looks like this was broken back in April. (The commit is show at the bottom of this email.)

So my questions are:
1. Is it reasonable for things like _git_branch to work as a
standalone autocompletion function instead of having to go through
_git? I certainly like it to work as a standalone function. I also use
it to add autocompletion to other bash scripts that I use frequently.
2. If I add code that verifies that the variable cword exists at the
start of these functions and only if not call something like
_get_comp_words_by_ref -n =: cur words cword prev. Would that be
reasonable? I think this should address the performance concerns that
caused these to be removed in the first place, but it may make the
code uglier.

I have already added wrapper functions in my bashrc so that this is no longer a problem for me, but there may be other people who start hitting this as well once they start using newer versions of git.

Nathan
commit da4902a73017ad82b9926d03101ec69a2802d1e7
Author: SZEDER Gábor <szeder@ira.uka.de>
Date:   Thu Apr 28 18:01:52 2011 +0200
   completion: remove unnecessary _get_comp_words_by_ref() invocations
   In v1.7.4-rc0~11^2~2 (bash: get --pretty=m<tab> completion to work
   with bash v4, 2010-12-02) we started to use _get_comp_words_by_ref()
   to access completion-related variables.  That was large change, and to
   make it easily reviewable, we invoked _get_comp_words_by_ref() in each
   completion function and systematically replaced every occurance of
   bash's completion-related variables ($COMP_WORDS and $COMP_CWORD) with
   variables set by _get_comp_words_by_ref().
   This has the downside that _get_comp_words_by_ref() is invoked several
   times during a single completion.  The worst offender is perhaps 'git
   log mas<TAB>': during the completion of 'master'
   _get_comp_words_by_ref() is invoked no less than six times.
   However, the variables $prev, $cword, and $words provided by
   _get_comp_words_by_ref() are not modified in any of the completion
   functions, and the previous commit ensures that the $cur variable is
   not modified as well.  This makes it possible to invoke
   _get_comp_words_by_ref() to get those variables only once in our
   toplevel completion functions _git() and _gitk(), and all other
   completion functions will inherit them.
   Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
   Signed-off-by: Junio C Hamano <gitster@pobox.com>
Next: Junio C Hamano
Message 1 of 6 in “Autocompletion - commands no longer work as stand alone”
  1. Nathan BullockJan 20, 2012
  2. Junio C HamanoJan 20, 2012
  3. Nathan BullockJan 20, 2012
  4. SZEDER GáborJan 24, 2012
  5. Nathan BullockJan 30, 2012
  6. Junio C HamanoJan 30, 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.