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 zstyle -T ':completion:*:*:git:*' tag-order && \
20 zstyle ':completion:*:*:git:*' tag-order 'common-commands'
22 zstyle -s ":completion:*:*:git:*" script script
23 if [ -z "$script" ]; then
27 "$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
28 '/etc/bash_completion.d/git' # fedora, old debian
29 '/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
30 '/usr/share/bash-completion/git' # gentoo
32 for e in $locations; do
33 test -f $e && script="$e" && break
42 local cur_="${3-$cur}"
59 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
69 compadd -Q -S "${4- }" -p "${2-}" -- ${(f)1} && _ret=0
72 __gitcomp_nl_append ()
76 compadd -Q -S "${4- }" -p "${2-}" -- ${(f)1} && _ret=0
83 compadd -Q -p "${2-}" -f -- ${(f)1} && _ret=0
86 __git_zsh_bash_func ()
92 local completion_func="_git_${command//-/_}"
93 declare -f $completion_func >/dev/null && $completion_func && return
95 local expansion=$(__git_aliased_command "$command")
96 if [ -n "$expansion" ]; then
98 completion_func="_git_${expansion//-/_}"
99 declare -f $completion_func >/dev/null && $completion_func
103 __git_zsh_cmd_common ()
107 add:'add file contents to the index'
108 bisect:'find by binary search the change that introduced a bug'
109 branch:'list, create, or delete branches'
110 checkout:'checkout a branch or paths to the working tree'
111 clone:'clone a repository into a new directory'
112 commit:'record changes to the repository'
113 diff:'show changes between commits, commit and working tree, etc'
114 fetch:'download objects and refs from another repository'
115 grep:'print lines matching a pattern'
116 init:'create an empty Git repository or reinitialize an existing one'
117 log:'show commit logs'
118 merge:'join two or more development histories together'
119 mv:'move or rename a file, a directory, or a symlink'
120 pull:'fetch from and merge with another repository or a local branch'
121 push:'update remote refs along with associated objects'
122 rebase:'forward-port local commits to the updated upstream head'
123 reset:'reset current HEAD to the specified state'
124 rm:'remove files from the working tree and from the index'
125 show:'show various types of objects'
126 status:'show the working tree status'
127 tag:'create, list, delete or verify a tag object signed with GPG')
128 _describe -t common-commands 'common commands' list && _ret=0
131 __git_zsh_cmd_alias ()
134 list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
135 _describe -t alias-commands 'aliases' list $* && _ret=0
141 emulate ksh -c __git_compute_all_commands
142 list=( ${=__git_all_commands} )
143 _describe -t all-commands 'all commands' list && _ret=0
148 local curcontext="$curcontext" state state_descr line
152 orig_words=( ${words[@]} )
155 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
156 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
157 '--git-dir=-[set the path to the repository]: :_directories' \
158 '--bare[treat the repository as a bare repository]' \
159 '(- :)--version[prints the git suite version]' \
160 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
161 '--html-path[print the path where git''s HTML documentation is installed]' \
162 '--info-path[print the path where the Info files are installed]' \
163 '--man-path[print the manpath (see `man(1)`) for the man pages]' \
164 '--work-tree=-[set the path to the working tree]: :_directories' \
165 '--namespace=-[set the git namespace]' \
166 '--no-replace-objects[do not use replacement refs to replace git objects]' \
167 '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
169 '(-)*:: :->arg' && return
174 'alias-commands:alias:__git_zsh_cmd_alias' \
175 'common-commands:common:__git_zsh_cmd_common' \
176 'all-commands:all:__git_zsh_cmd_all' && _ret=0
179 local command="${words[1]}" __git_dir
181 if (( $+opt_args[--bare] )); then
184 __git_dir=${opt_args[--git-dir]}
187 (( $+opt_args[--help] )) && command='help'
189 words=( ${orig_words[@]} )
191 __git_zsh_bash_func $command
201 cur=${words[CURRENT]}
202 prev=${words[CURRENT-1]}
205 if (( $+functions[__${service}_zsh_main] )); then
206 __${service}_zsh_main
207 elif (( $+functions[__${service}_main] )); then
208 emulate ksh -c __${service}_main
209 elif (( $+functions[_${service}] )); then
210 emulate ksh -c _${service}
213 let _ret && _default && _ret=0