Teach bash about git log/show/whatchanged options.
[gitweb.git] / contrib / completion / git-completion.bash
index b55431fbf297a561b68dab6af1ea9eeacb3a0943..3852f467d8729612db0277499734ee5dfdb1e0ba 100755 (executable)
 #    2) Added the following line to your .bashrc:
 #        source ~/.git-completion.sh
 #
+#    3) Consider changing your PS1 to also show the current branch:
+#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
+#
+#       The argument to __git_ps1 will be displayed only if you
+#       are currently in a git repository.  The %s token will be
+#       the name of the current branch.
+#
 
 __gitdir ()
 {
        echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
 }
 
+__git_ps1 ()
+{
+       local b="$(git symbolic-ref HEAD 2>/dev/null)"
+       if [ -n "$b" ]; then
+               if [ -n "$1" ]; then
+                       printf "$1" "${b##refs/heads/}"
+               else
+                       printf " (%s)" "${b##refs/heads/}"
+               fi
+       fi
+}
+
 __git_refs ()
 {
        local cmd i is_hash=y dir="${1:-$(__gitdir)}"
@@ -125,6 +144,26 @@ __git_complete_file ()
        esac
 }
 
+__git_complete_revlist ()
+{
+       local pfx cur="${COMP_WORDS[COMP_CWORD]}"
+       case "$cur" in
+       *...*)
+               pfx="${cur%...*}..."
+               cur="${cur#*...}"
+               COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
+               ;;
+       *..*)
+               pfx="${cur%..*}.."
+               cur="${cur#*..}"
+               COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
+               ;;
+       *)
+               COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
+               ;;
+       esac
+}
+
 __git_commands ()
 {
        local i IFS=" "$'\n'
@@ -230,6 +269,21 @@ _git_checkout ()
        COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
 }
 
+_git_cherry_pick ()
+{
+       local cur="${COMP_WORDS[COMP_CWORD]}"
+       case "$cur" in
+       --*)
+               COMPREPLY=($(compgen -W "
+                       --edit --no-commit
+                       " -- "$cur"))
+               ;;
+       *)
+               COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
+               ;;
+       esac
+}
+
 _git_diff ()
 {
        __git_complete_file
@@ -271,6 +325,26 @@ _git_fetch ()
        esac
 }
 
+_git_format_patch ()
+{
+       local cur="${COMP_WORDS[COMP_CWORD]}"
+       case "$cur" in
+       --*)
+               COMPREPLY=($(compgen -W "
+                       --stdout --attach --thread
+                       --output-directory
+                       --numbered --start-number
+                       --keep-subject
+                       --signoff
+                       --in-reply-to=
+                       --full-index --binary
+                       " -- "$cur"))
+               return
+               ;;
+       esac
+       __git_complete_revlist
+}
+
 _git_ls_remote ()
 {
        local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -284,22 +358,30 @@ _git_ls_tree ()
 
 _git_log ()
 {
-       local pfx cur="${COMP_WORDS[COMP_CWORD]}"
+       local cur="${COMP_WORDS[COMP_CWORD]}"
        case "$cur" in
-       *...*)
-               pfx="${cur%...*}..."
-               cur="${cur#*...}"
-               COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
-               ;;
-       *..*)
-               pfx="${cur%..*}.."
-               cur="${cur#*..}"
-               COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
+       --pretty=*)
+               COMPREPLY=($(compgen -W "
+                       oneline short medium full fuller email raw
+                       " -- "${cur##--pretty=}"))
+               return
                ;;
-       *)
-               COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
+       --*)
+               COMPREPLY=($(compgen -W "
+                       --max-count= --max-age= --since= --after=
+                       --min-age= --before= --until=
+                       --root --not --topo-order --date-order
+                       --no-merges
+                       --abbrev-commit --abbrev=
+                       --relative-date
+                       --author= --committer= --grep=
+                       --all-match
+                       --pretty= --name-status --name-only
+                       " -- "$cur"))
+               return
                ;;
        esac
+       __git_complete_revlist
 }
 
 _git_merge ()
@@ -308,16 +390,16 @@ _git_merge ()
        case "$cur" in
        --*)
                COMPREPLY=($(compgen -W "
-                       --no-commit --no-summary --squash
+                       --no-commit --no-summary --squash --strategy
                        " -- "$cur"))
                return
        esac
-       if [ $COMP_CWORD -gt 1 -a X-s = "X${COMP_WORDS[COMP_CWORD-1]}" ]
-       then
+       case "${COMP_WORDS[COMP_CWORD-1]}" in
+       -s|--strategy)
                COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
-       else
-               COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
-       fi
+               return
+       esac
+       COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
 }
 
 _git_merge_base ()
@@ -326,6 +408,12 @@ _git_merge_base ()
        COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
 }
 
+_git_name_rev ()
+{
+       local cur="${COMP_WORDS[COMP_CWORD]}"
+       COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
+}
+
 _git_pull ()
 {
        local cur="${COMP_WORDS[COMP_CWORD]}"
@@ -378,17 +466,35 @@ _git_push ()
        esac
 }
 
-_git_reset ()
+_git_rebase ()
 {
        local cur="${COMP_WORDS[COMP_CWORD]}"
-       local opt="--mixed --hard --soft"
-       COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
+       if [ -d .dotest ]; then
+               COMPREPLY=($(compgen -W "
+                       --continue --skip --abort
+                       " -- "$cur"))
+               return
+       fi
+       case "$cur" in
+       --*)
+               COMPREPLY=($(compgen -W "
+                       --onto --merge --strategy
+                       " -- "$cur"))
+               return
+       esac
+       case "${COMP_WORDS[COMP_CWORD-1]}" in
+       -s|--strategy)
+               COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
+               return
+       esac
+       COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
 }
 
-_git_show ()
+_git_reset ()
 {
        local cur="${COMP_WORDS[COMP_CWORD]}"
-       COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
+       local opt="--mixed --hard --soft"
+       COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
 }
 
 _git ()
@@ -422,18 +528,22 @@ _git ()
        branch)      _git_branch ;;
        cat-file)    _git_cat_file ;;
        checkout)    _git_checkout ;;
+       cherry-pick) _git_cherry_pick ;;
        diff)        _git_diff ;;
        diff-tree)   _git_diff_tree ;;
        fetch)       _git_fetch ;;
+       format-patch) _git_format_patch ;;
        log)         _git_log ;;
        ls-remote)   _git_ls_remote ;;
        ls-tree)     _git_ls_tree ;;
        merge)       _git_merge;;
        merge-base)  _git_merge_base ;;
+       name-rev)    _git_name_rev ;;
        pull)        _git_pull ;;
        push)        _git_push ;;
+       rebase)      _git_rebase ;;
        reset)       _git_reset ;;
-       show)        _git_show ;;
+       show)        _git_log ;;
        show-branch) _git_log ;;
        whatchanged) _git_log ;;
        *)           COMPREPLY=() ;;
@@ -451,18 +561,22 @@ 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            -F _git_cherry_pick git-cherry-pick
 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_format_patch git-format-patch
 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 git-merge
 complete -o default            -F _git_merge_base git-merge-base
+complete -o default            -F _git_name_rev git-name-rev
 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_rebase git-rebase
 complete -o default            -F _git_reset git-reset
-complete -o default            -F _git_show git-show
+complete -o default            -F _git_log git-show
 complete -o default -o nospace -F _git_log git-show-branch
 complete -o default -o nospace -F _git_log git-whatchanged
 
@@ -476,10 +590,13 @@ 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
+complete -o default -o nospace -F _git_format_patch git-format-patch.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            -F _git_name_rev git-name-rev.exe
 complete -o default -o nospace -F _git_push git-push.exe
+complete -o default -o nospace -F _git_log git-show.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