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