completion: zsh: update slave script locations
[git] / contrib / completion / git-completion.zsh
1 #compdef git gitk
2
3 # zsh completion wrapper for git
4 #
5 # Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com>
6 #
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.
9 #
10 # For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
11 # and then add the following to your ~/.zshrc file:
12 #
13 #  fpath=(~/.zsh $fpath)
14 #
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).
17 #
18 # If your bash completion script is somewhere else, you can specify the
19 # location in your ~/.zshrc:
20 #
21 #  zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
22 #
23
24 complete ()
25 {
26         # do nothing
27         return 0
28 }
29
30 zstyle -T ':completion:*:*:git:*' tag-order && \
31         zstyle ':completion:*:*:git:*' tag-order 'common-commands'
32
33 zstyle -s ":completion:*:*:git:*" script script
34 if [ -z "$script" ]; then
35         local -a locations
36         local e bash_completion
37
38         bash_completion=$(pkg-config --variable=completionsdir bash-completion 2>/dev/null) ||
39                 bash_completion='/usr/share/bash-completion/completions/'
40
41         locations=(
42                 "$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
43                 "$HOME/.local/share/bash-completion/completions/git"
44                 "$bash_completion/git"
45                 '/etc/bash_completion.d/git' # old debian
46                 )
47         for e in $locations; do
48                 test -f $e && script="$e" && break
49         done
50 fi
51 GIT_SOURCING_ZSH_COMPLETION=y . "$script"
52
53 __gitcomp ()
54 {
55         emulate -L zsh
56
57         local cur_="${3-$cur}"
58
59         case "$cur_" in
60         --*=)
61                 ;;
62         *)
63                 local c IFS=$' \t\n'
64                 local -a array
65                 for c in ${=1}; do
66                         c="$c${4-}"
67                         case $c in
68                         --*=*|*.) ;;
69                         *) c="$c " ;;
70                         esac
71                         array+=("$c")
72                 done
73                 compset -P '*[=:]'
74                 compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
75                 ;;
76         esac
77 }
78
79 __gitcomp_direct ()
80 {
81         emulate -L zsh
82
83         local IFS=$'\n'
84         compset -P '*[=:]'
85         compadd -Q -- ${${=1}% } && _ret=0
86 }
87
88 __gitcomp_nl ()
89 {
90         emulate -L zsh
91
92         local IFS=$'\n'
93         compset -P '*[=:]'
94         compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
95 }
96
97 __gitcomp_nl_append ()
98 {
99         emulate -L zsh
100
101         local IFS=$'\n'
102         compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
103 }
104
105 __gitcomp_file_direct ()
106 {
107         emulate -L zsh
108
109         local IFS=$'\n'
110         compset -P '*[=:]'
111         compadd -f -- ${=1} && _ret=0
112 }
113
114 __gitcomp_file ()
115 {
116         emulate -L zsh
117
118         local IFS=$'\n'
119         compset -P '*[=:]'
120         compadd -p "${2-}" -f -- ${=1} && _ret=0
121 }
122
123 __git_zsh_bash_func ()
124 {
125         emulate -L ksh
126
127         local command=$1
128
129         local completion_func="_git_${command//-/_}"
130         declare -f $completion_func >/dev/null && $completion_func && return
131
132         local expansion=$(__git_aliased_command "$command")
133         if [ -n "$expansion" ]; then
134                 words[1]=$expansion
135                 completion_func="_git_${expansion//-/_}"
136                 declare -f $completion_func >/dev/null && $completion_func
137         fi
138 }
139
140 __git_zsh_cmd_common ()
141 {
142         local -a list
143         list=(
144         add:'add file contents to the index'
145         bisect:'find by binary search the change that introduced a bug'
146         branch:'list, create, or delete branches'
147         checkout:'checkout a branch or paths to the working tree'
148         clone:'clone a repository into a new directory'
149         commit:'record changes to the repository'
150         diff:'show changes between commits, commit and working tree, etc'
151         fetch:'download objects and refs from another repository'
152         grep:'print lines matching a pattern'
153         init:'create an empty Git repository or reinitialize an existing one'
154         log:'show commit logs'
155         merge:'join two or more development histories together'
156         mv:'move or rename a file, a directory, or a symlink'
157         pull:'fetch from and merge with another repository or a local branch'
158         push:'update remote refs along with associated objects'
159         rebase:'forward-port local commits to the updated upstream head'
160         reset:'reset current HEAD to the specified state'
161         restore:'restore working tree files'
162         rm:'remove files from the working tree and from the index'
163         show:'show various types of objects'
164         status:'show the working tree status'
165         switch:'switch branches'
166         tag:'create, list, delete or verify a tag object signed with GPG')
167         _describe -t common-commands 'common commands' list && _ret=0
168 }
169
170 __git_zsh_cmd_alias ()
171 {
172         local -a list
173         list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
174         _describe -t alias-commands 'aliases' list $* && _ret=0
175 }
176
177 __git_zsh_cmd_all ()
178 {
179         local -a list
180         emulate ksh -c __git_compute_all_commands
181         list=( ${=__git_all_commands} )
182         _describe -t all-commands 'all commands' list && _ret=0
183 }
184
185 __git_zsh_main ()
186 {
187         local curcontext="$curcontext" state state_descr line
188         typeset -A opt_args
189         local -a orig_words
190
191         orig_words=( ${words[@]} )
192
193         _arguments -C \
194                 '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
195                 '(-p --paginate)--no-pager[do not pipe git output into a pager]' \
196                 '--git-dir=-[set the path to the repository]: :_directories' \
197                 '--bare[treat the repository as a bare repository]' \
198                 '(- :)--version[prints the git suite version]' \
199                 '--exec-path=-[path to where your core git programs are installed]:: :_directories' \
200                 '--html-path[print the path where git''s HTML documentation is installed]' \
201                 '--info-path[print the path where the Info files are installed]' \
202                 '--man-path[print the manpath (see `man(1)`) for the man pages]' \
203                 '--work-tree=-[set the path to the working tree]: :_directories' \
204                 '--namespace=-[set the git namespace]' \
205                 '--no-replace-objects[do not use replacement refs to replace git objects]' \
206                 '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
207                 '(-): :->command' \
208                 '(-)*:: :->arg' && return
209
210         case $state in
211         (command)
212                 _alternative \
213                          'alias-commands:alias:__git_zsh_cmd_alias' \
214                          'common-commands:common:__git_zsh_cmd_common' \
215                          'all-commands:all:__git_zsh_cmd_all' && _ret=0
216                 ;;
217         (arg)
218                 local command="${words[1]}" __git_dir
219
220                 if (( $+opt_args[--bare] )); then
221                         __git_dir='.'
222                 else
223                         __git_dir=${opt_args[--git-dir]}
224                 fi
225
226                 (( $+opt_args[--help] )) && command='help'
227
228                 words=( ${orig_words[@]} )
229
230                 __git_zsh_bash_func $command
231                 ;;
232         esac
233 }
234
235 _git ()
236 {
237         local _ret=1
238         local cur cword prev
239
240         cur=${words[CURRENT]}
241         prev=${words[CURRENT-1]}
242         let cword=CURRENT-1
243
244         if (( $+functions[__${service}_zsh_main] )); then
245                 __${service}_zsh_main
246         else
247                 emulate ksh -c __${service}_main
248         fi
249
250         let _ret && _default && _ret=0
251         return _ret
252 }
253
254 _git