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