2012-11-23 00:38:39 +08:00
|
|
|
#compdef git gitk
|
|
|
|
|
|
|
|
# zsh completion wrapper for git
|
|
|
|
#
|
2020-10-24 20:12:23 +08:00
|
|
|
# Copyright (c) 2012-2020 Felipe Contreras <felipe.contreras@gmail.com>
|
2012-11-23 00:38:39 +08:00
|
|
|
#
|
2020-10-24 20:12:23 +08:00
|
|
|
# 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.
|
2013-08-24 10:58:29 +08:00
|
|
|
#
|
2020-10-24 20:12:23 +08:00
|
|
|
# For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
|
|
|
|
# and then add the following to your ~/.zshrc file:
|
2012-11-23 00:38:39 +08:00
|
|
|
#
|
2020-10-24 20:12:23 +08:00
|
|
|
# fpath=(~/.zsh $fpath)
|
2012-11-23 00:38:39 +08:00
|
|
|
#
|
2020-10-24 20:12:23 +08:00
|
|
|
# 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
|
2012-11-23 00:38:39 +08:00
|
|
|
#
|
|
|
|
|
2013-08-24 10:58:29 +08:00
|
|
|
zstyle -T ':completion:*:*:git:*' tag-order && \
|
|
|
|
zstyle ':completion:*:*:git:*' tag-order 'common-commands'
|
|
|
|
|
2012-11-23 00:38:39 +08:00
|
|
|
zstyle -s ":completion:*:*:git:*" script script
|
2013-08-24 10:58:29 +08:00
|
|
|
if [ -z "$script" ]; then
|
|
|
|
local -a locations
|
2020-10-27 18:14:39 +08:00
|
|
|
local e bash_completion
|
|
|
|
|
|
|
|
bash_completion=$(pkg-config --variable=completionsdir bash-completion 2>/dev/null) ||
|
|
|
|
bash_completion='/usr/share/bash-completion/completions/'
|
|
|
|
|
2013-08-24 10:58:29 +08:00
|
|
|
locations=(
|
2020-10-24 20:12:23 +08:00
|
|
|
"$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
|
|
|
|
"$HOME/.local/share/bash-completion/completions/git"
|
2020-10-27 18:14:39 +08:00
|
|
|
"$bash_completion/git"
|
2020-10-24 20:12:23 +08:00
|
|
|
'/etc/bash_completion.d/git' # old debian
|
2013-08-24 10:58:29 +08:00
|
|
|
)
|
|
|
|
for e in $locations; do
|
|
|
|
test -f $e && script="$e" && break
|
|
|
|
done
|
|
|
|
fi
|
2020-10-27 18:14:39 +08:00
|
|
|
|
|
|
|
local old_complete="$functions[complete]"
|
|
|
|
functions[complete]=:
|
2020-11-20 02:45:19 +08:00
|
|
|
COMP_WORDBREAKS=':'
|
2020-10-24 20:12:23 +08:00
|
|
|
GIT_SOURCING_ZSH_COMPLETION=y . "$script"
|
2020-10-27 18:14:39 +08:00
|
|
|
functions[complete]="$old_complete"
|
2012-11-23 00:38:39 +08:00
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
__gitcompadd ()
|
|
|
|
{
|
|
|
|
compadd -Q -p "${2-}" -S "${3- }" ${@[4,-1]} -- ${=1} && _ret=0
|
|
|
|
}
|
|
|
|
|
2012-11-23 00:38:39 +08:00
|
|
|
__gitcomp ()
|
|
|
|
{
|
|
|
|
emulate -L zsh
|
|
|
|
|
|
|
|
local cur_="${3-$cur}"
|
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
[[ "$cur_" == *= ]] && return
|
2012-11-23 00:38:39 +08:00
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
local c IFS=$' \t\n' sfx
|
|
|
|
for c in ${=1}; do
|
|
|
|
if [[ $c == "--" ]]; then
|
|
|
|
[[ "$cur_" == --no-* ]] && continue
|
|
|
|
__gitcompadd "--no-..."
|
|
|
|
break
|
|
|
|
fi
|
2019-04-09 02:06:17 +08:00
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
if [[ -z "${4-}" ]]; then
|
|
|
|
case $c in
|
|
|
|
*=) c="${c%=}"; sfx="=" ;;
|
|
|
|
*.) sfx="" ;;
|
|
|
|
*) sfx=" " ;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
sfx="$4"
|
|
|
|
fi
|
|
|
|
__gitcompadd "$c" "${2-}" "$sfx" -q
|
|
|
|
done
|
2019-04-09 02:06:17 +08:00
|
|
|
}
|
|
|
|
|
2012-11-23 00:38:39 +08:00
|
|
|
__gitcomp_nl ()
|
|
|
|
{
|
|
|
|
emulate -L zsh
|
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
IFS=$'\n' __gitcompadd "$1" "${2-}" "${4- }"
|
2012-11-23 00:38:39 +08:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:14:39 +08:00
|
|
|
__gitcomp_file ()
|
2014-04-22 10:11:49 +08:00
|
|
|
{
|
|
|
|
emulate -L zsh
|
|
|
|
|
2020-10-27 18:14:39 +08:00
|
|
|
compadd -f -p "${2-}" -- ${(f)1} && _ret=0
|
2020-10-24 20:12:23 +08:00
|
|
|
}
|
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
__gitcomp_direct ()
|
2020-10-24 20:12:23 +08:00
|
|
|
{
|
2020-11-20 02:45:19 +08:00
|
|
|
__gitcomp_nl "$1" "" "" ""
|
|
|
|
}
|
|
|
|
|
|
|
|
__gitcomp_file_direct ()
|
|
|
|
{
|
|
|
|
__gitcomp_file "$1" ""
|
2014-04-22 10:11:49 +08:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:14:39 +08:00
|
|
|
__gitcomp_nl_append ()
|
2013-04-21 14:34:44 +08:00
|
|
|
{
|
2020-10-27 18:14:39 +08:00
|
|
|
__gitcomp_nl "$@"
|
|
|
|
}
|
2013-04-21 14:34:44 +08:00
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
__gitcomp_direct_append ()
|
2020-10-27 18:14:39 +08:00
|
|
|
{
|
2020-11-20 02:45:19 +08:00
|
|
|
__gitcomp_direct "$@"
|
2020-10-24 20:12:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_git_zsh ()
|
|
|
|
{
|
2020-11-20 02:45:19 +08:00
|
|
|
__gitcomp "v1.2"
|
2020-10-24 20:12:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
__git_complete_command ()
|
|
|
|
{
|
|
|
|
emulate -L zsh
|
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
compset -P '*[=:]'
|
|
|
|
|
2020-10-24 20:12:23 +08:00
|
|
|
local command="$1"
|
|
|
|
local completion_func="_git_${command//-/_}"
|
|
|
|
if (( $+functions[$completion_func] )); then
|
|
|
|
emulate ksh -c $completion_func
|
|
|
|
return 0
|
2020-11-20 02:45:19 +08:00
|
|
|
elif emulate ksh -c "__git_support_parseopt_helper $command"; then
|
|
|
|
emulate ksh -c "__git_complete_common $command"
|
|
|
|
return 0
|
2020-10-24 20:12:23 +08:00
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
2013-04-21 14:34:44 +08:00
|
|
|
}
|
|
|
|
|
2013-08-24 10:58:29 +08:00
|
|
|
__git_zsh_bash_func ()
|
|
|
|
{
|
|
|
|
emulate -L ksh
|
|
|
|
|
|
|
|
local command=$1
|
|
|
|
|
2020-10-24 20:12:23 +08:00
|
|
|
__git_complete_command "$command" && return
|
2013-08-24 10:58:29 +08:00
|
|
|
|
|
|
|
local expansion=$(__git_aliased_command "$command")
|
|
|
|
if [ -n "$expansion" ]; then
|
2016-05-18 16:07:09 +08:00
|
|
|
words[1]=$expansion
|
2020-10-24 20:12:23 +08:00
|
|
|
__git_complete_command "$expansion"
|
2013-08-24 10:58:29 +08:00
|
|
|
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'
|
2020-10-24 20:12:23 +08:00
|
|
|
restore:'restore working tree files'
|
2013-08-24 10:58:29 +08:00
|
|
|
rm:'remove files from the working tree and from the index'
|
|
|
|
show:'show various types of objects'
|
|
|
|
status:'show the working tree status'
|
2020-10-24 20:12:23 +08:00
|
|
|
switch:'switch branches'
|
2013-08-24 10:58:29 +08:00
|
|
|
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
|
2020-10-24 20:12:23 +08:00
|
|
|
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
|
2013-08-24 10:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
__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
|
2020-11-20 02:45:19 +08:00
|
|
|
local -a orig_words __git_C_args
|
2013-08-24 10:58:29 +08:00
|
|
|
|
|
|
|
orig_words=( ${words[@]} )
|
|
|
|
|
|
|
|
_arguments -C \
|
2020-11-20 02:45:19 +08:00
|
|
|
'(-p --paginate -P --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \
|
|
|
|
'(-p --paginate -P --no-pager)'{-P,--no-pager}'[do not pipe git output into a pager]' \
|
|
|
|
'(--bare)--git-dir=[set the path to the repository]: :_directories' \
|
|
|
|
'(--git-dir)--bare[treat the repository as a bare repository]' \
|
2013-08-24 10:58:29 +08:00
|
|
|
'(- :)--version[prints the git suite version]' \
|
2020-11-20 02:45:19 +08:00
|
|
|
'--exec-path=[path to where your core git programs are installed]: :_directories' \
|
|
|
|
'(- :)--exec-path[print the path where your core git programs are installed]' \
|
|
|
|
'(- :)--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]:' \
|
2013-08-24 10:58:29 +08:00
|
|
|
'--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' \
|
2020-11-20 02:45:19 +08:00
|
|
|
'*-C[run as if git was started in the given path]: :_directories' \
|
|
|
|
'*-c[pass a configuration parameter to the command]: :->config' \
|
2013-08-24 10:58:29 +08:00
|
|
|
'(-): :->command' \
|
|
|
|
'(-)*:: :->arg' && return
|
|
|
|
|
|
|
|
case $state in
|
|
|
|
(command)
|
2020-10-24 20:12:23 +08:00
|
|
|
_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
|
2013-08-24 10:58:29 +08:00
|
|
|
;;
|
2020-11-20 02:45:19 +08:00
|
|
|
(config)
|
|
|
|
compset -P '*[=:]'
|
|
|
|
emulate ksh -c __git_complete_config_variable_name_and_value
|
|
|
|
;;
|
2013-08-24 10:58:29 +08:00
|
|
|
(arg)
|
|
|
|
local command="${words[1]}" __git_dir
|
|
|
|
|
|
|
|
if (( $+opt_args[--bare] )); then
|
|
|
|
__git_dir='.'
|
|
|
|
else
|
2020-11-20 02:45:19 +08:00
|
|
|
__git_dir=${~opt_args[--git-dir]}
|
2013-08-24 10:58:29 +08:00
|
|
|
fi
|
|
|
|
|
2020-11-20 02:45:19 +08:00
|
|
|
for x in ${(s.:.)opt_args[-C]}; do
|
|
|
|
__git_C_args+=('-C' ${~x})
|
|
|
|
done
|
|
|
|
|
2013-08-24 10:58:29 +08:00
|
|
|
(( $+opt_args[--help] )) && command='help'
|
|
|
|
|
|
|
|
words=( ${orig_words[@]} )
|
|
|
|
|
|
|
|
__git_zsh_bash_func $command
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2012-11-23 00:38:39 +08:00
|
|
|
_git ()
|
|
|
|
{
|
|
|
|
local _ret=1
|
2013-08-24 10:58:29 +08:00
|
|
|
local cur cword prev
|
|
|
|
|
|
|
|
cur=${words[CURRENT]}
|
|
|
|
prev=${words[CURRENT-1]}
|
|
|
|
let cword=CURRENT-1
|
|
|
|
|
|
|
|
if (( $+functions[__${service}_zsh_main] )); then
|
|
|
|
__${service}_zsh_main
|
2020-05-06 02:32:52 +08:00
|
|
|
elif (( $+functions[__${service}_main] )); then
|
2013-08-24 10:58:29 +08:00
|
|
|
emulate ksh -c __${service}_main
|
2020-05-06 02:32:52 +08:00
|
|
|
elif (( $+functions[_${service}] )); then
|
|
|
|
emulate ksh -c _${service}
|
2020-10-24 22:33:43 +08:00
|
|
|
elif (( $+functions[_${service//-/_}] )); then
|
|
|
|
emulate ksh -c _${service//-/_}
|
2013-08-24 10:58:29 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
let _ret && _default && _ret=0
|
2012-11-23 00:38:39 +08:00
|
|
|
return _ret
|
|
|
|
}
|
|
|
|
|
|
|
|
_git
|