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