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