Merge branch 'jc/checkout-detach-doc'
[git.git] / contrib / completion / git-completion.zsh
blobfac5e711ebbbd074366d89848165e0f9dbc36c36
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 '/etc/bash_completion.d/git' # fedora, old debian
34 '/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
35 '/usr/share/bash-completion/git' # gentoo
36 $(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
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_file ()
81 emulate -L zsh
83 local IFS=$'\n'
84 compset -P '*[=:]'
85 compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
88 __git_zsh_bash_func ()
90 emulate -L ksh
92 local command=$1
94 local completion_func="_git_${command//-/_}"
95 declare -f $completion_func >/dev/null && $completion_func && return
97 local expansion=$(__git_aliased_command "$command")
98 if [ -n "$expansion" ]; then
99 completion_func="_git_${expansion//-/_}"
100 declare -f $completion_func >/dev/null && $completion_func
104 __git_zsh_cmd_common ()
106 local -a list
107 list=(
108 add:'add file contents to the index'
109 bisect:'find by binary search the change that introduced a bug'
110 branch:'list, create, or delete branches'
111 checkout:'checkout a branch or paths to the working tree'
112 clone:'clone a repository into a new directory'
113 commit:'record changes to the repository'
114 diff:'show changes between commits, commit and working tree, etc'
115 fetch:'download objects and refs from another repository'
116 grep:'print lines matching a pattern'
117 init:'create an empty Git repository or reinitialize an existing one'
118 log:'show commit logs'
119 merge:'join two or more development histories together'
120 mv:'move or rename a file, a directory, or a symlink'
121 pull:'fetch from and merge with another repository or a local branch'
122 push:'update remote refs along with associated objects'
123 rebase:'forward-port local commits to the updated upstream head'
124 reset:'reset current HEAD to the specified state'
125 rm:'remove files from the working tree and from the index'
126 show:'show various types of objects'
127 status:'show the working tree status'
128 tag:'create, list, delete or verify a tag object signed with GPG')
129 _describe -t common-commands 'common commands' list && _ret=0
132 __git_zsh_cmd_alias ()
134 local -a list
135 list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
136 _describe -t alias-commands 'aliases' list $* && _ret=0
139 __git_zsh_cmd_all ()
141 local -a list
142 emulate ksh -c __git_compute_all_commands
143 list=( ${=__git_all_commands} )
144 _describe -t all-commands 'all commands' list && _ret=0
147 __git_zsh_main ()
149 local curcontext="$curcontext" state state_descr line
150 typeset -A opt_args
151 local -a orig_words
153 orig_words=( ${words[@]} )
155 _arguments -C \
156 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
157 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
158 '--git-dir=-[set the path to the repository]: :_directories' \
159 '--bare[treat the repository as a bare repository]' \
160 '(- :)--version[prints the git suite version]' \
161 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
162 '--html-path[print the path where git''s HTML documentation is installed]' \
163 '--info-path[print the path where the Info files are installed]' \
164 '--man-path[print the manpath (see `man(1)`) for the man pages]' \
165 '--work-tree=-[set the path to the working tree]: :_directories' \
166 '--namespace=-[set the git namespace]' \
167 '--no-replace-objects[do not use replacement refs to replace git objects]' \
168 '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
169 '(-): :->command' \
170 '(-)*:: :->arg' && return
172 case $state in
173 (command)
174 _alternative \
175 'alias-commands:alias:__git_zsh_cmd_alias' \
176 'common-commands:common:__git_zsh_cmd_common' \
177 'all-commands:all:__git_zsh_cmd_all' && _ret=0
179 (arg)
180 local command="${words[1]}" __git_dir
182 if (( $+opt_args[--bare] )); then
183 __git_dir='.'
184 else
185 __git_dir=${opt_args[--git-dir]}
188 (( $+opt_args[--help] )) && command='help'
190 words=( ${orig_words[@]} )
192 __git_zsh_bash_func $command
194 esac
197 _git ()
199 local _ret=1
200 local cur cword prev
202 cur=${words[CURRENT]}
203 prev=${words[CURRENT-1]}
204 let cword=CURRENT-1
206 if (( $+functions[__${service}_zsh_main] )); then
207 __${service}_zsh_main
208 else
209 emulate ksh -c __${service}_main
212 let _ret && _default && _ret=0
213 return _ret
216 _git