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