Skip to content

Commit a3f5329

Browse files
committed
Repair zsh completions for stg branch
- Completion generally works now; was confused/confusing. - Branch commands are now presented separately from options and args of the default switch command. - Branch commands that operate on any git branch now show completions for all git branches instead of just stgit-enabled branches.
1 parent 2f0a398 commit a3f5329

File tree

1 file changed

+172
-41
lines changed

1 file changed

+172
-41
lines changed

completion/stgit.zsh

Lines changed: 172 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,164 @@
1414
#
1515

1616
_stg-branch() {
17+
local -a subcmd_args
18+
local curcontext="$curcontext" state line
19+
integer ret=1
20+
21+
__stg_add_args_help
22+
__stg_add_args_color
23+
subcmd_args+=(
24+
': :->command'
25+
'*:: :->option-or-argument'
26+
)
27+
28+
_arguments -C $subcmd_args && ret=0
29+
30+
case $state in
31+
(command)
32+
declare -a commands switch_options
33+
commands=(
34+
{-l,--list}':list branches'
35+
{-c,--create}':create and switch to new branch'
36+
'--clone:clone current branch to new branch'
37+
{-r,--rename}':rename existing branch'
38+
{-p,--protect}':prevent stg from modifying branch'
39+
{-u,--unprotect}':allow stg to modify branch'
40+
'--delete:delete branch'
41+
'--cleanup:cleanup stg metadata for branch'
42+
{-d,--describe}':set branch description'
43+
)
44+
switch_options=(
45+
'--merge:merge worktree changes into other branch'
46+
)
47+
_alternative \
48+
'command: : _describe -t commands command commands' \
49+
'switch-option: : _describe -t switch-options switch-option switch_options' \
50+
'branch: : __stg_git_branch_names' && ret=0
51+
;;
52+
53+
(option-or-argument)
54+
curcontext=${curcontext%:*}-$line[1]
55+
case $line[1] in
56+
(--cleanup)
57+
_call_function ret _stg-branch-cleanup ;;
58+
(--clone)
59+
_call_function ret _stg-branch-clone ;;
60+
(-c|--create)
61+
_call_function ret _stg-branch-create ;;
62+
(--delete)
63+
_call_function ret _stg-branch-delete ;;
64+
(-d|--describe|--description)
65+
_call_function ret _stg-branch-describe ;;
66+
(-l|--list)
67+
_call_function ret _stg-branch-list ;;
68+
(-p|--protect)
69+
_call_function ret _stg-branch-protect ;;
70+
(-r|--rename)
71+
_call_function ret _stg-branch-rename ;;
72+
(-u|--unprotect)
73+
_call_function ret _stg-branch-unprotect ;;
74+
75+
# Options and arguments for the default command (switch branch).
76+
(--merge)
77+
_call_function ret _stg-branch-switch has-merge ;;
78+
(*)
79+
_call_function ret _stg-branch-switch has-branch ;;
80+
esac
81+
;;
82+
esac
83+
84+
return ret
85+
}
86+
87+
_stg-branch-switch() {
88+
local -a subcmd_args
89+
__stg_add_args_help
90+
__stg_add_args_color
91+
if [ "$1" != "has-merge" ]; then
92+
subcmd_args+=('--merge[merge worktree changes into other branch]')
93+
fi
94+
if [ "$1" != "has-branch" ]; then
95+
subcmd_args+=(':branch:__stg_git_branch_names')
96+
fi
97+
_arguments -S $subcmd_args
98+
}
99+
100+
_stg-branch-cleanup() {
101+
local -a subcmd_args
102+
__stg_add_args_help
103+
__stg_add_args_color
104+
subcmd_args+=(
105+
'--force[force cleanup when series is non-empty]'
106+
':stgit branch:__stg_stgit_branch_names'
107+
)
108+
_arguments -S $subcmd_args
109+
}
110+
111+
_stg-branch-clone() {
112+
local -a subcmd_args
113+
__stg_add_args_help
114+
__stg_add_args_color
115+
_arguments $subcmd_args ':new-branch:'
116+
}
117+
118+
_stg-branch-create() {
17119
local -a subcmd_args
18120
__stg_add_args_help
121+
__stg_add_args_color
122+
_arguments -s -S $subcmd_args ':new-branch:' ':committish:'
123+
}
124+
125+
_stg-branch-delete() {
126+
local -a subcmd_args
127+
__stg_add_args_help
128+
__stg_add_args_color
19129
subcmd_args+=(
20-
- group-list
21-
'--list[list branches]'
22-
- group-switch
23-
'--merge[merge worktree changes into other branch]'
24-
':branch:__stg_branch_stgit'
25-
- group-create
26-
'--create[create and switch to new branch]'
27-
':new-branch:'
28-
':committish:'
29-
- group-clone
30-
'--clone[clone current branch to new branch]'
31-
':new-branch:'
32-
- group-rename
33-
'(-r --rename)'{-r,--rename}'[rename existing branch]'
34-
':branch:__stg_branch_stgit'
35-
':new-branch:'
36-
- group-protect
37-
'(-p --protect)'{-p,--protect}'[prevent stg from modifying branch]'
38-
':branch:__stg_branch_stgit'
39-
- group-unprotect
40-
'(-u --unprotect)'{-u,--unprotect}'[allow stg to modify branch]'
41-
':branch:__stg_branch_stgit'
42-
- group-delete
43-
'--delete[delete branch]'
44-
'--force[force delete when series is non-empty]'
45-
':branch:__stg_branch_stgit'
46-
- group-cleanup
47-
'--cleanup[cleanup stg metadata for branch]'
48130
'--force[force cleanup when series is non-empty]'
49-
':branch:__stg_branch_stgit'
50-
- group-description
51-
'(-d --description)'{-d,--description}'[set branch description]:description'
52-
':branch:__stg_branch_stgit'
131+
':branch:__stg_git_branch_names'
53132
)
54133
_arguments -s -S $subcmd_args
55134
}
56135

136+
_stg-branch-describe() {
137+
local -a subcmd_args
138+
__stg_add_args_help
139+
__stg_add_args_color
140+
_arguments -s -S $subcmd_args ':description:' ':branch:__stg_git_branch_names'
141+
}
142+
143+
_stg-branch-list() {
144+
local -a subcmd_args
145+
__stg_add_args_help
146+
__stg_add_args_color
147+
_arguments $subcmd_args
148+
}
149+
150+
_stg-branch-protect() {
151+
local -a subcmd_args
152+
__stg_add_args_help
153+
__stg_add_args_color
154+
_arguments $subcmd_args ':branch:__stg_stgit_branch_names'
155+
}
156+
157+
_stg-branch-rename() {
158+
local -a subcmd_args
159+
__stg_add_args_help
160+
__stg_add_args_color
161+
subcmd_args+=(
162+
':old or new branch name:__stg_git_branch_names'
163+
'::new branch name:__stg_git_branch_names'
164+
)
165+
_arguments $subcmd_args
166+
}
167+
168+
_stg-branch-unprotect() {
169+
local -a subcmd_args
170+
__stg_add_args_help
171+
__stg_add_args_color
172+
_arguments $subcmd_args ':branch:__stg_stgit_branch_names'
173+
}
174+
57175
_stg-clean() {
58176
local -a subcmd_args
59177
__stg_add_args_help
@@ -679,7 +797,7 @@ _stg-pick() {
679797
__stg_add_args_help
680798
subcmd_args+=(
681799
'(-n --name)'{-n,--name=}'[name for picked patch]:name'
682-
'(-B --ref-branch)'{-B,--ref-branch=}'[pick patches from branch]: :__stg_branch_stgit'
800+
'(-B --ref-branch)'{-B,--ref-branch=}'[pick patches from branch]: :__stg_stgit_branch_names'
683801
'(-r --revert)'{-r,--revert}'[revert given commit object]'
684802
'(-p --parent=)'{-p,--parent}'[use commit id as parent]:commit'
685803
'(-x --expose)'{-x,--expose}'[append imported commit id to patch log]'
@@ -840,7 +958,7 @@ _stg-series() {
840958
'(-d --description)'{-d,--description}'[show short descriptions]'
841959
'--no-description[do not show patch descriptions]'
842960
'(-e --empty)'{-e,--empty}'[identify empty patches]'
843-
'(-m --missing)'{-m,--missing=}'[show patches from branch missing in current]: :__stg_branch_stgit'
961+
'(-m --missing)'{-m,--missing=}'[show patches from branch missing in current]: :__stg_stgit_branch_names'
844962
'(-P --no-prefix)'{-P,--no-prefix}'[do not show the patch status prefix]'
845963
'(-s --short)'{-s,--short}'[list just patches around the topmost patch]'
846964
'--showbranch[show branch name of listed patches]'
@@ -942,7 +1060,7 @@ _stg-sync() {
9421060
'(-a --all)'{-a,--all}'[synchronize all applied patches]'
9431061
'*:patches:__stg_dedup_inside_arguments __stg_patchrange --suggest-range --use-ref-branch'
9441062
+ '(source)'
945-
'(-B --ref-branch)'{-B,--ref-branch}'[synchronize patches with branch]: :__stg_branch_stgit'
1063+
'(-B --ref-branch)'{-B,--ref-branch}'[synchronize patches with branch]: :__stg_stgit_branch_names'
9461064
'(-s --series)'{-s,--series=}'[synchronize patches with series]: :_files'
9471065
)
9481066
_arguments -s -S $subcmd_args
@@ -1012,7 +1130,7 @@ __stg_add_args_author() {
10121130

10131131
__stg_add_args_branch() {
10141132
subcmd_args+=(
1015-
'(-b --branch)'{-b,--branch=}'[specify another branch]: :__stg_branch_stgit'
1133+
'(-b --branch)'{-b,--branch=}'[specify another branch]: :__stg_stgit_branch_names'
10161134
)
10171135
}
10181136

@@ -1205,13 +1323,26 @@ __stg_git_describe_branch () {
12051323
fi
12061324
}
12071325

1208-
__stg_branch_stgit() {
1209-
declare -a stg_branches
1210-
stg_branches=(
1326+
__stg_stgit_branch_names () {
1327+
local expl
1328+
declare -a branch_names
1329+
1330+
stgit_branches=(
12111331
${${(f)"$(_call_program branchrefs git ${__stg_C_args} for-each-ref --format='"%(refname)"' refs/stacks 2>/dev/null)"}#refs/stacks/}
12121332
)
1213-
local expl
1214-
_wanted -V branches expl "branch" compadd $stg_branches
1333+
__stg_git_command_successful $pipestatus || return 1
1334+
1335+
__stg_git_describe_commit stgit_branches branch-names 'stgit branch name' "$@"
1336+
}
1337+
1338+
__stg_git_branch_names () {
1339+
local expl
1340+
declare -a branch_names
1341+
1342+
branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/})
1343+
__stg_git_command_successful $pipestatus || return 1
1344+
1345+
__stg_git_describe_commit branch_names branch-names 'branch name' "$@"
12151346
}
12161347

12171348
__stg_files_relative() {

0 commit comments

Comments
 (0)