Skip to content

Commit 4560eae

Browse files
committed
Merge branch 'fc/zsh-completion'
Zsh autocompletion (in contrib/) update. * fc/zsh-completion: (29 commits) zsh: update copyright notices completion: bash: remove old compat wrappers completion: bash: cleanup cygwin check completion: bash: trivial cleanup completion: zsh: add simple version check completion: zsh: trivial simplification completion: zsh: add alias descriptions completion: zsh: improve command tags completion: zsh: refactor command completion completion: zsh: shuffle functions around completion: zsh: simplify file_direct completion: zsh: simplify nl_append completion: zsh: trivial cleanup completion: zsh: simplify direct compadd completion: zsh: simplify compadd functions completion: zsh: fix splitting of words completion: zsh: add missing direct_append completion: fix conflict with bashcomp completion: zsh: fix completion for --no-.. options completion: bash: remove zsh wrapper ...
2 parents caf3ca7 + af806a2 commit 4560eae

File tree

4 files changed

+112
-156
lines changed

4 files changed

+112
-156
lines changed

contrib/completion/git-completion.bash

Lines changed: 10 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,13 @@ __gitcomp_builtin ()
416416
local options
417417
eval "options=\${$var-}"
418418

419-
local completion_helper
420-
if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
421-
completion_helper="--git-completion-helper-all"
422-
else
423-
completion_helper="--git-completion-helper"
424-
fi
425-
426419
if [ -z "$options" ]; then
420+
local completion_helper
421+
if [ "$GIT_COMPLETION_SHOW_ALL" = "1" ]; then
422+
completion_helper="--git-completion-helper-all"
423+
else
424+
completion_helper="--git-completion-helper"
425+
fi
427426
# leading and trailing spaces are significant to make
428427
# option removal work correctly.
429428
options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
@@ -3464,88 +3463,8 @@ __gitk_main ()
34643463
__git_complete_revlist
34653464
}
34663465

3467-
if [[ -n ${ZSH_VERSION-} ]] &&
3468-
# Don't define these functions when sourced from 'git-completion.zsh',
3469-
# it has its own implementations.
3470-
[[ -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3471-
echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
3472-
3473-
autoload -U +X compinit && compinit
3474-
3475-
__gitcomp ()
3476-
{
3477-
emulate -L zsh
3478-
3479-
local cur_="${3-$cur}"
3480-
3481-
case "$cur_" in
3482-
--*=)
3483-
;;
3484-
*)
3485-
local c IFS=$' \t\n'
3486-
local -a array
3487-
for c in ${=1}; do
3488-
c="$c${4-}"
3489-
case $c in
3490-
--*=*|*.) ;;
3491-
*) c="$c " ;;
3492-
esac
3493-
array[${#array[@]}+1]="$c"
3494-
done
3495-
compset -P '*[=:]'
3496-
compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
3497-
;;
3498-
esac
3499-
}
3500-
3501-
__gitcomp_direct ()
3502-
{
3503-
emulate -L zsh
3504-
3505-
local IFS=$'\n'
3506-
compset -P '*[=:]'
3507-
compadd -Q -- ${=1} && _ret=0
3508-
}
3509-
3510-
__gitcomp_nl ()
3511-
{
3512-
emulate -L zsh
3513-
3514-
local IFS=$'\n'
3515-
compset -P '*[=:]'
3516-
compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3517-
}
3518-
3519-
__gitcomp_file_direct ()
3520-
{
3521-
emulate -L zsh
3522-
3523-
local IFS=$'\n'
3524-
compset -P '*[=:]'
3525-
compadd -f -- ${=1} && _ret=0
3526-
}
3527-
3528-
__gitcomp_file ()
3529-
{
3530-
emulate -L zsh
3531-
3532-
local IFS=$'\n'
3533-
compset -P '*[=:]'
3534-
compadd -p "${2-}" -f -- ${=1} && _ret=0
3535-
}
3536-
3537-
_git ()
3538-
{
3539-
local _ret=1 cur cword prev
3540-
cur=${words[CURRENT]}
3541-
prev=${words[CURRENT-1]}
3542-
let cword=CURRENT-1
3543-
emulate ksh -c __${service}_main
3544-
let _ret && _default && _ret=0
3545-
return _ret
3546-
}
3547-
3548-
compdef _git git gitk
3466+
if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
3467+
echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
35493468
return
35503469
fi
35513470

@@ -3567,25 +3486,13 @@ __git_complete ()
35673486
|| complete -o default -o nospace -F $wrapper $1
35683487
}
35693488

3570-
# wrapper for backwards compatibility
3571-
_git ()
3572-
{
3573-
__git_wrap__git_main
3574-
}
3575-
3576-
# wrapper for backwards compatibility
3577-
_gitk ()
3578-
{
3579-
__git_wrap__gitk_main
3580-
}
3581-
35823489
__git_complete git __git_main
35833490
__git_complete gitk __gitk_main
35843491

35853492
# The following are necessary only for Cygwin, and only are needed
35863493
# when the user has tab-completed the executable name and consequently
35873494
# included the '.exe' suffix.
35883495
#
3589-
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3590-
__git_complete git.exe __git_main
3496+
if [ "$OSTYPE" = cygwin ]; then
3497+
__git_complete git.exe __git_main
35913498
fi

contrib/completion/git-completion.zsh

Lines changed: 94 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,51 @@
22

33
# zsh completion wrapper for git
44
#
5-
# Copyright (c) 2012-2013 Felipe Contreras <[email protected]>
5+
# Copyright (c) 2012-2020 Felipe Contreras <[email protected]>
66
#
7-
# You need git's bash completion script installed somewhere, by default it
8-
# would be the location bash-completion uses.
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.
99
#
10-
# If your script is somewhere else, you can configure it on your ~/.zshrc:
10+
# For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
11+
# and then add the following to your ~/.zshrc file:
1112
#
12-
# zstyle ':completion:*:*:git:*' script ~/.git-completion.bash
13+
# fpath=(~/.zsh $fpath)
1314
#
14-
# The recommended way to install this script is to make a copy of it in
15-
# ~/.zsh/ directory as ~/.zsh/git-completion.zsh and then add the following
16-
# to your ~/.zshrc file:
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
1722
#
18-
# fpath=(~/.zsh $fpath)
19-
# autoload -Uz compinit && compinit
20-
21-
complete ()
22-
{
23-
# do nothing
24-
return 0
25-
}
2623

2724
zstyle -T ':completion:*:*:git:*' tag-order && \
2825
zstyle ':completion:*:*:git:*' tag-order 'common-commands'
2926

3027
zstyle -s ":completion:*:*:git:*" script script
3128
if [ -z "$script" ]; then
3229
local -a locations
33-
local e
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+
3435
locations=(
35-
$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
36-
'/etc/bash_completion.d/git' # fedora, old debian
37-
'/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
38-
'/usr/share/bash-completion/git' # gentoo
36+
"$(dirname ${funcsourcetrace[1]%:*})"/git-completion.bash
37+
"$HOME/.local/share/bash-completion/completions/git"
38+
"$bash_completion/git"
39+
'/etc/bash_completion.d/git' # old debian
3940
)
4041
for e in $locations; do
4142
test -f $e && script="$e" && break
4243
done
4344
fi
45+
46+
local old_complete="$functions[complete]"
47+
functions[complete]=:
4448
GIT_SOURCING_ZSH_COMPLETION=y . "$script"
49+
functions[complete]="$old_complete"
4550

4651
__gitcomp ()
4752
{
@@ -52,13 +57,35 @@ __gitcomp ()
5257
case "$cur_" in
5358
--*=)
5459
;;
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+
;;
5577
*)
5678
local c IFS=$' \t\n'
5779
local -a array
5880
for c in ${=1}; do
81+
if [[ $c == "--" ]]; then
82+
c="--no-...${4-}"
83+
array+=("$c ")
84+
break
85+
fi
5986
c="$c${4-}"
6087
case $c in
61-
--*=*|*.) ;;
88+
--*=|*.) ;;
6289
*) c="$c " ;;
6390
esac
6491
array+=("$c")
@@ -73,44 +100,57 @@ __gitcomp_direct ()
73100
{
74101
emulate -L zsh
75102

76-
local IFS=$'\n'
77103
compset -P '*[=:]'
78-
compadd -Q -- ${=1} && _ret=0
104+
compadd -Q -S '' -- ${(f)1} && _ret=0
79105
}
80106

81107
__gitcomp_nl ()
82108
{
83109
emulate -L zsh
84110

85-
local IFS=$'\n'
86111
compset -P '*[=:]'
87-
compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
112+
compadd -Q -S "${4- }" -p "${2-}" -- ${(f)1} && _ret=0
88113
}
89114

90-
__gitcomp_nl_append ()
115+
__gitcomp_file ()
91116
{
92117
emulate -L zsh
93118

94-
local IFS=$'\n'
95-
compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
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+
__gitcomp_nl "$@"
96130
}
97131

98132
__gitcomp_file_direct ()
99133
{
100-
emulate -L zsh
134+
__gitcomp_file "$1" ""
135+
}
101136

102-
local IFS=$'\n'
103-
compset -P '*[=:]'
104-
compadd -f -- ${=1} && _ret=0
137+
_git_zsh ()
138+
{
139+
__gitcomp "v1.1"
105140
}
106141

107-
__gitcomp_file ()
142+
__git_complete_command ()
108143
{
109144
emulate -L zsh
110145

111-
local IFS=$'\n'
112-
compset -P '*[=:]'
113-
compadd -p "${2-}" -f -- ${=1} && _ret=0
146+
local command="$1"
147+
local completion_func="_git_${command//-/_}"
148+
if (( $+functions[$completion_func] )); then
149+
emulate ksh -c $completion_func
150+
return 0
151+
else
152+
return 1
153+
fi
114154
}
115155

116156
__git_zsh_bash_func ()
@@ -119,14 +159,12 @@ __git_zsh_bash_func ()
119159

120160
local command=$1
121161

122-
local completion_func="_git_${command//-/_}"
123-
declare -f $completion_func >/dev/null && $completion_func && return
162+
__git_complete_command "$command" && return
124163

125164
local expansion=$(__git_aliased_command "$command")
126165
if [ -n "$expansion" ]; then
127166
words[1]=$expansion
128-
completion_func="_git_${expansion//-/_}"
129-
declare -f $completion_func >/dev/null && $completion_func
167+
__git_complete_command "$expansion"
130168
fi
131169
}
132170

@@ -163,8 +201,9 @@ __git_zsh_cmd_common ()
163201
__git_zsh_cmd_alias ()
164202
{
165203
local -a list
166-
list=(${${${(0)"$(git config -z --get-regexp '^alias\.')"}#alias.}%$'\n'*})
167-
_describe -t alias-commands 'aliases' list $* && _ret=0
204+
list=(${${(0)"$(git config -z --get-regexp '^alias\.*')"}#alias.})
205+
list=(${(f)"$(printf "%s:alias for '%s'\n" ${(f@)list})"})
206+
_describe -t alias-commands 'aliases' list && _ret=0
168207
}
169208

170209
__git_zsh_cmd_all ()
@@ -202,10 +241,13 @@ __git_zsh_main ()
202241

203242
case $state in
204243
(command)
205-
_alternative \
206-
'alias-commands:alias:__git_zsh_cmd_alias' \
207-
'common-commands:common:__git_zsh_cmd_common' \
208-
'all-commands:all:__git_zsh_cmd_all' && _ret=0
244+
_tags common-commands alias-commands all-commands
245+
while _tags; do
246+
_requested common-commands && __git_zsh_cmd_common
247+
_requested alias-commands && __git_zsh_cmd_alias
248+
_requested all-commands && __git_zsh_cmd_all
249+
let _ret || break
250+
done
209251
;;
210252
(arg)
211253
local command="${words[1]}" __git_dir
@@ -236,8 +278,12 @@ _git ()
236278

237279
if (( $+functions[__${service}_zsh_main] )); then
238280
__${service}_zsh_main
239-
else
281+
elif (( $+functions[__${service}_main] )); then
240282
emulate ksh -c __${service}_main
283+
elif (( $+functions[_${service}] )); then
284+
emulate ksh -c _${service}
285+
elif (( $+functions[_${service//-/_}] )); then
286+
emulate ksh -c _${service//-/_}
241287
fi
242288

243289
let _ret && _default && _ret=0

0 commit comments

Comments
 (0)