threads / discuss / 53537

git git<tab> completes non-existent command `git gitk`

Subject: git git<tab> completes non-existent command `git gitk`

## tl;dr

4 messages between May 23, 2020 and May 25, 2020.

replies: 3people: 3as markdown or json

Anthony Sottile· May 23, 2020, 22:25 UTC · lore
easiest to reproduce is with docker
```dockerfile
FROM ubuntu:focal
RUN : \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y
--no-install-recommends \
        bash-completion \
        git \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*
```

```console $ docker run --rm -ti test bash root@23e691ecc7ba:/# [ -f /etc/bash_completion ] && . /etc/bash_completion root@23e691ecc7ba:/# git gitk ```

(I typed git git<tab>)

```console $ git gitk git: 'gitk' is not a git command. See 'git --help'. ```

this is a bit annoying because I have some aliases/commands for git-github-*
the git version I have is 2.25.1:

```console $ git --version git version 2.25.1 ```

Anthony
Matheus Tavares· May 24, 2020, 21:38 UTC · re: Anthony Sottile · lore

Re: git git<tab> completes non-existent command `git gitk`

Hi, Anthony
On Sat, May 23, 2020 at 7:25 PM Anthony Sottile <asottile@umich.edu> wrote:
Show 30 quoted lines
>
> easiest to reproduce is with docker
>
> ```dockerfile
> FROM ubuntu:focal
> RUN : \
>     && apt-get update \
>     && DEBIAN_FRONTEND=noninteractive apt-get install -y
> --no-install-recommends \
>         bash-completion \
>         git \
>     && apt-get clean \
>     && rm -rf /var/lib/apt/lists/*
> ```
>
> ```console
> $ docker run --rm -ti test bash
> root@23e691ecc7ba:/# [ -f /etc/bash_completion ] && . /etc/bash_completion
> root@23e691ecc7ba:/# git gitk
> ```
>
> (I typed git git<tab>)
>
> ```console
> $ git gitk
> git: 'gitk' is not a git command. See 'git --help'.
> ```
> this is a bit annoying because I have some aliases/commands for git-github-*
>
> the git version I have is 2.25.1:

Thanks for the report. This is also reproducible in Git 2.27.0-rc1. To complete subcommands, git-completion.bash uses[1] the output of:

	git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config

And the list returned by the line above contains "gitk", because this command is in the "mainporcelain" category (in command-list.txt).

One possible solution to the invalid completion you mentioned, without having to re-categorize "gitk", is to explicitly exclude it in git-completion.bash:

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b1d6e5ebed..f07394584f 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3214,7 +3214,10 @@ __git_main ()
 			then
 				__gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
 			else
-				__gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
+				local cmds="$(__git --list-cmds=list-mainporcelain,others,nohelpers,list-complete,config | \
+						sed -e '/^gitk$/d')"
+				local aliases="$(__git --list-cmds=alias,config)"
+				__gitcomp "$cmds $aliases"
 			fi
 			;;
 		esac

(I had to split the list into "cmds" and "aliases" so that we could still give
completion for a valid "git gitk" alias, if present.)

This should solve the problem, althought it's admittedly not very elegant...
Nevertheless, I'd be happy to send a complete patch if folks are happy with the
workaround.

In the meantime, you could use:

	git config completion.commands -gitk

To locally remove the completion for "gitk".

Thanks,
Matheus

[1]: See contrib/completion/git-completion.bash +3218
     (https://github.com/git/git/blob/master/contrib/completion/git-completion.bash#L3218)
Junio C Hamano· May 25, 2020, 00:27 UTC · re: Matheus Tavares · lore

Re: git git<tab> completes non-existent command `git gitk`

Matheus Tavares <matheus.bernardino@usp.br> writes:
> This should solve the problem, althought it's admittedly not very elegant...
> Nevertheless, I'd be happy to send a complete patch if folks are happy with the
> workaround.
Please don't.
Show 5 quoted lines
> In the meantime, you could use:
>
> 	git config completion.commands -gitk
>
> To locally remove the completion for "gitk".

I think an equivalent of this in-core would be the way to go. I wonder if there are any commands other than gitk that cannot be invoked as a subcommand of "git" potty.

t/t9902-completion.sh would be a good place to test this.
Matheus Tavares Bernardino· May 25, 2020, 01:53 UTC · re: Junio C Hamano · lore

Re: git git<tab> completes non-existent command `git gitk`

On Sun, May 24, 2020 at 9:27 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 10 quoted lines
>
> Matheus Tavares <matheus.bernardino@usp.br> writes:
>
> > In the meantime, you could use:
> >
> >       git config completion.commands -gitk
> >
> > To locally remove the completion for "gitk".
>
> I think an equivalent of this in-core would be the way to go.

I'm not sure I understand the idea, but would that be something along these lines? Adding a "nocomplete" category in commands-list.txt, to do the opposite of the "complete" we already have[1]. Then adding a new group name to "--list-cmds" (such as "--list-cmds=completeonly"), which would include the commands that are in "complete" and exclude those in "nocomplete"? We then might be able to complete subcommands with the output of:

git --list-cmds=list-mainporcelain,others,nohelpers,alias,completeonly,config

[1]: "nocomplete" would only be relevant in conjunction with "list-mainporcelain", as the commands in the latter (as gitk) are currently completed by default (even without "complete").

> I wonder if there are any commands other than gitk that cannot be
> invoked as a subcommand of "git" potty.

In the list suggested by git-completion, I think gitk is the only one (if I haven't missed any).

← back to recent threads