git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Tim Jaacks <timjaacks@posteo.de>
To: "SZEDER Gábor" <szeder.dev@gmail.com>
Cc: <git@vger.kernel.org>
Subject: Re: Bash completion for git aliases containing nested subcommands
Date: Mon, 03 Oct 2022 15:11:59 +0000	[thread overview]
Message-ID: <1839e676218.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de> (raw)
In-Reply-To: <1839e62f930.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de>

Hi Gabor,

thanks a lot for your detailed reply! I tried that and it works, thank you.

Just another small follow-up question, out of curiosity: I noticed that zsh 
handles the alias completion correctly out of the box (using the exact same 
gitconfig file). I don't have any dedicated zsh completion file, just 
installed zsh via apt. Do you know why and how zsh handles this differently?

Kind regards,
Tim

Am 3. Oktober 2022 16:24:41 schrieb SZEDER Gábor <szeder.dev@gmail.com>:

> On Mon, Oct 03, 2022 at 11:45:51AM +0000, Tim Jaacks wrote:
>> Hello everyone,
>>
>> I have set up the following alias in my .gitconfig file:
>>
>> [alias]
>> ss = stash show
>>
>> Unfortunately bash completion does not work correctly on this alias. When I
>> type "git ss <TAB>", I get:
>>
>> $ git ss <TAB>
>> apply clear drop pop show
>> branch create list push
>>
>> Which is obviously the completion for "git stash" instead of "git stash
>> show".
>>
>> With the original command I get the list of available stashes:
>>
>> $ git stash show <TAB>
>> stash@{0} stash@{1}
>>
>> Is there a way to get the completion on the alias behave like on the
>> original command?
>
> In general: no.  Our Bash completion script is organized as one
> _git_cmd() function for each git supported command.  If a command has
> subcommands, then its completion function looks for any of its
> subcommands amond the words on the command line, and takes the
> appropriate action, which is usually executing a particular arm of a
> case statement.  The two main issues are that in case of such an alias
> there is no subcommand ("show") on the command line, and there is no
> dedicated function to handle only the completion for 'git stash show'.
>
> However, it is possible to make completion work for your particular
> alias by using our completion script's extension mechanism that allows
> users to specify completion functions to their own git commands.  If
> you type 'git foo <TAB>' and there is a _git_foo() function in your
> shell's environment, then the completion script will invoke that
> function to perform completion; this works not only for commands but
> for aliases as well.  So for your alias you only need to "borrow" all
> the "show"-subcommand-specific case arms from _git_stash() and place
> them in a _git_ss() function, e.g. like this:
>
> _git_ss () {
> case "$cur" in
> --*)
> __gitcomp_builtin stash_show "$__git_diff_common_options"
> ;;
> *)
> __gitcomp_nl "$(__git stash list | sed -n -e 's/:.*//p')"
> ;;
> esac
> }
>
> Add it to your ~/.bashrc, or to a separate file that you source from
> your .bashrc;  If you use bash-completion, then you don't even have to
> touch you .bashrc: save that function to a file 'git-ss' (dash, not
> underscore!) in one of the directories scanned by bash-completion
> ($BASH_COMPLETION_USER_DIR, ~/.local/share/bash-completion/completions
> or its XDG_DATA_HOME-equivalent) and it will be auto-loaded as needed.
>
> This approach should work just as well for any other similar "command
> subcommand" alias that you might have; with the downside that you'll
> have to write all these functions yourself.
>
>> I am on Ubuntu 20.04 and using the distro's default git completions.
>>
>> Kind regards,
>> Tim




  parent reply	other threads:[~2022-10-03 15:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-03 11:45 Bash completion for git aliases containing nested subcommands Tim Jaacks
2022-10-03 14:24 ` SZEDER Gábor
     [not found]   ` <1839e62f930.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de>
2022-10-03 15:11     ` Tim Jaacks [this message]
2022-10-03 15:43     ` SZEDER Gábor
2022-10-03 22:24   ` Jeff King
2022-10-04 11:21     ` Tim Jaacks
2022-10-05 10:05     ` SZEDER Gábor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1839e676218.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de \
    --to=timjaacks@posteo.de \
    --cc=git@vger.kernel.org \
    --cc=szeder.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).