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