34

If I run a regular git command such as git checkout I get helpful autocompletion of branch names when hitting the tab key.

I have a few git aliases which take branch names as parameters, and I'm wondering if there's a way of getting the branch name autocompletion to work with them?

Edit:

Just to provide some clarification from the discussion in the comments, aliases with a direct mapping work fine, i.e.:

ci = commit
co = checkout

It's ones that are a bit more involved and use $1 as a parameter that don't, for example:

tagarchive = !f() { git tag archive/$1 origin/$1 && git push origin :$1 && git push origin archive/$1 && git branch -d $1; }; f
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • 2
    The autocompletion does work on aliases, for me. Are you using bash? What system are you on? (OS, distro, package manager, etc.) And just checking, but by "git aliases" you do mean git config entries called alias.foo, right? – kini Jul 13 '12 at 09:47
  • @kini bash on Ubuntu. Git aliases as stored in `[alias]` in the config, yes. – bcmcfc Jul 13 '12 at 09:54
  • Can you show us these aliases? I have simple renaming aliases for `checkout`, and `whatchanged`, for example, and these aliases still work with tab completion (this is on Ubuntu as well, for what it's worth). – David Cain Jul 13 '12 at 10:33
  • 1
    @David That sheds some light on this - I have some renaming aliases too - I suspect that autocompletion stops working when the alias does a little more than a direct mapping. Aliases such as `co` (`checkout`) work fine for me. Updated the question. – bcmcfc Jul 13 '12 at 10:35
  • As it seems that your problem is more related to `bash` than `git`, you may find this link helpful: [Bash completion for aliased commands](http://ubuntuforums.org/showthread.php?t=733397). – Yannick Blondeau Jul 13 '12 at 11:02
  • Thanks for the question. I've learn a thing or two while searching for a solution. – Shawn Chin Jul 13 '12 at 11:56
  • 1
    Git completion should work better with Git 2.1 (August 2014). See [my answer below](http://stackoverflow.com/a/25099026/6309) – VonC Aug 02 '14 at 20:18

2 Answers2

37

For git aliases, the autocomplete function for the git command (__git()) uses a call to git config --get "alias.$1" to determine that equivalent autocomplete function. This works for simple mappings but will choke on more complex aliases.

To get around this, define an autocomplete function with a name that matches your alias, i.e. _git_tagarchive(). The autocomplete function for git should pick that up and use it for autocompletion.

For example:

[me@home]$ git tagarchive <TAB><TAB>
AUTHORS             gentleSelect/       .gitignore          LICENSE             test_multiple.html  
cron/               .git/               index.html          README.md  
[me@home]$ _git_tagarchive() {
> _git_branch  # reuse that of git branch
> }
[me@home]$ git tagarchive  <TAB><TAB>
enable_multiple          master                   origin/gh-pages          v0.1                     v0.1.3 
FETCH_HEAD               ORIG_HEAD                origin/HEAD              v0.1.1                   v0.1.3.1 
HEAD                     origin/enable_multiple   origin/master            v0.1.2 

For a more permanent solution simply add the function definition to your bashrc file. Eg:

_git_tagarchive() 
{
    _git_branch
}

Note that I've simply reused the autocomplete function for git branch; you may wish to change this to something more suitable or write your own.

More info

This solution was identified based on an exploration of /etc/bash_completion.d/git.

Typically, aliased git commands are handled by the __git_aliased_commands() function which parses the output of git config --get "alias.$1" to decide on the autocomplete function to use. Using a more complex shell command as the alias target would understandably foil this approach.

Looking further, it appears the autocomplete function for git (_git()) chains in autocomplete function for subcommands by simple prepending the function with _git_ (with dashes (-) in the command replaced by underscores). This is done before __git_aliased_command() is checked so this is something we could use.

_git ()
{ 
   # .....
   local completion_func="_git_${command//-/_}"
   declare -f $completion_func >/dev/null && $completion_func && return

   local expansion=$(__git_aliased_command "$command")
   if [ -n "$expansion" ]; then
       completion_func="_git_${expansion//-/_}"
       declare -f $completion_func >/dev/null && $completion_func
   fi

}

The approach I've gone for is therefore to ensure that a function that matches your alias exists, i.e. _git_tagarchive().

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
4

Update July 2015 (Git 2.5): getting the git aliases is easier in contrib/completion/git-completion.bash.

See commit 12bdc88, commit e8f9e42 (10 May 2015) by SZEDER Gábor (szeder).
(Merged by Junio C Hamano -- gitster -- in commit 935d937, 22 May 2015)

~~~~~~~~~~~~~~

Git completion should work better with complex aliases in Git 2.1 (August 2014).
See commit 56f24e8 by Steffen Prohaska (sprohaska)

completion: handle '!f() { ... }; f' and "!sh -c '...' -" aliases

'!f() { ... }; f' and "!sh -c '....' -" are recommended patterns for declaring more complex aliases (see git wiki).
This commit teaches the completion to handle them.

When determining which completion to use for an alias, an opening brace or single quote is now skipped, and the search for a git command is continued.
For example, the aliases '!f() { git commit ... }' or "!sh -c 'git commit ...'" now trigger commit completion.
Previously, the search stopped on the opening brace or quote, and the completion tried it to determine how to complete, which obviously was useless.

The null command ':' is now skipped, so that it can be used as a workaround to declare the desired completion style.

For example, the aliases:

!f() { : git commit ; if ... } f
!sh -c ': git commit; if ...' -

now trigger commit completion.

Shell function declarations now work with or without space before the parens, i.e. '!f() ...' and '!f () ...' both work.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • For me using git v2.17.1 it weren't that simple. I couldn't use `;` char within the value of the alias in any way (even escaped). I ended up using `&&` instead - different meaning, but effectively no difference in combination w/ the null command. – helvete Oct 31 '18 at 11:28