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

Re: [RESEND PATCH 2/3] git-completion.bash: fix `git <args>... stash branch` bug

From
Denton Liu <liu.denton@gmail.com>
Date
Mar 19, 2021, 08:05 UTC
Message-ID
<YFRbM1st0yINtScF@generichostname>
In-Reply-To
<xmqqsg4sryq9.fsf@gitster.g>
Hi Junio,
On Thu, Mar 18, 2021 at 01:30:38PM -0700, Junio C Hamano wrote:
Show 31 quoted lines
> Denton Liu <liu.denton@gmail.com> writes:
> 
> > When completions are offered for `git stash branch<TAB>`, the user is
> > supposed to receive refs. This works in the case where the main git
> > command is called without arguments but if options are provided, such as
> > `git -C dir stash branch<TAB>`, then the `$cword -eq 3` provides
> > incorrect results.
> >
> > Count the words relative to the first instance of "stash" so that we
> > ignore arguments to the main git command.
> >
> > Unfortunately, this still does not work 100% correctly. For example, in
> > the case of something like `git -C stash stash branch<TAB>`, this will
> > incorrectly identify the first "stash" as the command. This seems to be
> > an edge-case that we can ignore, though, as other functions, such as
> > _git_worktree(), suffer from the same problem.
> 
> I am not familiar with how the completion support works, but doing
> this inside _git_stash() and still not being able to tell which
> "stash" on the command line is supposed to be the git subcommand
> smells quite fishy to me.  
> 
> How did the caller decide to invoke _git_stash helper function in
> the first place?
> 
> When it is given "git -C push --paginate stash branch<TAB>", it must
> have parsed the command line, past the options given to the "git"
> potty, to find "stash" on the command line that it is _git_stash and
> not _git_push that needs to be called, no?  If it were possible to
> propagate that information without losing it, then we do not have to
> recompute where the subcommand name is at all, do we?

Good observation. _git_stash() is called in the body of __git_complete_command() which is called by __git_main(). There is currently no mechanism by which to pass the index of the command over to _git_*() completion functions.

That being said, passing in the index to all functions would definitely be doable. I can work on a series in the future that passes in the index of the command so that working with $cword is more robust but I'd prefer if that were handled outside this series to keep it focused.

Thanks, Denton

Previous: Junio C HamanoNext: Junio C Hamano
Message 8 of 22 in “git-completion.bash: improvements to _git_stash()”
  1. 0/3 git-completion.bash: improvements to _git_stash()Denton Liu, Mar 16, 2021
  2. 1/3 git-completion.bash: extract from else in _git_stash()Denton Liu, Mar 16, 2021
  3. 2/3 git-completion.bash: fix `git <args>... stash branch` bugDenton Liu, Mar 16, 2021
  4. 3/3 git-completion.bash: use __gitcomp_builtin() in _git_stash()Denton Liu, Mar 16, 2021
  5. 0/3 git-completion.bash: improvements to _git_stash()Denton Liu, Mar 18, 2021
  6. 2/3 git-completion.bash: fix `git <args>... stash branch` bugDenton Liu, Mar 18, 2021
  7. Junio C HamanoMar 18, 2021
  8. Denton LiuMar 19, 2021
  9. Junio C HamanoMar 19, 2021
  10. 1/3 git-completion.bash: extract from else in _git_stash()Denton Liu, Mar 18, 2021
  11. 3/3 git-completion.bash: use __gitcomp_builtin() in _git_stash()Denton Liu, Mar 18, 2021
  12. Junio C HamanoMar 18, 2021
  13. Denton LiuMar 19, 2021
  14. Junio C HamanoMar 19, 2021
  15. 0/3 git-completion.bash: improvements to _git_stash()Denton Liu, Mar 24, 2021
  16. 1/3 git-completion.bash: pass $__git_subcommand_idx from __git_main()Denton Liu, Mar 24, 2021
  17. SZEDER GáborMar 27, 2021
  18. SZEDER GáborMar 28, 2021
  19. 3/3 git-completion.bash: use __gitcomp_builtin() in _git_stash()Denton Liu, Mar 24, 2021
  20. SZEDER GáborMar 28, 2021
  21. 2/3 git-completion.bash: extract from else in _git_stash()Denton Liu, Mar 24, 2021
  22. SZEDER GáborMar 28, 2021

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.