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