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