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. pkg-config --variable=completionsdir bash-completion).
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
24 zstyle -T ':completion:*:*:git:*' tag-order && \
25 zstyle ':completion:*:*:git:*' tag-order 'common-commands'
27 zstyle -s ":completion:*:*:git:*" script script
28 if [ -z "$script" ]; then
30 local e bash_completion
32 bash_completion=$(pkg-config --variable=completionsdir bash-completion 2>/dev/null) ||
33 bash_completion='/usr/share/bash-completion/completions/'
36 "$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
37 "$HOME/.local/share/bash-completion/completions/git"
38 "$bash_completion/git"
39 '/etc/bash_completion.d/git' # old debian
41 for e in $locations; do
42 test -f $e && script="$e" && break
46 local old_complete="$functions[complete]"
48 GIT_SOURCING_ZSH_COMPLETION=y . "$script"
49 functions[complete]="$old_complete"
55 local cur_="${3-$cur}"
64 if [[ $c == "--" ]]; then
75 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
81 if [[ $c == "--" ]]; then
94 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
105 compadd -Q -- ${${=1}% } && _ret=0
114 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
117 __gitcomp_nl_append ()
122 compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
125 __gitcomp_file_direct ()
131 compadd -f -- ${=1} && _ret=0
140 compadd -p "${2-}" -f -- ${=1} && _ret=0
143 __git_zsh_bash_func ()
149 local completion_func="_git_${command//-/_}"
150 declare -f $completion_func >/dev/null && $completion_func && return
152 local expansion=$(__git_aliased_command "$command")
153 if [ -n "$expansion" ]; then
155 completion_func="_git_${expansion//-/_}"
156 declare -f $completion_func >/dev/null && $completion_func
160 __git_zsh_cmd_common ()
164 add:'add file contents to the index'
165 bisect:'find by binary search the change that introduced a bug'
166 branch:'list, create, or delete branches'
167 checkout:'checkout a branch or paths to the working tree'
168 clone:'clone a repository into a new directory'
169 commit:'record changes to the repository'
170 diff:'show changes between commits, commit and working tree, etc'
171 fetch:'download objects and refs from another repository'
172 grep:'print lines matching a pattern'
173 init:'create an empty Git repository or reinitialize an existing one'
174 log:'show commit logs'
175 merge:'join two or more development histories together'
176 mv:'move or rename a file, a directory, or a symlink'
177 pull:'fetch from and merge with another repository or a local branch'
178 push:'update remote refs along with associated objects'
179 rebase:'forward-port local commits to the updated upstream head'
180 reset:'reset current HEAD to the specified state'
181 restore:'restore working tree files'
182 rm:'remove files from the working tree and from the index'
183 show:'show various types of objects'
184 status:'show the working tree status'
185 switch:'switch branches'
186 tag:'create, list, delete or verify a tag object signed with GPG')
187 _describe -t common-commands 'common commands' list && _ret=0
190 __git_zsh_cmd_alias ()
193 list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
194 _describe -t alias-commands 'aliases' list $* && _ret=0
200 emulate ksh -c __git_compute_all_commands
201 list=( ${=__git_all_commands} )
202 _describe -t all-commands 'all commands' list && _ret=0
207 local curcontext="$curcontext" state state_descr line
211 orig_words=( ${words[@]} )
214 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
215 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
216 '--git-dir=-[set the path to the repository]: :_directories' \
217 '--bare[treat the repository as a bare repository]' \
218 '(- :)--version[prints the git suite version]' \
219 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
220 '--html-path[print the path where git''s HTML documentation is installed]' \
221 '--info-path[print the path where the Info files are installed]' \
222 '--man-path[print the manpath (see `man(1)`) for the man pages]' \
223 '--work-tree=-[set the path to the working tree]: :_directories' \
224 '--namespace=-[set the git namespace]' \
225 '--no-replace-objects[do not use replacement refs to replace git objects]' \
226 '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
228 '(-)*:: :->arg' && return
233 'alias-commands:alias:__git_zsh_cmd_alias' \
234 'common-commands:common:__git_zsh_cmd_common' \
235 'all-commands:all:__git_zsh_cmd_all' && _ret=0
238 local command="${words[1]}" __git_dir
240 if (( $+opt_args[--bare] )); then
243 __git_dir=${opt_args[--git-dir]}
246 (( $+opt_args[--help] )) && command='help'
248 words=( ${orig_words[@]} )
250 __git_zsh_bash_func $command
260 cur=${words[CURRENT]}
261 prev=${words[CURRENT-1]}
264 if (( $+functions[__${service}_zsh_main] )); then
265 __${service}_zsh_main
266 elif (( $+functions[__${service}_main] )); then
267 emulate ksh -c __${service}_main
268 elif (( $+functions[_${service}] )); then
269 emulate ksh -c _${service}
270 elif (( $+functions[_${service//-/_}] )); then
271 emulate ksh -c _${service//-/_}
274 let _ret && _default && _ret=0