3 # zsh completion wrapper for git
5 # Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com>
7 # The recommended way to install this script is to make a copy of it as a
8 # file named '_git' inside any directory in your fpath.
10 # For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
11 # and then add the following to your ~/.zshrc file:
13 # fpath=(~/.zsh $fpath)
15 # You need git's bash completion script installed. By default bash-completion's
16 # location will be used (e.g. /usr/share/bash-completion/completions/git).
18 # If your bash completion script is somewhere else, you can specify the
19 # location in your ~/.zshrc:
21 # zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
30 zstyle -T ':completion:*:*:git:*' tag-order && \
31 zstyle ':completion:*:*:git:*' tag-order 'common-commands'
33 zstyle -s ":completion:*:*:git:*" script script
34 if [ -z "$script" ]; then
38 "$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
39 '/etc/bash_completion.d/git' # fedora, old debian
40 '/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
41 '/usr/share/bash-completion/git' # gentoo
43 for e in $locations; do
44 test -f $e && script="$e" && break
47 GIT_SOURCING_ZSH_COMPLETION=y . "$script"
53 local cur_="${3-$cur}"
70 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
81 compadd -Q -- ${${=1}% } && _ret=0
90 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
93 __gitcomp_nl_append ()
98 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
101 __gitcomp_file_direct ()
107 compadd -f -- ${=1} && _ret=0
116 compadd -p "${2-}" -f -- ${=1} && _ret=0
119 __git_zsh_bash_func ()
125 local completion_func="_git_${command//-/_}"
126 declare -f $completion_func >/dev/null && $completion_func && return
128 local expansion=$(__git_aliased_command "$command")
129 if [ -n "$expansion" ]; then
131 completion_func="_git_${expansion//-/_}"
132 declare -f $completion_func >/dev/null && $completion_func
136 __git_zsh_cmd_common ()
140 add:'add file contents to the index'
141 bisect:'find by binary search the change that introduced a bug'
142 branch:'list, create, or delete branches'
143 checkout:'checkout a branch or paths to the working tree'
144 clone:'clone a repository into a new directory'
145 commit:'record changes to the repository'
146 diff:'show changes between commits, commit and working tree, etc'
147 fetch:'download objects and refs from another repository'
148 grep:'print lines matching a pattern'
149 init:'create an empty Git repository or reinitialize an existing one'
150 log:'show commit logs'
151 merge:'join two or more development histories together'
152 mv:'move or rename a file, a directory, or a symlink'
153 pull:'fetch from and merge with another repository or a local branch'
154 push:'update remote refs along with associated objects'
155 rebase:'forward-port local commits to the updated upstream head'
156 reset:'reset current HEAD to the specified state'
157 restore:'restore working tree files'
158 rm:'remove files from the working tree and from the index'
159 show:'show various types of objects'
160 status:'show the working tree status'
161 switch:'switch branches'
162 tag:'create, list, delete or verify a tag object signed with GPG')
163 _describe -t common-commands 'common commands' list && _ret=0
166 __git_zsh_cmd_alias ()
169 list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
170 _describe -t alias-commands 'aliases' list $* && _ret=0
176 emulate ksh -c __git_compute_all_commands
177 list=( ${=__git_all_commands} )
178 _describe -t all-commands 'all commands' list && _ret=0
183 local curcontext="$curcontext" state state_descr line
187 orig_words=( ${words[@]} )
190 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
191 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
192 '--git-dir=-[set the path to the repository]: :_directories' \
193 '--bare[treat the repository as a bare repository]' \
194 '(- :)--version[prints the git suite version]' \
195 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
196 '--html-path[print the path where git''s HTML documentation is installed]' \
197 '--info-path[print the path where the Info files are installed]' \
198 '--man-path[print the manpath (see `man(1)`) for the man pages]' \
199 '--work-tree=-[set the path to the working tree]: :_directories' \
200 '--namespace=-[set the git namespace]' \
201 '--no-replace-objects[do not use replacement refs to replace git objects]' \
202 '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
204 '(-)*:: :->arg' && return
209 'alias-commands:alias:__git_zsh_cmd_alias' \
210 'common-commands:common:__git_zsh_cmd_common' \
211 'all-commands:all:__git_zsh_cmd_all' && _ret=0
214 local command="${words[1]}" __git_dir
216 if (( $+opt_args[--bare] )); then
219 __git_dir=${opt_args[--git-dir]}
222 (( $+opt_args[--help] )) && command='help'
224 words=( ${orig_words[@]} )
226 __git_zsh_bash_func $command
236 cur=${words[CURRENT]}
237 prev=${words[CURRENT-1]}
240 if (( $+functions[__${service}_zsh_main] )); then
241 __${service}_zsh_main
243 emulate ksh -c __${service}_main
246 let _ret && _default && _ret=0