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