9

I have implemented a custom git command by writing a shell script located in /usr/local/bin. It works fine, but I would like the script to autocomplete branches in the command line, just like git checkout [TAB][TAB]. How could this be done?

EDIT: Just adding some context: git allows you to very easily add your own commands by creating a script git-$subcommandName. In my case here git-install, the script facilitates checking out a branch, building, packaging, and installing the source code.

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
primitivist
  • 805
  • 10
  • 18
  • Well, the required things would include to understand the full plethora of git commands and their confusing in-consistent usage of switches and parameters. To then implement some kind of lookahead search on that data. But I am wondering: why are you re-inventing the wheel? git has full support for bash autocompletion; so what makes you think that your re-invention of the wheel would be of interest to anybody? Dont get me wrong: this is an interesting project. But it would require considerable efforts ... and you would probably be always "not as good" as the original... – GhostCat Dec 23 '16 at 20:39
  • 2
    Not really reinventing the wheel here, my custom command "git install" takes a branch name, which it checks out (so yes, same as git checkout), but it then does a series of steps to package and install the source onto a compute cluster. – primitivist Dec 23 '16 at 20:43
  • maybe a git alias could be easier to implement? – pedrorijo91 Dec 24 '16 at 09:19
  • 1
    @pedrorijo91 aliases only allow you to reuse a preexisting git commands, which would not work for me. I don't think my question was clear so I just added an edit. – primitivist Dec 24 '16 at 15:33
  • 4
    I nominated this question for reopening - it's a perfectly succinct question about a very specific need, with broad applicability. – Ken Williams Sep 28 '17 at 21:06
  • 1
    git alias can be used for other things, you can have an alias such as `!sh do_foo`. – thecoshman Oct 11 '18 at 13:47
  • 4
    @KenWilliams, glad you did. I think those who voted to close should re-examine the question. :-( Even the unedited question isn't asking to re-implement all of git's bash autocompletion. (That would be insane!) It's very clear that it's limited in scope to a user-added custom command. The original poster even gave a very short answer that works perfectly, and gives a jumping off point for others implementing their own custom commands. – user1902689 Nov 06 '18 at 09:27

1 Answers1

12

Figured it out. I needed to download git bash completion (here), create a new file in /etc/bash_completion.d with the following contents:

source ~/.git-completion.bash
_git_install ()
{
  __gitcomp_nl "$(__git_refs)"
}

and then exec bash to reload completion scripts.

primitivist
  • 805
  • 10
  • 18
  • 4
    Note if your custom git command has a dash in it (i.e. `log-diff`) then the bash function you create must replace the dash with an underscore (i.e. `_git_log_diff()`.) – user1902689 Nov 06 '18 at 09:10
  • 2
    I'll also add that to simply duplicate the exact completion as another command, the body of the bash function can just call another. Not what you want here, but if you did, the body of the bash function could just be `_git_checkout "$@"`. Useful if you're making a custom command like `log-diff` and want it to complete identically to how `log` does. – user1902689 Nov 06 '18 at 09:36
  • 1
    There's no need to pass arguments, just `_git_install() { _git_checkout ; }`. – FelipeC Jun 25 '19 at 16:17
  • The answer doesn't mention what the name of the file in `/etc/bash_completion.d/` needs to be. From some experimentation it looks like it doesn't matter, and you can put multiple functions inside the same file, so you can just name it `git-commands` and have the one source line at the top and then a bunch of bash functions for each of your git functions. – Nickolai Feb 25 '20 at 20:01
  • However, in some systems (e.g., Ubuntu 20.04), there is already an up-to-date file at e.g. `/usr/share/bash-completion/completions/git`. So, you can source that, instead of downloading one. – Stavros Feb 02 '23 at 12:39