Parent directory

_git

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