Bash completion support for remotes in .git/config.
[gitweb.git] / contrib / completion / git-completion.bash
index d9cb17d0b2f21b6cabd78c245f4deaf23cc6b6a0..5f1be46ba57c87772c1115191b7949999b7bff8f 100755 (executable)
@@ -59,12 +59,21 @@ __git_refs2 ()
 
 __git_remotes ()
 {
-       local i REVERTGLOB=$(shopt -p nullglob)
+       local i ngoff IFS=$'\n'
+       shopt -q nullglob || ngoff=1
        shopt -s nullglob
        for i in .git/remotes/*; do
                echo ${i#.git/remotes/}
        done
-       $REVERTGLOB
+       [ "$ngoff" ] && shopt -u nullglob
+       for i in $(git repo-config --list); do
+               case "$i" in
+               remote.*.url=*)
+                       i="${i#remote.}"
+                       echo "${i/.url=*/}"
+                       ;;
+               esac
+       done
 }
 
 __git_complete_file ()
@@ -101,6 +110,30 @@ __git_complete_file ()
        esac
 }
 
+__git_aliases ()
+{
+       local i IFS=$'\n'
+       for i in $(git repo-config --list); do
+               case "$i" in
+               alias.*)
+                       i="${i#alias.}"
+                       echo "${i/=*/}"
+                       ;;
+               esac
+       done
+}
+
+__git_aliased_command ()
+{
+       local word cmdline=$(git repo-config --get "alias.$1")
+       for word in $cmdline; do
+               if [ "${word##-*}" ]; then
+                       echo $word
+                       return
+               fi
+       done
+}
+
 _git_branch ()
 {
        local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -254,6 +287,13 @@ _git_push ()
        esac
 }
 
+_git_reset ()
+{
+       local cur="${COMP_WORDS[COMP_CWORD]}"
+       local opt="--mixed --hard --soft"
+       COMPREPLY=($(compgen -W "$opt $(__git_refs .)" -- "$cur"))
+}
+
 _git_show ()
 {
        local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -264,10 +304,18 @@ _git ()
 {
        if [ $COMP_CWORD = 1 ]; then
                COMPREPLY=($(compgen \
-                       -W "--version $(git help -a|egrep '^ ')" \
+                       -W "--version $(git help -a|egrep '^ ') \
+                           $(__git_aliases)" \
                        -- "${COMP_WORDS[COMP_CWORD]}"))
        else
-               case "${COMP_WORDS[1]}" in
+               local command="${COMP_WORDS[1]}"
+               local expansion=$(__git_aliased_command "$command")
+
+               if [ "$expansion" ]; then
+                       command="$expansion"
+               fi
+
+               case "$command" in
                branch)      _git_branch ;;
                cat-file)    _git_cat_file ;;
                checkout)    _git_checkout ;;
@@ -277,8 +325,10 @@ _git ()
                log)         _git_log ;;
                ls-remote)   _git_ls_remote ;;
                ls-tree)     _git_ls_tree ;;
+               merge-base)  _git_merge_base ;;
                pull)        _git_pull ;;
                push)        _git_push ;;
+               reset)       _git_reset ;;
                show)        _git_show ;;
                show-branch) _git_log ;;
                whatchanged) _git_log ;;
@@ -307,13 +357,18 @@ 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            -F _git_reset git-reset
 complete -o default            -F _git_show git-show
+complete -o default -o nospace -F _git_log git-show-branch
 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
 # included the '.exe' suffix.
 #
+if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
+complete -o default -o nospace -F _git git.exe
+complete -o default            -F _git_branch git-branch.exe
 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
@@ -321,4 +376,6 @@ 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_log git-show-branch.exe
 complete -o default -o nospace -F _git_log git-whatchanged.exe
+fi