contrib / completion / git-completion.zshon commit Merge branch 'ld/p4-unshelve' (1e5d454)
   1#compdef git gitk
   2
   3# zsh completion wrapper for git
   4#
   5# Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com>
   6#
   7# You need git's bash completion script installed somewhere, by default it
   8# would be the location bash-completion uses.
   9#
  10# If your script is somewhere else, you can configure it on your ~/.zshrc:
  11#
  12#  zstyle ':completion:*:*:git:*' script ~/.git-completion.zsh
  13#
  14# The recommended way to install this script is to copy to '~/.zsh/_git', and
  15# then add the following to your ~/.zshrc file:
  16#
  17#  fpath=(~/.zsh $fpath)
  18
  19complete ()
  20{
  21        # do nothing
  22        return 0
  23}
  24
  25zstyle -T ':completion:*:*:git:*' tag-order && \
  26        zstyle ':completion:*:*:git:*' tag-order 'common-commands'
  27
  28zstyle -s ":completion:*:*:git:*" script script
  29if [ -z "$script" ]; then
  30        local -a locations
  31        local e
  32        locations=(
  33                $(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
  34                '/etc/bash_completion.d/git' # fedora, old debian
  35                '/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
  36                '/usr/share/bash-completion/git' # gentoo
  37                )
  38        for e in $locations; do
  39                test -f $e && script="$e" && break
  40        done
  41fi
  42GIT_SOURCING_ZSH_COMPLETION=y . "$script"
  43
  44__gitcomp ()
  45{
  46        emulate -L zsh
  47
  48        local cur_="${3-$cur}"
  49
  50        case "$cur_" in
  51        --*=)
  52                ;;
  53        *)
  54                local c IFS=$' \t\n'
  55                local -a array
  56                for c in ${=1}; do
  57                        c="$c${4-}"
  58                        case $c in
  59                        --*=*|*.) ;;
  60                        *) c="$c " ;;
  61                        esac
  62                        array+=("$c")
  63                done
  64                compset -P '*[=:]'
  65                compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
  66                ;;
  67        esac
  68}
  69
  70__gitcomp_direct ()
  71{
  72        emulate -L zsh
  73
  74        local IFS=$'\n'
  75        compset -P '*[=:]'
  76        compadd -Q -- ${=1} && _ret=0
  77}
  78
  79__gitcomp_nl ()
  80{
  81        emulate -L zsh
  82
  83        local IFS=$'\n'
  84        compset -P '*[=:]'
  85        compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
  86}
  87
  88__gitcomp_nl_append ()
  89{
  90        emulate -L zsh
  91
  92        local IFS=$'\n'
  93        compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
  94}
  95
  96__gitcomp_file_direct ()
  97{
  98        emulate -L zsh
  99
 100        local IFS=$'\n'
 101        compset -P '*[=:]'
 102        compadd -Q -f -- ${=1} && _ret=0
 103}
 104
 105__gitcomp_file ()
 106{
 107        emulate -L zsh
 108
 109        local IFS=$'\n'
 110        compset -P '*[=:]'
 111        compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
 112}
 113
 114__git_zsh_bash_func ()
 115{
 116        emulate -L ksh
 117
 118        local command=$1
 119
 120        local completion_func="_git_${command//-/_}"
 121        declare -f $completion_func >/dev/null && $completion_func && return
 122
 123        local expansion=$(__git_aliased_command "$command")
 124        if [ -n "$expansion" ]; then
 125                words[1]=$expansion
 126                completion_func="_git_${expansion//-/_}"
 127                declare -f $completion_func >/dev/null && $completion_func
 128        fi
 129}
 130
 131__git_zsh_cmd_common ()
 132{
 133        local -a list
 134        list=(
 135        add:'add file contents to the index'
 136        bisect:'find by binary search the change that introduced a bug'
 137        branch:'list, create, or delete branches'
 138        checkout:'checkout a branch or paths to the working tree'
 139        clone:'clone a repository into a new directory'
 140        commit:'record changes to the repository'
 141        diff:'show changes between commits, commit and working tree, etc'
 142        fetch:'download objects and refs from another repository'
 143        grep:'print lines matching a pattern'
 144        init:'create an empty Git repository or reinitialize an existing one'
 145        log:'show commit logs'
 146        merge:'join two or more development histories together'
 147        mv:'move or rename a file, a directory, or a symlink'
 148        pull:'fetch from and merge with another repository or a local branch'
 149        push:'update remote refs along with associated objects'
 150        rebase:'forward-port local commits to the updated upstream head'
 151        reset:'reset current HEAD to the specified state'
 152        rm:'remove files from the working tree and from the index'
 153        show:'show various types of objects'
 154        status:'show the working tree status'
 155        tag:'create, list, delete or verify a tag object signed with GPG')
 156        _describe -t common-commands 'common commands' list && _ret=0
 157}
 158
 159__git_zsh_cmd_alias ()
 160{
 161        local -a list
 162        list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
 163        _describe -t alias-commands 'aliases' list $* && _ret=0
 164}
 165
 166__git_zsh_cmd_all ()
 167{
 168        local -a list
 169        emulate ksh -c __git_compute_all_commands
 170        list=( ${=__git_all_commands} )
 171        _describe -t all-commands 'all commands' list && _ret=0
 172}
 173
 174__git_zsh_main ()
 175{
 176        local curcontext="$curcontext" state state_descr line
 177        typeset -A opt_args
 178        local -a orig_words
 179
 180        orig_words=( ${words[@]} )
 181
 182        _arguments -C \
 183                '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
 184                '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
 185                '--git-dir=-[set the path to the repository]: :_directories' \
 186                '--bare[treat the repository as a bare repository]' \
 187                '(- :)--version[prints the git suite version]' \
 188                '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
 189                '--html-path[print the path where git''s HTML documentation is installed]' \
 190                '--info-path[print the path where the Info files are installed]' \
 191                '--man-path[print the manpath (see `man(1)`) for the man pages]' \
 192                '--work-tree=-[set the path to the working tree]: :_directories' \
 193                '--namespace=-[set the git namespace]' \
 194                '--no-replace-objects[do not use replacement refs to replace git objects]' \
 195                '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
 196                '(-): :->command' \
 197                '(-)*:: :->arg' && return
 198
 199        case $state in
 200        (command)
 201                _alternative \
 202                         'alias-commands:alias:__git_zsh_cmd_alias' \
 203                         'common-commands:common:__git_zsh_cmd_common' \
 204                         'all-commands:all:__git_zsh_cmd_all' && _ret=0
 205                ;;
 206        (arg)
 207                local command="${words[1]}" __git_dir
 208
 209                if (( $+opt_args[--bare] )); then
 210                        __git_dir='.'
 211                else
 212                        __git_dir=${opt_args[--git-dir]}
 213                fi
 214
 215                (( $+opt_args[--help] )) && command='help'
 216
 217                words=( ${orig_words[@]} )
 218
 219                __git_zsh_bash_func $command
 220                ;;
 221        esac
 222}
 223
 224_git ()
 225{
 226        local _ret=1
 227        local cur cword prev
 228
 229        cur=${words[CURRENT]}
 230        prev=${words[CURRENT-1]}
 231        let cword=CURRENT-1
 232
 233        if (( $+functions[__${service}_zsh_main] )); then
 234                __${service}_zsh_main
 235        else
 236                emulate ksh -c __${service}_main
 237        fi
 238
 239        let _ret && _default && _ret=0
 240        return _ret
 241}
 242
 243_git