Contributed bash completion support for core Git tools.
PATCH 1/1 Contributed bash completion support for core Git tools.
This is a set of bash completion routines for many of the popular core Git tools. I wrote these routines from scratch after reading the git-compl and git-compl-lib routines available from the gitcompletion package at http://gitweb.hawaga.org.uk/
and found those to be lacking in functionality for some commands. Consequently there may be some similarities but many differences.
Since these are completion routines only for tools shipped with core Git and since bash is a popular shell on many of the native core Git platforms (Linux, Mac OS X, Solaris, BSD) including these routines as part of the stock package would probably be convienent for many users.
---
I put this together rather quickly today so although I find it to work well for me and be useful, others might not. User be warned.
In particular I have found the completion of remote refs and paths within 'ref:foo/path' style arguments to be very useful, even if there is some (mild) latency involved.
contrib/bash-git-completion.sh | 330 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 330 insertions(+), 0 deletions(-)
contrib/bash-git-completion.sh new file +330 -0
@@ -0,0 +1,330 @@ +# +# bash completion support for core Git. +# +# Copyright (C) 2006 Shawn Pearce +# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/). +# +# The contained completion routines provide support for completing: +# +# *) local and remote branch names +# *) local and remote tag names +# *) .git/remotes file names +# *) git 'subcommands' +# *) tree paths within 'ref:path/to/file' expressions +# +# To use these routines: +# +# 1) Copy this file to somewhere (e.g. ~/.git-completion.sh). +# 2) Added the following line to your .bashrc: +# source ~/.git-completion.sh +# + +__git_refs () +{ + local cmd i is_hash=y + if [ -d "$1" ]; then + cmd=git-peek-remote + else + cmd=git-ls-remote + fi + for i in $($cmd "$1" 2>/dev/null); do + case "$is_hash,$i" in + y,*) is_hash=n ;; + n,*^{}) is_hash=y ;; + n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;; + n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;; + n,*) is_hash=y; echo "$i" ;; + esac + done +} + +__git_refs2 () +{ + local cmd i is_hash=y + if [ -d "$1" ]; then + cmd=git-peek-remote + else + cmd=git-ls-remote + fi + for i in $($cmd "$1" 2>/dev/null); do + case "$is_hash,$i" in + y,*) is_hash=n ;; + n,*^{}) is_hash=y ;; + n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}:${i#refs/tags/}" ;; + n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;; + n,*) is_hash=y; echo "$i:$i" ;; + esac + done +} + +__git_remotes () +{ + local i REVERTGLOB=$(shopt -p nullglob) + shopt -s nullglob + for i in .git/remotes/*; do + echo ${i#.git/remotes/} + done + $REVERTGLOB +} + +__git_complete_file () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + ?*:*) + local pfx ls ref="$(echo "$cur" | sed 's,:.*$,,')" + cur="$(echo "$cur" | sed 's,^.*:,,')" + case "$cur" in + ?*/*) + pfx="$(echo "$cur" | sed 's,/[^/]*$,,')" + cur="$(echo "$cur" | sed 's,^.*/,,')" + ls="$ref:$pfx" + pfx="$pfx/" + ;; + *) + ls="$ref" + ;; + esac + COMPREPLY=($(compgen -P "$pfx" \ + -W "$(git-ls-tree "$ls" \ + | sed '/^100... blob /s,^.* ,, + /^040000 tree /{ + s,^.* ,, + s,$,/, + } + s/^.* //')" \ + -- "$cur")) + ;; + *) + COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) + ;; + esac +} + +_git_branch () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs .)" -- "$cur")) +} + +_git_cat_file () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "${COMP_WORDS[0]},$COMP_CWORD" in + git-cat-file*,1) + COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur")) + ;; + git,2) + COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur")) + ;; + *) + __git_complete_file + ;; + esac +} + +_git_checkout () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "-l -b $(__git_refs .)" -- "$cur")) +} + +_git_diff () +{ + __git_complete_file +} + +_git_diff_tree () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "-r -p -M $(__git_refs .)" -- "$cur")) +} + +_git_fetch () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + + case "${COMP_WORDS[0]},$COMP_CWORD" in + git-fetch*,1) + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) + ;; + git,2) + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) + ;; + *) + case "$cur" in + *:*) + cur=$(echo "$cur" | sed 's/^.*://') + COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) + ;; + *) + local remote + case "${COMP_WORDS[0]}" in + git-fetch) remote="${COMP_WORDS[1]}" ;; + git) remote="${COMP_WORDS[2]}" ;; + esac + COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur")) + ;; + esac + ;; + esac +} + +_git_ls_remote () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) +} + +_git_ls_tree () +{ + __git_complete_file +} + +_git_log () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + *..*) + local pfx=$(echo "$cur" | sed 's/\.\..*$/../') + cur=$(echo "$cur" | sed 's/^.*\.\.//') + COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur")) + ;; + *) + COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) + ;; + esac +} + +_git_merge_base () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) +} + +_git_pull () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + + case "${COMP_WORDS[0]},$COMP_CWORD" in + git-pull*,1) + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) + ;; + git,2) + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) + ;; + *) + local remote + case "${COMP_WORDS[0]}" in + git-pull) remote="${COMP_WORDS[1]}" ;; + git) remote="${COMP_WORDS[2]}" ;; + esac + COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur")) + ;; + esac +} + +_git_push () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + + case "${COMP_WORDS[0]},$COMP_CWORD" in + git-push*,1) + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) + ;; + git,2) + COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur")) + ;; + *) + case "$cur" in + *:*) + local remote + case "${COMP_WORDS[0]}" in + git-push) remote="${COMP_WORDS[1]}" ;; + git) remote="${COMP_WORDS[2]}" ;; + esac + cur=$(echo "$cur" | sed 's/^.*://') + COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur")) + ;; + *) + COMPREPLY=($(compgen -W "$(__git_refs2 .)" -- "$cur")) + ;; + esac + ;; + esac +} + +_git_whatchanged () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + case "$cur" in + *..*) + local pfx=$(echo "$cur" | sed 's/\.\..*$/../') + cur=$(echo "$cur" | sed 's/^.*\.\.//') + COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur")) + ;; + *) + COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) + ;; + esac +} + +_git () +{ + if [ $COMP_CWORD = 1 ]; then + COMPREPLY=($(compgen \ + -W "--version $(git help -a|egrep '^ ')" \ + -- "${COMP_WORDS[COMP_CWORD]}")) + else + case "${COMP_WORDS[1]}" in + branch) _git_branch ;; + cat-file) _git_cat_file ;; + checkout) _git_checkout ;; + diff) _git_diff ;; + diff-tree) _git_diff_tree ;; + fetch) _git_fetch ;; + log) _git_log ;; + ls-remote) _git_ls_remote ;; + ls-tree) _git_ls_tree ;; + pull) _git_pull ;; + push) _git_push ;; + whatchanged) _git_whatchanged ;; + *) COMPREPLY=() ;; + esac + fi +} + +_gitk () +{ + local cur="${COMP_WORDS[COMP_CWORD]}" + COMPREPLY=($(compgen -W "--all $(__git_refs .)" -- "$cur")) +} + +complete -o default -o nospace -F _git git +complete -o default -F _gitk gitk +complete -o default -F _git_branch git-branch +complete -o default -o nospace -F _git_cat_file git-cat-file +complete -o default -F _git_checkout git-checkout +complete -o default -o nospace -F _git_diff git-diff +complete -o default -F _git_diff_tree git-diff-tree +complete -o default -o nospace -F _git_fetch git-fetch +complete -o default -o nospace -F _git_log git-log +complete -o default -F _git_ls_remote git-ls-remote +complete -o default -o nospace -F _git_ls_tree git-ls-tree +complete -o default -F _git_merge_base git-merge-base +complete -o default -o nospace -F _git_pull git-pull +complete -o default -o nospace -F _git_push git-push +complete -o default -o nospace -F _git_whatchanged git-whatchanged + +# The following are necessary only for Cygwin, and only are needed +# when the user has tab-completed the executable name and consequently +# included the '.exe' suffix. +# +complete -o default -o nospace -F _git_cat_file git-cat-file.exe +complete -o default -o nospace -F _git_diff git-diff.exe +complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe +complete -o default -o nospace -F _git_log git-log.exe +complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe +complete -o default -F _git_merge_base git-merge-base.exe +complete -o default -o nospace -F _git_push git-push.exe +complete -o default -o nospace -F _git_whatchanged git-whatchanged.exe
Signature · 1 line
1.4.2.1.ga817
Hi,
Quoted 5 lines
This is a set of bash completion routines for many of the popular core Git tools. I wrote these routines from scratch after reading the git-compl and git-compl-lib routines available from the gitcompletion package at http://gitweb.hawaga.org.uk/
and found those to be lacking in functionality for some commands.
Did you talk to Ben Clifford (the maintainer of these scripts) before? His scripts seem to be in pretty wide-spread use already, so it might make sense to join efforts and improve his scripts (and get them into git-core).
Quoted 1 line
Consequently there may be some similarities but many differences.
Do you know of any (incompatible) differences?
Cheers,
Sebastian
Signature · 3 lines
Sebastian "tokkee" Harl GnuPG-ID: 0x8501C7FC http://tokkee.org/
Quoted 9 lines · Sebastian Harl
Hi,
Quoted 5 lines
This is a set of bash completion routines for many of the popular core Git tools. I wrote these routines from scratch after reading the git-compl and git-compl-lib routines available from the gitcompletion package at http://gitweb.hawaga.org.uk/
and found those to be lacking in functionality for some commands.Did you talk to Ben Clifford (the maintainer of these scripts) before?
No. I found his scripts yesterday, played around with them for about 15 minutes and found them to be missing some features. In particular they don't actually list all branch names as they only list only those contained directly in refs/heads. This is certainly very annoying when your topic branch policy uses "sp/", "jh/", "lt/" as branch name prefixes. It also won't work with Linus' new packed ref format...
Ben's scripts also don't always complete tags at points where Git accepts a tag, nor can they complete through a path with git diff or git cat-file to yank a file out of another branch which doesn't exist in the current working directory. They also can't complete branch names in a remote repository when you are fetching or pushing.
So I set out to write my own, finished it in less than an hour, used it for 4 hours while doing some merging, and sent an email to put the script into contrib. :-)
Quoted 3 lines
His scripts seem to be in pretty wide-spread use already, so it might make sense to join efforts and improve his scripts (and get them into git-core).
Agreed. There may be a few things my script is lacking but I think the one I sent yesterday is already more powerful than Ben's. But I'd like to see it be smarter about completion context and do even more. But right now I'm happy as it can complete my topic branch names and tag names.
I'd like to see core Git at least carry the completion for core Git. I know Ben has support for StGit and Cogito as well; two packages that my script doesn't support. In my humble opnion the completion scripts should migrate into the packages they support. I don't think its unreasonable to expect bash completion support to be part of a popular package which is heavily dependent on the shell for its user interface[*1*].
Quoted 3 lines
Quoted 1 line
Consequently there may be some similarities but many differences.
Do you know of any (incompatible) differences?
None that I can think of. I believe that my script will complete anything Ben's does with the exception of a stray single character option here or there.
You can't load both into your shell at the same time as bash will only accept one completion function for any given command and both packages use the same function names to implement the completion logic.
[*1*] So long as there is someone to maintain it anyway. :-)
Signature · 1 line
Shawn.
contrib/bash-git-completion.sh line 270
Quoted 9 lines · Shawn Pearce
contrib/bash-git-completion.sh new file +9
-0+_git_log () +{ +... +} +... +_git_whatchanged () +{ +... +}
These two look the same. Probably not very easy to maintain in the long run.
It would be nice to have git-show as well but it usually does not take ranges unlike these two. It is more like "git branch" from completion purposes.
Quoted 14 lines · Junio C Hamano
Review
Quoted 9 lines · Shawn Pearce
(quoted diff) +9
-0+_git_log () +{ +... +} +... +_git_whatchanged () +{ +... +}These two look the same. Probably not very easy to maintain in the long run.
They are the same.
Quoted 3 lines
It would be nice to have git-show as well but it usually does not take ranges unlike these two. It is more like "git branch" from completion purposes.
Like this? :-)
-- >8 --
Consolidated git_log and git_whatchanged; added git_show.
Minor requests from Junio: Consolidate the identical implementations of git_log and git_whatchanged, especially since these two commands take pretty much identical arguments. This should make the completion package a little easier to maintain.
Also added branch name completion for git-show. I tried to implement --pretty=oneline (etc.) but am apparently missing something as bash did not want to complete it through the registered completion routine, so that's still unsupported.
--- contrib/bash-git-completion.sh | 21 +++++++-------------- 1 files changed, 7 insertions(+), 14 deletions(-)
contrib/bash-git-completion.sh +7 -14
@@ -254,19 +254,10 @@ _git_push () esac } -_git_whatchanged () +_git_show () { local cur="${COMP_WORDS[COMP_CWORD]}" - case "$cur" in - *..*) - local pfx=$(echo "$cur" | sed 's/\.\..*$/../') - cur=$(echo "$cur" | sed 's/^.*\.\.//') - COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur")) - ;; - *) - COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) - ;; - esac + COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur")) } _git ()
@@ -288,7 +279,8 @@ _git () ls-tree) _git_ls_tree ;; pull) _git_pull ;; push) _git_push ;; - whatchanged) _git_whatchanged ;; + show) _git_show ;; + whatchanged) _git_log ;; *) COMPREPLY=() ;; esac fi
@@ -314,7 +306,8 @@ complete -o default -o nospace -F _git_l complete -o default -F _git_merge_base git-merge-base complete -o default -o nospace -F _git_pull git-pull complete -o default -o nospace -F _git_push git-push -complete -o default -o nospace -F _git_whatchanged git-whatchanged +complete -o default -F _git_show git-show +complete -o default -o nospace -F _git_log git-whatchanged # The following are necessary only for Cygwin, and only are needed # when the user has tab-completed the executable name and consequently
@@ -327,4 +320,4 @@ complete -o default -o nospace -F _git_l complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe complete -o default -F _git_merge_base git-merge-base.exe complete -o default -o nospace -F _git_push git-push.exe -complete -o default -o nospace -F _git_whatchanged git-whatchanged.exe +complete -o default -o nospace -F _git_log git-whatchanged.exe
Signature · 1 line
1.4.2.1.ga817
Quoted 10 lines · Shawn Pearce
Since these are completion routines only for tools shipped with core Git and since bash is a popular shell on many of the native core Git platforms (Linux, Mac OS X, Solaris, BSD) including these routines as part of the stock package would probably be convienent for many users.
Signed-off-by Shawn O. Pearce <[email protected]>
contrib/bash-git-completion.sh| 330 +++++++++++++++...1 files changed, 330 insertions(+), 0 deletions(-)
Hmph. I tried this and found that I like "git pull ." completion quite a bit. Having said that:
* If many people like it (like me), this may deserve to be outside contrib/
* Otherwise, it would probably be better to place it in either contrib/bash/git-completion.sh (with potentially other bash-related things not just completion, but I do not know offhand what other kind of hooks would be useful) or contrib/completion/git-completion.bash (possibly with completion for other shells).
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Shawn Pearce
Since these are completion routines only for tools shipped with core Git and since bash is a popular shell on many of the native core Git platforms (Linux, Mac OS X, Solaris, BSD) including these routines as part of the stock package would probably be convienent for many users.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Shawn O. Pearce <[email protected]>
contrib/bash-git-completion.sh| 330 +++++++++++++++...1 files changed, 330 insertions(+), 0 deletions(-)Hmph. I tried this and found that I like "git pull ." completion quite a bit. Having said that:
I got hooked very fast on the completion for git pull, git checkout and git fetch, especially on my local network where setting up the SSH connection for a remote git-ls-remote isn't that much of a performance hit. Trying to use ref completion on the Git repository itself on kernel.org was quite horrible. But it does (sort of) beat doing an ls-remote first.
Quoted 9 lines
* If many people like it (like me), this may deserve to be outside contrib/
* Otherwise, it would probably be better to place it in either
contrib/bash/git-completion.sh(with potentially other bash-related things not just completion, but I do not know offhand what other kind of hooks would be useful) or contrib/completion/git-completion.bash (possibly with completion for other shells).
I'm not sure there are too many other things to hook into bash in addition to completion so contrib/completion/git-completion.bash may be the better location, assuming it doesn't graduate out of contrib/.
Signature · 1 line
Shawn.
Quoted 3 lines · Shawn Pearce
I'm not sure there are too many other things to hook into bash in addition to completion so contrib/completion/git-completion.bash may be the better location, assuming it doesn't graduate out of contrib/.
Just so that we do not forget, I applied this (and your follow-up patch).
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Shawn Pearce
I'm not sure there are too many other things to hook into bash in addition to completion so contrib/completion/git-completion.bash may be the better location, assuming it doesn't graduate out of contrib/.
Just so that we do not forget, I applied this (and your follow-up patch).
Thanks. I was starting to wonder about that patch set and was probably going to send a reminder next week. Now I don't have to. :-)
Signature · 1 line
Shawn.
Hi,
the only gripe I have with it: whenever some options are added, or removed, or renamed, you have to adapt the completion script. That is why I started (ages ago) to auto-generate the completion script from the documentation (of course, this means that the options have to be described consistently in the documentation, which is good in its own).
Unfortunately, I never got around to finish it, but a preliminary version is in the mailing list archives.
However, since your script works well, and I do not have a working script, you clearly won! So, unless some kind soul actually implements my proposal, I am happy with your script.
Ciao,
Dscho
Quoted 8 lines · Johannes Schindelin
the only gripe I have with it: whenever some options are added, or removed, or renamed, you have to adapt the completion script. That is why I started (ages ago) to auto-generate the completion script from the documentation (of course, this means that the options have to be described consistently in the documentation, which is good in its own).
Unfortunately, I never got around to finish it, but a preliminary version is in the mailing list archives.
I was torn about putting options into the completion list. Right now "-l -b -f" are available for 'git checkout' but I think that was a bad idea. There's little point in offering completion for an option which is a single character. But that's my opinion and I'm sure someone else might actually find it useful.
Longer options however such as "--since" or "--pretty=" with its associated format options, or the merge strategy argument to "-s" would all be nice to have completion support for. However I did not implement these.
Quoted 3 lines
However, since your script works well, and I do not have a working script, you clearly won! So, unless some kind soul actually implements my proposal, I am happy with your script.
I was mainly getting tired of copying and pasting or retyping branch names and remote names. So that's what I implemented. (And remote names aren't quite right as they don't tab complete the ones available in .git/config.)
Signature · 1 line
Shawn.