_git 6.93 KB
Newer Older
Felipe Contreras's avatar
Felipe Contreras committed
1
2
3
4
#compdef git gitk

# zsh completion wrapper for git
#
5
# Copyright (c) 2012-2020 Felipe Contreras <felipe.contreras@gmail.com>
Felipe Contreras's avatar
Felipe Contreras committed
6
#
7
8
# The recommended way to install this script is to make a copy of it as a
# file named '_git' inside any directory in your fpath.
9
#
10
11
# For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
# and then add the following to your ~/.zshrc file:
Felipe Contreras's avatar
Felipe Contreras committed
12
#
13
#  fpath=(~/.zsh $fpath)
Felipe Contreras's avatar
Felipe Contreras committed
14
#
15
16
17
18
19
20
21
# You need git's bash completion script installed. By default bash-completion's
# location will be used (e.g. pkg-config --variable=completionsdir bash-completion).
#
# If your bash completion script is somewhere else, you can specify the
# location in your ~/.zshrc:
#
#  zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
Felipe Contreras's avatar
Felipe Contreras committed
22
23
#

24
25
26
zstyle -T ':completion:*:*:git:*' tag-order && \
	zstyle ':completion:*:*:git:*' tag-order 'common-commands'

Felipe Contreras's avatar
Felipe Contreras committed
27
zstyle -s ":completion:*:*:git:*" script script
28
29
if [ -z "$script" ]; then
	local -a locations
30
31
32
33
34
	local e bash_completion

	bash_completion=$(pkg-config --variable=completionsdir bash-completion 2>/dev/null) ||
		bash_completion='/usr/share/bash-completion/completions/'

35
	locations=(
36
37
		"$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
		"$HOME/.local/share/bash-completion/completions/git"
38
		"$bash_completion/git"
39
		'/etc/bash_completion.d/git' # old debian
40
41
42
43
44
		)
	for e in $locations; do
		test -f $e && script="$e" && break
	done
fi
45
46
47

local old_complete="$functions[complete]"
functions[complete]=:
48
GIT_SOURCING_ZSH_COMPLETION=y . "$script"
49
functions[complete]="$old_complete"
Felipe Contreras's avatar
Felipe Contreras committed
50
51
52
53
54
55
56
57
58
59

__gitcomp ()
{
	emulate -L zsh

	local cur_="${3-$cur}"

	case "$cur_" in
	--*=)
		;;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	--no-*)
		local c IFS=$' \t\n'
		local -a array
		for c in ${=1}; do
			if [[ $c == "--" ]]; then
				continue
			fi
			c="$c${4-}"
			case $c in
			--*=|*.) ;;
			*) c="$c " ;;
			esac
			array+=("$c")
		done
		compset -P '*[=:]'
		compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
		;;
Felipe Contreras's avatar
Felipe Contreras committed
77
78
79
80
	*)
		local c IFS=$' \t\n'
		local -a array
		for c in ${=1}; do
81
82
83
84
85
			if [[ $c == "--" ]]; then
				c="--no-...${4-}"
				array+=("$c ")
				break
			fi
Felipe Contreras's avatar
Felipe Contreras committed
86
87
			c="$c${4-}"
			case $c in
88
			--*=|*.) ;;
Felipe Contreras's avatar
Felipe Contreras committed
89
90
91
92
93
94
95
96
97
98
			*) c="$c " ;;
			esac
			array+=("$c")
		done
		compset -P '*[=:]'
		compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
		;;
	esac
}

99
100
101
102
103
__gitcomp_direct ()
{
	emulate -L zsh

	compset -P '*[=:]'
104
	compadd -Q -S '' -- ${(f)1} && _ret=0
105
106
}

Felipe Contreras's avatar
Felipe Contreras committed
107
108
109
110
111
__gitcomp_nl ()
{
	emulate -L zsh

	compset -P '*[=:]'
112
	compadd -Q -S "${4- }" -p "${2-}" -- ${(f)1} && _ret=0
Felipe Contreras's avatar
Felipe Contreras committed
113
114
}

115
__gitcomp_file ()
116
117
118
{
	emulate -L zsh

119
	compadd -f -p "${2-}" -- ${(f)1} && _ret=0
120
121
}

122
__gitcomp_direct_append ()
123
{
124
	__gitcomp_direct "$@"
125
126
}

127
__gitcomp_nl_append ()
128
{
129
130
	__gitcomp_nl "$@"
}
131

132
133
134
__gitcomp_file_direct ()
{
	__gitcomp_file "$1" ""
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
}

_git_zsh ()
{
	__gitcomp "v1.0"
}

__git_complete_command ()
{
	emulate -L zsh

	local command="$1"
	local completion_func="_git_${command//-/_}"
	if (( $+functions[$completion_func] )); then
		emulate ksh -c $completion_func
		return 0
	else
		return 1
	fi
154
155
}

156
157
158
159
160
161
__git_zsh_bash_func ()
{
	emulate -L ksh

	local command=$1

162
	__git_complete_command "$command" && return
163
164
165

	local expansion=$(__git_aliased_command "$command")
	if [ -n "$expansion" ]; then
166
		words[1]=$expansion
167
		__git_complete_command "$expansion"
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
	fi
}

__git_zsh_cmd_common ()
{
	local -a list
	list=(
	add:'add file contents to the index'
	bisect:'find by binary search the change that introduced a bug'
	branch:'list, create, or delete branches'
	checkout:'checkout a branch or paths to the working tree'
	clone:'clone a repository into a new directory'
	commit:'record changes to the repository'
	diff:'show changes between commits, commit and working tree, etc'
	fetch:'download objects and refs from another repository'
	grep:'print lines matching a pattern'
	init:'create an empty Git repository or reinitialize an existing one'
	log:'show commit logs'
	merge:'join two or more development histories together'
	mv:'move or rename a file, a directory, or a symlink'
	pull:'fetch from and merge with another repository or a local branch'
	push:'update remote refs along with associated objects'
	rebase:'forward-port local commits to the updated upstream head'
	reset:'reset current HEAD to the specified state'
192
	restore:'restore working tree files'
193
194
195
	rm:'remove files from the working tree and from the index'
	show:'show various types of objects'
	status:'show the working tree status'
196
	switch:'switch branches'
197
198
199
200
201
202
203
	tag:'create, list, delete or verify a tag object signed with GPG')
	_describe -t common-commands 'common commands' list && _ret=0
}

__git_zsh_cmd_alias ()
{
	local -a list
204
205
206
	list=(${${(0)"$(git config -z --get-regexp '^alias\.*')"}#alias.})
	list=(${(f)"$(printf "%s:alias for '%s'\n" ${(f@)list})"})
	_describe -t alias-commands 'aliases' list && _ret=0
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
}

__git_zsh_cmd_all ()
{
	local -a list
	emulate ksh -c __git_compute_all_commands
	list=( ${=__git_all_commands} )
	_describe -t all-commands 'all commands' list && _ret=0
}

__git_zsh_main ()
{
	local curcontext="$curcontext" state state_descr line
	typeset -A opt_args
	local -a orig_words

	orig_words=( ${words[@]} )

	_arguments -C \
		'(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
		'(-p --paginate)--no-pager[do not pipe git output into a pager]' \
		'--git-dir=-[set the path to the repository]: :_directories' \
		'--bare[treat the repository as a bare repository]' \
		'(- :)--version[prints the git suite version]' \
		'--exec-path=-[path to where your core git programs are installed]:: :_directories' \
		'--html-path[print the path where git''s HTML documentation is installed]' \
		'--info-path[print the path where the Info files are installed]' \
		'--man-path[print the manpath (see `man(1)`) for the man pages]' \
		'--work-tree=-[set the path to the working tree]: :_directories' \
		'--namespace=-[set the git namespace]' \
		'--no-replace-objects[do not use replacement refs to replace git objects]' \
		'(- :)--help[prints the synopsis and a list of the most commonly used commands]: :->arg' \
		'(-): :->command' \
		'(-)*:: :->arg' && return

	case $state in
	(command)
244
245
246
247
248
249
250
		_tags common-commands alias-commands all-commands
		while _tags; do
			_requested common-commands && __git_zsh_cmd_common
			_requested alias-commands && __git_zsh_cmd_alias
			_requested all-commands && __git_zsh_cmd_all
			let _ret || break
		done
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
		;;
	(arg)
		local command="${words[1]}" __git_dir

		if (( $+opt_args[--bare] )); then
			__git_dir='.'
		else
			__git_dir=${opt_args[--git-dir]}
		fi

		(( $+opt_args[--help] )) && command='help'

		words=( ${orig_words[@]} )

		__git_zsh_bash_func $command
		;;
	esac
}

Felipe Contreras's avatar
Felipe Contreras committed
270
271
272
_git ()
{
	local _ret=1
273
274
275
276
277
278
279
280
	local cur cword prev

	cur=${words[CURRENT]}
	prev=${words[CURRENT-1]}
	let cword=CURRENT-1

	if (( $+functions[__${service}_zsh_main] )); then
		__${service}_zsh_main
281
	elif (( $+functions[__${service}_main] )); then
282
		emulate ksh -c __${service}_main
283
284
	elif (( $+functions[_${service}] )); then
		emulate ksh -c _${service}
285
286
	elif ((	$+functions[_${service//-/_}] )); then
		emulate ksh -c _${service//-/_}
287
288
289
	fi

	let _ret && _default && _ret=0
Felipe Contreras's avatar
Felipe Contreras committed
290
291
292
293
	return _ret
}

_git