]>
Commit | Line | Data |
---|---|---|
c9407860 FC |
1 | #compdef git gitk |
2 | ||
3 | # zsh completion wrapper for git | |
4 | # | |
af806a2c | 5 | # Copyright (c) 2012-2020 Felipe Contreras <felipe.contreras@gmail.com> |
4911589b | 6 | # |
5eb25bcf FC |
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: | |
c9407860 | 12 | # |
3646b1a5 | 13 | # fpath=(~/.zsh $fpath) |
cb9dcbdb FC |
14 | # |
15 | # You need git's bash completion script installed. By default bash-completion's | |
98fc2684 | 16 | # location will be used (e.g. pkg-config --variable=completionsdir bash-completion). |
cb9dcbdb FC |
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 | # | |
c9407860 | 23 | |
4911589b FC |
24 | zstyle -T ':completion:*:*:git:*' tag-order && \ |
25 | zstyle ':completion:*:*:git:*' tag-order 'common-commands' | |
26 | ||
c9407860 | 27 | zstyle -s ":completion:*:*:git:*" script script |
3646b1a5 FC |
28 | if [ -z "$script" ]; then |
29 | local -a locations | |
98fc2684 FC |
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 | ||
3646b1a5 | 35 | locations=( |
44b37abb | 36 | "$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash |
98fc2684 FC |
37 | "$HOME/.local/share/bash-completion/completions/git" |
38 | "$bash_completion/git" | |
39 | '/etc/bash_completion.d/git' # old debian | |
3646b1a5 FC |
40 | ) |
41 | for e in $locations; do | |
42 | test -f $e && script="$e" && break | |
43 | done | |
44 | fi | |
3791968b FC |
45 | |
46 | local old_complete="$functions[complete]" | |
47 | functions[complete]=: | |
61d48c66 | 48 | GIT_SOURCING_ZSH_COMPLETION=y . "$script" |
3791968b | 49 | functions[complete]="$old_complete" |
c9407860 FC |
50 | |
51 | __gitcomp () | |
52 | { | |
53 | emulate -L zsh | |
54 | ||
55 | local cur_="${3-$cur}" | |
56 | ||
57 | case "$cur_" in | |
58 | --*=) | |
59 | ;; | |
aa1f1f80 FC |
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 | ;; | |
c9407860 FC |
77 | *) |
78 | local c IFS=$' \t\n' | |
79 | local -a array | |
80 | for c in ${=1}; do | |
aa1f1f80 FC |
81 | if [[ $c == "--" ]]; then |
82 | c="--no-...${4-}" | |
83 | array+=("$c ") | |
84 | break | |
85 | fi | |
c9407860 FC |
86 | c="$c${4-}" |
87 | case $c in | |
bbd7f458 | 88 | --*=|*.) ;; |
c9407860 FC |
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 | ||
fef56eb0 SG |
99 | __gitcomp_direct () |
100 | { | |
101 | emulate -L zsh | |
102 | ||
fef56eb0 | 103 | compset -P '*[=:]' |
ecaf7989 | 104 | compadd -Q -S '' -- ${(f)1} && _ret=0 |
fef56eb0 SG |
105 | } |
106 | ||
c9407860 FC |
107 | __gitcomp_nl () |
108 | { | |
109 | emulate -L zsh | |
110 | ||
c9407860 | 111 | compset -P '*[=:]' |
2f459b00 | 112 | compadd -Q -S "${4- }" -p "${2-}" -- ${(f)1} && _ret=0 |
c9407860 FC |
113 | } |
114 | ||
46af9b37 | 115 | __gitcomp_file () |
f33c2c0f | 116 | { |
46af9b37 FC |
117 | emulate -L zsh |
118 | ||
c5dd0c86 | 119 | compset -P '*[=:]' |
46af9b37 | 120 | compadd -f -p "${2-}" -- ${(f)1} && _ret=0 |
f33c2c0f RR |
121 | } |
122 | ||
46af9b37 | 123 | __gitcomp_direct_append () |
7b003420 | 124 | { |
46af9b37 | 125 | __gitcomp_direct "$@" |
7b003420 SG |
126 | } |
127 | ||
46af9b37 | 128 | __gitcomp_nl_append () |
926eb7ba | 129 | { |
46af9b37 FC |
130 | __gitcomp_nl "$@" |
131 | } | |
926eb7ba | 132 | |
46af9b37 FC |
133 | __gitcomp_file_direct () |
134 | { | |
135 | __gitcomp_file "$1" "" | |
926eb7ba MM |
136 | } |
137 | ||
35a4170d FC |
138 | _git_zsh () |
139 | { | |
140 | __gitcomp "v1.1" | |
141 | } | |
142 | ||
9a397ea5 FC |
143 | __git_complete_command () |
144 | { | |
145 | emulate -L zsh | |
146 | ||
147 | local command="$1" | |
148 | local completion_func="_git_${command//-/_}" | |
149 | if (( $+functions[$completion_func] )); then | |
150 | emulate ksh -c $completion_func | |
151 | return 0 | |
152 | else | |
153 | return 1 | |
154 | fi | |
155 | } | |
156 | ||
4911589b FC |
157 | __git_zsh_bash_func () |
158 | { | |
159 | emulate -L ksh | |
160 | ||
161 | local command=$1 | |
162 | ||
9a397ea5 | 163 | __git_complete_command "$command" && return |
4911589b FC |
164 | |
165 | local expansion=$(__git_aliased_command "$command") | |
166 | if [ -n "$expansion" ]; then | |
880111c1 | 167 | words[1]=$expansion |
9a397ea5 | 168 | __git_complete_command "$expansion" |
4911589b FC |
169 | fi |
170 | } | |
171 | ||
172 | __git_zsh_cmd_common () | |
173 | { | |
174 | local -a list | |
175 | list=( | |
176 | add:'add file contents to the index' | |
177 | bisect:'find by binary search the change that introduced a bug' | |
178 | branch:'list, create, or delete branches' | |
179 | checkout:'checkout a branch or paths to the working tree' | |
180 | clone:'clone a repository into a new directory' | |
181 | commit:'record changes to the repository' | |
182 | diff:'show changes between commits, commit and working tree, etc' | |
183 | fetch:'download objects and refs from another repository' | |
184 | grep:'print lines matching a pattern' | |
185 | init:'create an empty Git repository or reinitialize an existing one' | |
186 | log:'show commit logs' | |
187 | merge:'join two or more development histories together' | |
188 | mv:'move or rename a file, a directory, or a symlink' | |
189 | pull:'fetch from and merge with another repository or a local branch' | |
190 | push:'update remote refs along with associated objects' | |
191 | rebase:'forward-port local commits to the updated upstream head' | |
192 | reset:'reset current HEAD to the specified state' | |
051cc549 | 193 | restore:'restore working tree files' |
4911589b FC |
194 | rm:'remove files from the working tree and from the index' |
195 | show:'show various types of objects' | |
196 | status:'show the working tree status' | |
051cc549 | 197 | switch:'switch branches' |
4911589b FC |
198 | tag:'create, list, delete or verify a tag object signed with GPG') |
199 | _describe -t common-commands 'common commands' list && _ret=0 | |
200 | } | |
201 | ||
202 | __git_zsh_cmd_alias () | |
203 | { | |
204 | local -a list | |
2769e567 FC |
205 | list=(${${(0)"$(git config -z --get-regexp '^alias\.*')"}#alias.}) |
206 | list=(${(f)"$(printf "%s:alias for '%s'\n" ${(f@)list})"}) | |
207 | _describe -t alias-commands 'aliases' list && _ret=0 | |
4911589b FC |
208 | } |
209 | ||
210 | __git_zsh_cmd_all () | |
211 | { | |
212 | local -a list | |
213 | emulate ksh -c __git_compute_all_commands | |
214 | list=( ${=__git_all_commands} ) | |
215 | _describe -t all-commands 'all commands' list && _ret=0 | |
216 | } | |
217 | ||
218 | __git_zsh_main () | |
219 | { | |
220 | local curcontext="$curcontext" state state_descr line | |
221 | typeset -A opt_args | |
222 | local -a orig_words | |
223 | ||
224 | orig_words=( ${words[@]} ) | |
225 | ||
226 | _arguments -C \ | |
227 | '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \ | |
228 | '(-p --paginate)--no-pager[do not pipe git output into a pager]' \ | |
229 | '--git-dir=-[set the path to the repository]: :_directories' \ | |
230 | '--bare[treat the repository as a bare repository]' \ | |
231 | '(- :)--version[prints the git suite version]' \ | |
232 | '--exec-path=-[path to where your core git programs are installed]:: :_directories' \ | |
233 | '--html-path[print the path where git''s HTML documentation is installed]' \ | |
234 | '--info-path[print the path where the Info files are installed]' \ | |
235 | '--man-path[print the manpath (see `man(1)`) for the man pages]' \ | |
236 | '--work-tree=-[set the path to the working tree]: :_directories' \ | |
237 | '--namespace=-[set the git namespace]' \ | |
238 | '--no-replace-objects[do not use replacement refs to replace git objects]' \ | |
239 | '(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \ | |
240 | '(-): :->command' \ | |
241 | '(-)*:: :->arg' && return | |
242 | ||
243 | case $state in | |
244 | (command) | |
9d760527 FC |
245 | _tags common-commands alias-commands all-commands |
246 | while _tags; do | |
247 | _requested common-commands && __git_zsh_cmd_common | |
248 | _requested alias-commands && __git_zsh_cmd_alias | |
249 | _requested all-commands && __git_zsh_cmd_all | |
250 | let _ret || break | |
251 | done | |
4911589b FC |
252 | ;; |
253 | (arg) | |
254 | local command="${words[1]}" __git_dir | |
255 | ||
256 | if (( $+opt_args[--bare] )); then | |
257 | __git_dir='.' | |
258 | else | |
259 | __git_dir=${opt_args[--git-dir]} | |
260 | fi | |
261 | ||
262 | (( $+opt_args[--help] )) && command='help' | |
263 | ||
264 | words=( ${orig_words[@]} ) | |
265 | ||
266 | __git_zsh_bash_func $command | |
267 | ;; | |
268 | esac | |
269 | } | |
270 | ||
c9407860 FC |
271 | _git () |
272 | { | |
273 | local _ret=1 | |
1ca6d4bc FC |
274 | local cur cword prev |
275 | ||
276 | cur=${words[CURRENT]} | |
277 | prev=${words[CURRENT-1]} | |
278 | let cword=CURRENT-1 | |
279 | ||
4911589b FC |
280 | if (( $+functions[__${service}_zsh_main] )); then |
281 | __${service}_zsh_main | |
81f717bb | 282 | elif (( $+functions[__${service}_main] )); then |
4911589b | 283 | emulate ksh -c __${service}_main |
81f717bb FC |
284 | elif (( $+functions[_${service}] )); then |
285 | emulate ksh -c _${service} | |
286 | elif (( $+functions[_${service//-/_}] )); then | |
287 | emulate ksh -c _${service//-/_} | |
4911589b | 288 | fi |
1ca6d4bc | 289 | |
6606a69f | 290 | let _ret && _default && _ret=0 |
c9407860 FC |
291 | return _ret |
292 | } | |
293 | ||
294 | _git |