]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/completion/git-completion.zsh
completion: zsh: fix completion for --no-.. options
[thirdparty/git.git] / contrib / completion / git-completion.zsh
CommitLineData
c9407860
FC
1#compdef git gitk
2
3# zsh completion wrapper for git
4#
4911589b
FC
5# Copyright (c) 2012-2013 Felipe Contreras <felipe.contreras@gmail.com>
6#
5eb25bcf
FC
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:
c9407860 12#
3646b1a5 13# fpath=(~/.zsh $fpath)
cb9dcbdb
FC
14#
15# You need git's bash completion script installed. By default bash-completion's
98fc2684 16# location will be used (e.g. pkg-config --variable=completionsdir bash-completion).
cb9dcbdb
FC
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#
c9407860
FC
23
24complete ()
25{
26 # do nothing
27 return 0
28}
29
4911589b
FC
30zstyle -T ':completion:*:*:git:*' tag-order && \
31 zstyle ':completion:*:*:git:*' tag-order 'common-commands'
32
c9407860 33zstyle -s ":completion:*:*:git:*" script script
3646b1a5
FC
34if [ -z "$script" ]; then
35 local -a locations
98fc2684
FC
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
3646b1a5 41 locations=(
44b37abb 42 "$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
98fc2684
FC
43 "$HOME/.local/share/bash-completion/completions/git"
44 "$bash_completion/git"
45 '/etc/bash_completion.d/git' # old debian
3646b1a5
FC
46 )
47 for e in $locations; do
48 test -f $e && script="$e" && break
49 done
50fi
61d48c66 51GIT_SOURCING_ZSH_COMPLETION=y . "$script"
c9407860
FC
52
53__gitcomp ()
54{
55 emulate -L zsh
56
57 local cur_="${3-$cur}"
58
59 case "$cur_" in
60 --*=)
61 ;;
aa1f1f80
FC
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 ;;
c9407860
FC
79 *)
80 local c IFS=$' \t\n'
81 local -a array
82 for c in ${=1}; do
aa1f1f80
FC
83 if [[ $c == "--" ]]; then
84 c="--no-...${4-}"
85 array+=("$c ")
86 break
87 fi
c9407860
FC
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
fef56eb0
SG
101__gitcomp_direct ()
102{
103 emulate -L zsh
104
105 local IFS=$'\n'
106 compset -P '*[=:]'
4ce2b516 107 compadd -Q -- ${${=1}% } && _ret=0
fef56eb0
SG
108}
109
c9407860
FC
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
f33c2c0f
RR
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
7b003420
SG
127__gitcomp_file_direct ()
128{
129 emulate -L zsh
130
131 local IFS=$'\n'
132 compset -P '*[=:]'
7a478b36 133 compadd -f -- ${=1} && _ret=0
7b003420
SG
134}
135
926eb7ba
MM
136__gitcomp_file ()
137{
138 emulate -L zsh
139
140 local IFS=$'\n'
141 compset -P '*[=:]'
7a478b36 142 compadd -p "${2-}" -f -- ${=1} && _ret=0
926eb7ba
MM
143}
144
4911589b
FC
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
880111c1 156 words[1]=$expansion
4911589b
FC
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'
051cc549 183 restore:'restore working tree files'
4911589b
FC
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'
051cc549 187 switch:'switch branches'
4911589b
FC
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
c9407860
FC
257_git ()
258{
259 local _ret=1
1ca6d4bc
FC
260 local cur cword prev
261
262 cur=${words[CURRENT]}
263 prev=${words[CURRENT-1]}
264 let cword=CURRENT-1
265
4911589b
FC
266 if (( $+functions[__${service}_zsh_main] )); then
267 __${service}_zsh_main
81f717bb 268 elif (( $+functions[__${service}_main] )); then
4911589b 269 emulate ksh -c __${service}_main
81f717bb
FC
270 elif (( $+functions[_${service}] )); then
271 emulate ksh -c _${service}
272 elif (( $+functions[_${service//-/_}] )); then
273 emulate ksh -c _${service//-/_}
4911589b 274 fi
1ca6d4bc 275
6606a69f 276 let _ret && _default && _ret=0
c9407860
FC
277 return _ret
278}
279
280_git