add object_as_type helper for casting objects
[git/mingw.git] / contrib / completion / git-completion.zsh
blob9f6f0fa5581002a2371e062fd5def0978733edc1
1 #compdef git gitk
3 # zsh completion wrapper for git
5 # Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com>
7 # You need git's bash completion script installed somewhere, by default it
8 # would be the location bash-completion uses.
10 # If your script is somewhere else, you can configure it on your ~/.zshrc:
12 # zstyle ':completion:*:*:git:*' script ~/.git-completion.sh
14 # The recommended way to install this script is to copy to '~/.zsh/_git', and
15 # then add the following to your ~/.zshrc file:
17 # fpath=(~/.zsh $fpath)
19 complete ()
21 # do nothing
22 return 0
25 zstyle -T ':completion:*:*:git:*' tag-order && \
26 zstyle ':completion:*:*:git:*' tag-order 'common-commands'
28 zstyle -s ":completion:*:*:git:*" script script
29 if [ -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
38 for e in $locations; do
39 test -f $e && script="$e" && break
40 done
42 ZSH_VERSION='' . "$script"
44 __gitcomp ()
46 emulate -L zsh
48 local cur_="${3-$cur}"
50 case "$cur_" in
51 --*=)
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
67 esac
70 __gitcomp_nl ()
72 emulate -L zsh
74 local IFS=$'\n'
75 compset -P '*[=:]'
76 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
79 __gitcomp_nl_append ()
81 emulate -L zsh
83 local IFS=$'\n'
84 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
87 __gitcomp_file ()
89 emulate -L zsh
91 local IFS=$'\n'
92 compset -P '*[=:]'
93 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
96 __git_zsh_bash_func ()
98 emulate -L ksh
100 local command=$1
102 local completion_func="_git_${command//-/_}"
103 declare -f $completion_func >/dev/null && $completion_func && return
105 local expansion=$(__git_aliased_command "$command")
106 if [ -n "$expansion" ]; then
107 words[1]=$expansion
108 completion_func="_git_${expansion//-/_}"
109 declare -f $completion_func >/dev/null && $completion_func
113 __git_zsh_cmd_common ()
115 local -a list
116 list=(
117 add:'add file contents to the index'
118 bisect:'find by binary search the change that introduced a bug'
119 branch:'list, create, or delete branches'
120 checkout:'checkout a branch or paths to the working tree'
121 clone:'clone a repository into a new directory'
122 commit:'record changes to the repository'
123 diff:'show changes between commits, commit and working tree, etc'
124 fetch:'download objects and refs from another repository'
125 grep:'print lines matching a pattern'
126 init:'create an empty Git repository or reinitialize an existing one'
127 log:'show commit logs'
128 merge:'join two or more development histories together'
129 mv:'move or rename a file, a directory, or a symlink'
130 pull:'fetch from and merge with another repository or a local branch'
131 push:'update remote refs along with associated objects'
132 rebase:'forward-port local commits to the updated upstream head'
133 reset:'reset current HEAD to the specified state'
134 rm:'remove files from the working tree and from the index'
135 show:'show various types of objects'
136 status:'show the working tree status'
137 tag:'create, list, delete or verify a tag object signed with GPG')
138 _describe -t common-commands 'common commands' list && _ret=0
141 __git_zsh_cmd_alias ()
143 local -a list
144 list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
145 _describe -t alias-commands 'aliases' list $* && _ret=0
148 __git_zsh_cmd_all ()
150 local -a list
151 emulate ksh -c __git_compute_all_commands
152 list=( ${=__git_all_commands} )
153 _describe -t all-commands 'all commands' list && _ret=0
156 __git_zsh_main ()
158 local curcontext="$curcontext" state state_descr line
159 typeset -A opt_args
160 local -a orig_words
162 orig_words=( ${words[@]} )
164 _arguments -C \
165 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
166 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
167 '--git-dir=-[set the path to the repository]: :_directories' \
168 '--bare[treat the repository as a bare repository]' \
169 '(- :)--version[prints the git suite version]' \
170 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
171 '--html-path[print the path where git''s HTML documentation is installed]' \
172 '--info-path[print the path where the Info files are installed]' \
173 '--man-path[print the manpath (see `man(1)`) for the man pages]' \
174 '--work-tree=-[set the path to the working tree]: :_directories' \
175 '--namespace=-[set the git namespace]' \
176 '--no-replace-objects[do not use replacement refs to replace git objects]' \
177 '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
178 '(-): :->command' \
179 '(-)*:: :->arg' && return
181 case $state in
182 (command)
183 _alternative \
184 'alias-commands:alias:__git_zsh_cmd_alias' \
185 'common-commands:common:__git_zsh_cmd_common' \
186 'all-commands:all:__git_zsh_cmd_all' && _ret=0
188 (arg)
189 local command="${words[1]}" __git_dir
191 if (( $+opt_args[--bare] )); then
192 __git_dir='.'
193 else
194 __git_dir=${opt_args[--git-dir]}
197 (( $+opt_args[--help] )) && command='help'
199 words=( ${orig_words[@]} )
201 __git_zsh_bash_func $command
203 esac
206 _git ()
208 local _ret=1
209 local cur cword prev
211 cur=${words[CURRENT]}
212 prev=${words[CURRENT-1]}
213 let cword=CURRENT-1
215 if (( $+functions[__${service}_zsh_main] )); then
216 __${service}_zsh_main
217 else
218 emulate ksh -c __${service}_main
221 let _ret && _default && _ret=0
222 return _ret
225 _git