Stack metadata version 5
[stgit.git] / completion / stgit.zsh
blobb23b0ef2c7234a2fcea1ecc973b74586e1c9f82e
1 #compdef stg
3 # This script implements zsh completions for StGit (stg).
5 # To use these completions, copy to a directory in $fpath as _stgit.
6 # For example:
8 # $ mkdir ~/.zsh.d
9 # $ cp completion/stgit.zsh ~/.zsh.d/_stgit
10 # $ $EDITOR ~/.zshrc
12 # fpath=("$HOME/.zsh.d" $fpath)
13 # autoload -U compinit
16 _stg-branch() {
17 __stg_add_args_help
18 subcmd_args+=(
19 - group-list
20 '--list[list branches]'
21 - group-switch
22 '--merge[merge worktree changes into other branch]'
23 ':branch:__stg_branch_stgit'
24 - group-create
25 '--create[create and switch to new branch]'
26 ':new-branch:'
27 ':committish:'
28 - group-clone
29 '--clone[clone current branch to new branch]'
30 ':new-branch:'
31 - group-rename
32 '(-r --rename)'{-r,--rename}'[rename existing branch]'
33 ':branch:__stg_branch_stgit'
34 ':new-branch:'
35 - group-protect
36 '(-p --protect)'{-p,--protect}'[prevent stg from modifying branch]'
37 ':branch:__stg_branch_stgit'
38 - group-unprotect
39 '(-u --unprotect)'{-u,--unprotect}'[allow stg to modify branch]'
40 ':branch:__stg_branch_stgit'
41 - group-delete
42 '--delete[delete branch]'
43 '--force[force delete when series is non-empty]'
44 ':branch:__stg_branch_stgit'
45 - group-cleanup
46 '--cleanup[cleanup stg metadata for branch]'
47 '--force[force cleanup when series is non-empty]'
48 ':branch:__stg_branch_stgit'
49 - group-description
50 '(-d --description)'{-d,--description}'[set branch description]:description'
51 ':branch:__stg_branch_stgit'
53 _arguments -s -S $subcmd_args
56 _stg-clean() {
57 __stg_add_args_help
58 subcmd_args+=(
59 '(-a --applied)'{-a,--applied}'[delete empty applied patches]'
60 '(-u --unapplied)'{-u,--unapplied}'[delete empty unapplied patches]'
62 _arguments -s -S $subcmd_args
65 _stg-clone() {
66 __stg_add_args_help
67 subcmd_args+=(
68 ':repository:'
69 ': :_directories'
71 _arguments -s $subcmd_args
74 _stg-commit() {
75 __stg_add_args_help
76 subcmd_args+=(
77 - group-all
78 '(-a --all)'{-a,--all}'[commit all unapplied patches]'
79 - group-number
80 '(-n --number)'{-n+,--number=}'[commit specified number of patches]:number'
81 - group-patches
82 '*:applied patches:__stg_patches_applied'
84 _arguments -s -S $subcmd_args
87 _stg-delete() {
88 __stg_add_args_help
89 __stg_add_args_branch
90 subcmd_args+=(
91 '--spill[spill patch contents to worktree and index]'
92 - group-top
93 '(-t --top)'{-t,--top}'[delete top patch]'
94 - group-patchnames
95 '*:patches:__stg_patches_all'
97 _arguments -s -S $subcmd_args
100 _stg-diff() {
101 __stg_add_args_help
102 __stg_add_args_diffopts
103 subcmd_args+=(
104 '(-r --range)'{-r,--range=}'[show diff between revisions]: :__stg_patches_all'
105 '(-s --stat)'{-s,--stat}'[show stat instead of diff]'
106 '*:files:__stg_changed_files'
108 _arguments -s -S $subcmd_args
111 _stg-edit() {
112 __stg_add_args_help
113 __stg_add_args_author
114 __stg_add_args_diffopts
115 __stg_add_args_edit
116 __stg_add_args_hook
117 __stg_add_args_savetemplate
118 __stg_add_args_sign
119 subcmd_args+=(
120 '(-d --diff)'{-d,--diff}'[edit patch diff]'
121 '(-t --set-tree)'{-t,--set-tree=}'[set git tree of patch]:treeish'
122 ':patch:__stg_patches_all'
124 __stg_add_args_message
125 _arguments -s -S $subcmd_args
128 _stg-export() {
129 __stg_add_args_help
130 __stg_add_args_branch
131 __stg_add_args_diffopts
132 subcmd_args+=(
133 '(-d --dir)'{-d,--dir}'[export patches to directory]: :_directories'
134 '(-n --numbered)'{-n,--numbered}'[prefix patch names with order numbers]'
135 '(-s --stdout)'{-s,--stdout}'[dump patches to standard output]'
136 '(-t --template)'{-t,--template=}'[use template file]: :_files'
137 '*:patches:__stg_patches_unhidden'
138 + '(suffix)'
139 '(-e --extension)'{-e,--extension=}'[extension to append to patch names]:extension'
140 '(-p --patch)'{-p,--patch}'[append .patch to patch names]'
142 _arguments -s -S $subcmd_args
145 _stg-files() {
146 __stg_add_args_help
147 __stg_add_args_diffopts
148 subcmd_args+=(
149 '--bare[bare file names]'
150 '(-s --stat)'{-s,--stat}'[show diff stat]'
151 ':patches:__stg_patches_all'
153 _arguments -s -S $subcmd_args
156 _stg-float() {
157 __stg_add_args_help
158 __stg_add_args_keep
159 subcmd_args+=(
160 '(-s --series)'{-s,--series=}'[arrange according to series file]: :_files'
161 '*:patches:__stg_patches_all'
163 _arguments -s -S $subcmd_args
166 _stg-fold() {
167 __stg_add_args_help
168 subcmd_args+=(
169 '(-b --base)'{-b,--base=}'[apply on base commit instead of HEAD]:commit'
170 '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num'
171 '-C=[ensure N lines of surrounding context for each change]:num'
172 '--reject[leave rejected hunks in .rej files]'
173 ':file:_files'
175 _arguments -s -S $subcmd_args
178 _stg-goto() {
179 __stg_add_args_help
180 __stg_add_args_keep
181 __stg_add_args_merged
182 subcmd_args+=(
183 ':patches:__stg_patches_all'
185 _arguments -s -S $subcmd_args
188 _stg-help() {
189 _arguments -s ':commands:__stg_subcommands'
192 _stg-hide() {
193 __stg_add_args_help
194 __stg_add_args_branch
195 subcmd_args+=(
196 ':patches:__stg_patches_unhidden'
198 _arguments -s -S $subcmd_args
201 _stg-id() {
202 __stg_add_args_help
203 subcmd_args+=(
204 ':references:__stg_patches_all'
206 _arguments -s -S $subcmd_args
209 _stg-import() {
210 __stg_add_args_help
211 __stg_add_args_author
212 __stg_add_args_edit
213 __stg_add_args_sign
214 subcmd_args+=(
215 '(-n --name)'{-n,--name}'[name for imported patch]'
216 '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num'
217 '(-t --stripname)'{-t,--stripname}'[strip number and extension from patch name]'
218 '-C=[ensure N lines of surrounding context for each change]:num'
219 '(-i --ignore)'{-i,--ignore}'[ingore applied patches in series]'
220 '--replace[replace unapplied patches in series]'
221 '--reject[leave rejected hunks in .rej files]'
222 '--keep-cr[do not remove CR from email lines ending with CRLF]'
223 '(-d --showdiff)'{-d,--showdiff}'[show patch content in editor buffer]'
224 ':file:_files'
225 + '(source)'
226 '(-m --mail)'{-m,--mail}'[import from standard email file]'
227 '(-M --mbox)'{-M,--mbox}'[import from mbox file]'
228 '(-s --series)'{-s,--series}'[import from series file]'
229 '(-u --url)'{-u,--url}'[import patch from URL]'
231 _arguments -s -S $subcmd_args
234 _stg-init() {
235 __stg_add_args_help
236 _arguments -s $subcmd_args
239 _stg-log() {
240 __stg_add_args_help
241 __stg_add_args_branch
242 subcmd_args+=(
243 '--clear[clear log history]'
244 '(-d --diff)'{-d,--diff}'[show refresh diffs]'
245 '(-f --full)'{-f,--full}'[show full commit ids]'
246 '(-g --graphical)'{-g,--graphical}'[show log in gitk]'
247 '(-n --number)'{-n+,--number=}'[limit to number of commits]'
248 '*:patches:__stg_patches_all'
250 _arguments -s -S $subcmd_args
253 _stg-mail() {
254 __stg_add_args_help
255 __stg_add_args_branch
256 __stg_add_args_diffopts
257 subcmd_args+=(
258 '--to=[add address to To: list]:address'
259 '--cc=[add address to Cc: list]:address'
260 '--bcc=[add address to Bcc: list]:address'
261 '--auto[automatically cc patch signers]'
262 '--no-thread[do not send subsequent messages as replies]'
263 '--unrelated[send patches without sequence numbers]'
264 '(-v --version)'{-v,--version=}'[add version to subject prefix]:version'
265 '--prefix=[add prefix to subject]:prefix'
266 '(-c --cover)'{-c,--cover=}'[cover message file]: :_files'
267 '(-e --edit-cover)'{-e,--edit-cover}'[edit cover message before sending]'
268 '(-E --edit-patches)'{-E,--edit-patches}'[edit patches before sending]'
269 '(-s --sleep)'{-s,--sleep=}'[seconds to sleep between sending emails]:seconds'
270 '--in-reply-to=[reply reference id]:refid'
271 '--domain=[domain to use for message ID]:domain'
272 '(-u --user)'{-u,--user=}'[username for SMTP authentication]:user'
273 '(-p --password)'{-p,--password=}'[password for SMTP authentication]:password'
274 '(-T --smtp-tls)'{-t,--smtp-tls}'[use TLS for SMTP authentication]'
275 + '(send-method)'
276 '--git[use `git send-email`]'
277 '(-m --mbox)'{-m,--mbox}'[generate mbox file instead of sending]'
278 '--smtp-server=[server or command for sending email]'
279 + '(attachment)'
280 '--attach[send patch as attachment]'
281 '--attach-inline[send patch as inline attachment]'
282 '(-t --template)'{-t,--template=}'[message template file]: :_files'
283 + '(patches)'
284 '(-a --all)'{-a,--all}'[email all applied patches]'
285 '*:patches:__stg_patches_all'
287 _arguments -s -S $subcmd_args
290 _stg-new() {
291 __stg_add_args_help
292 __stg_add_args_author
293 __stg_add_args_sign
294 __stg_add_args_hook
295 __stg_add_args_savetemplate
296 subcmd_args+=(
297 '(-v --verbose)'{-v,--verbose}'[show diff of file changes]'
298 ':: :_guard "([^-]?#|)" name'
300 __stg_add_args_message
301 _arguments -s -S $subcmd_args
304 _stg-next() {
305 __stg_add_args_help
306 __stg_add_args_branch
307 _arguments -s -S $subcmd_args
310 _stg-patches() {
311 __stg_add_args_help
312 __stg_add_args_branch
313 __stg_add_args_diffopts
314 subcmd_args+=(
315 '(-d --diff)'{-d,--diff}'[show diffs of given files]'
316 '*:files:__stg_files_known'
318 _arguments -s -S $subcmd_args
321 _stg-pick() {
322 # TODO: complete --parent commit id
323 __stg_add_args_help
324 subcmd_args+=(
325 '(-n --name)'{-n,--name=}'[name for picked patch]:name'
326 '(-B --ref-branch)'{-B,--ref-branch=}'[pick patches from branch]: :__stg_branch_stgit'
327 '(-r --revert)'{-r,--revert}'[revert given commit object]'
328 '(-p --parent=)'{-p,--parent}'[use commit id as parent]:commit'
329 '(-x --expose)'{-x,--expose}'[append imported commit id to patch log]'
330 '--unapplied[keep patch unapplied]'
331 '*'{-f,--file=}'[only fold given file]: :_files'
332 '*:patches:__stg_patches_refbranch'
333 + '(mode)'
334 '--fold[fold the commit into current patch]'
335 '--update[fold limited to current patch files]'
337 _arguments -s -S $subcmd_args
340 _stg-pop() {
341 __stg_add_args_help
342 __stg_add_args_keep
343 subcmd_args+=(
344 '(-s --spill)'{-s,--spill}'[pop a patch keeping its modifications in the tree]'
345 - group-number
346 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
347 - group-all
348 '(-a --all)'{-a,--all}'[push all unapplied patches]'
349 - group-patches
350 '*:applied patches:__stg_patches_applied'
352 _arguments -s -S $subcmd_args
355 _stg-prev() {
356 __stg_add_args_help
357 __stg_add_args_branch
358 _arguments -s -S $subcmd_args
361 _stg-pull() {
362 __stg_add_args_help
363 __stg_add_args_merged
364 subcmd_args+=(
365 '(-n --nopush)'{-n,--nopush}'[do not push patches after rebasing]'
366 ':repository:__stg_remotes'
368 _arguments -s -S $subcmd_args
371 _stg-push() {
372 __stg_add_args_help
373 __stg_add_args_keep
374 __stg_add_args_merged
375 subcmd_args+=(
376 '--reverse[push patches in reverse order]'
377 '--set-tree[push patch with the original tree]'
378 - group-all
379 '(-a --all)'{-a,--all}'[push all unapplied patches]'
380 - group-number
381 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
382 - group-patches
383 '*:unapplied patches:__stg_patches_unapplied'
385 _arguments -s -S $subcmd_args
388 _stg-rebase() {
389 __stg_add_args_help
390 __stg_add_args_merged
391 subcmd_args+=(
392 '(-n --nopush)'{-n,--nopush}'[do not push patches after rebasing]'
393 ':new-base-id:__stg_heads'
395 _arguments -s -S $subcmd_args
398 _stg-redo() {
399 __stg_add_args_help
400 subcmd_args+=(
401 '--hard[discard changes in index/worktree]'
402 '(-n --number)'{-n+,--number=}'[number of undos to redo]:number'
404 _arguments -s -S $subcmd_args
407 _stg-refresh() {
408 __stg_add_args_help
409 __stg_add_args_author
410 __stg_add_args_edit
411 __stg_add_args_hook
412 __stg_add_args_sign
413 __stg_add_args_diffopts
414 subcmd_args+=(
415 '(-a --annotate)'{-a,--annotate=}'[annotate patch log entry]:note'
416 '(-d --diff)'{-d,--diff}'[show diff when editing patch message]'
417 '(-F --force)'{-F,--force}'[force refresh even if index is dirty]'
418 '(-i --index)'{-i,--index}'[refresh from index instead of worktree]'
419 '(-p --patch)'{-p,--patch=}'[refresh patch other than top patch]: :__stg_patches_applied'
420 '--spill[Spill patch contents to worktree and index, and erase patch content]'
421 + '(update-files)'
422 '(-u --update)'{-u,--update}'[only update current patch files]'
423 '*:files:__stg_changed_files'
424 + '(submodules)'
425 '(-s --submodules)'{-s,--submodules}'[include submodules in refresh]'
426 '--no-submodules[exclude submodules from refresh]'
428 __stg_add_args_message
429 _arguments -s -S $subcmd_args
432 _stg-rename() {
433 __stg_add_args_help
434 __stg_add_args_branch
435 subcmd_args+=(
436 ':old-patch:__stg_patches_all'
438 _arguments -s -S $subcmd_args
441 _stg-repair() {
442 __stg_add_args_help
443 _arguments -s $subcmd_args
446 _stg-reset() {
447 __stg_add_args_help
448 subcmd_args+=(
449 '--hard[discard changes in index/worktree]'
450 ':state:'
451 '*:patches:__stg_patches_all'
453 _arguments -s -S $subcmd_args
456 _stg-series() {
457 __stg_add_args_help
458 __stg_add_args_branch
459 subcmd_args+=(
460 '--author[show the author name for each patch]'
461 '(-c --count)'{-c,--count}'[print number of patches]'
462 '(-d --description)'{-d,--description}'[show short descriptions]'
463 '--no-description[do not show patch descriptions]'
464 '(-e --empty)'{-e,--empty}'[identify empty patches]'
465 '(-m --missing)'{-m,--missing=}'[show patches from branch missing in current]: :__stg_branch_stgit'
466 '--noprefix[do not show the patch status prefix]'
467 '(-s --short)'{-s,--short}'[list just patches around the topmost patch]'
468 '--showbranch[show branch name of listed patches]'
469 - group-ahu
470 '(-A --applied)'{-A,--applied}'[show applied patches]'
471 '(-H --hidden)'{-H,--hidden}'[show hidden patches]'
472 '(-U --unapplied)'{-U,--unapplied}'[show unapplied patches]'
473 - group-all
474 '(-a --all)'{-a,--all}'[show all patches including hidden]'
475 - group-patches
476 ':patches:__stg_patches_all'
478 _arguments -s -S $subcmd_args
481 _stg-show() {
482 __stg_add_args_help
483 __stg_add_args_branch
484 __stg_add_args_diffopts
485 subcmd_args+=(
486 '(-s --stat)'{-s,--stat}'[show diff stat]'
487 '*:patches:__stg_patches_all'
488 + '(patch-selection)'
489 '(-a --applied)'{-a,--applied}'[show applied patches]'
490 '(-u --unapplied)'{-u,--unapplied}'[show unapplied patches]'
492 _arguments -s -S $subcmd_args
495 _stg-sink() {
496 __stg_add_args_help
497 __stg_add_args_keep
498 subcmd_args+=(
499 '(-n --nopush)'{-n,--nopush}'[do not push patches after sinking]'
500 '(-t --to)'{-t,--to=}'[sink patches below target patch]: :__stg_patches_applied'
501 ':patches:__stg_patches_all'
503 _arguments -s -S $subcmd_args
506 _stg-squash() {
507 __stg_add_args_help
508 __stg_add_args_hook
509 __stg_add_args_savetemplate
510 subcmd_args+=(
511 '(-n --name)'{-n,--name=}'[name for squashed patch]: :__stg_patches_all'
512 '*:patches:__stg_patches_all_allow_dups'
514 __stg_add_args_message
515 _arguments -s -S $subcmd_args
518 _stg-sync() {
519 __stg_add_args_help
520 subcmd_args+=(
521 + '(patches)'
522 '(-a --all)'{-a,--all}'[synchronize all applied patches]'
523 '*:patches:__stg_patches_refbranch'
524 + '(source)'
525 '(-B --ref-branch)'{-B,--ref-branch}'[synchronize patches with branch]: :__stg_branch_stgit'
526 '(-s --series)'{-s,--series=}'[synchronize patches with series]: :_files'
528 _arguments -s -S $subcmd_args
531 _stg-top() {
532 __stg_add_args_help
533 __stg_add_args_branch
534 _arguments -s -S $subcmd_args
537 _stg-uncommit() {
538 __stg_add_args_help
539 subcmd_args+=(
540 - group-number
541 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
542 ':prefix:'
543 - group-to
544 '(-t --to)'{-t,--to=}'[uncommit to the specified commit]:commit'
545 '(-x --exclusive)'{-x,--exclusive}'[exclude the commit specified by --to]'
546 - group-names
547 '*: :_guard "([^-]?#|)" names'
549 _arguments -s -S $subcmd_args
552 _stg-undo() {
553 __stg_add_args_help
554 subcmd_args+=(
555 '--hard[discard changes in index/worktree]'
556 '(-n --number)'{-n+,--number=}'[number commands to undo]:number'
558 _arguments -s -S $subcmd_args
561 _stg-unhide() {
562 __stg_add_args_help
563 __stg_add_args_branch
564 subcmd_args+=(
565 ':patches:__stg_patches_hidden'
567 _arguments -s -S $subcmd_args
570 __stg_add_args_author() {
571 subcmd_args+=(
572 '--author=[set author details]'
573 '--authdate=[set author date]:date'
574 '--authemail=[set author email]:email'
575 '--authname=[set author name]:name'
579 __stg_add_args_branch() {
580 subcmd_args+=(
581 '(-b --branch)'{-b,--branch=}'[specify another branch]: :__stg_branch_stgit'
585 __stg_add_args_diffopts() {
586 # TODO: complete diff-opts values (with separators?)
587 subcmd_args+=(
588 '(-O --diff-opts)'{-O+,--diff-opts=}'[extra options for git diff]:opts'
592 __stg_add_args_edit() {
593 subcmd_args+=(
594 '(-e --edit)'{-e,--edit}'[invoke interactive editor]'
598 __stg_add_args_help() {
599 subcmd_args+=(
600 '(- *)'{-h,--help}'[show help message and exit]'
604 __stg_add_args_hook() {
605 subcmd_args+=(
606 '--no-verify[disable commit-msg hook]'
610 __stg_add_args_keep() {
611 subcmd_args+=(
612 '(-k --keep)'{-k,--keep}'[keep local changes]'
616 __stg_add_args_merged() {
617 subcmd_args+=(
618 '(-m --merged)'{-m,--merged}'[check for patches merged upstream]'
622 __stg_add_args_message() {
623 subcmd_args+=(
624 + '(message)'
625 '(-f --file)'{-f,--file=}'[use message file instead of invoking editor]: :_files'
626 '(-m --message)'{-m,--message=}'[specify message instead of invoking editor]:message'
630 __stg_add_args_savetemplate() {
631 subcmd_args+=(
632 '--save-template=[save message template to file and exit]: :_files'
636 __stg_add_args_sign() {
637 subcmd_args+=(
638 '--ack[add Acked-by trailer]'
639 '--ack-by[add Acked-by trailer]:value'
640 '--review[add Reviewed-by trailer]'
641 '--review-by[add Reviewed-by trailer]:value'
642 '--sign[add Signed-off-by trailer]'
643 '--sign-by[add Signed-off-by trailer]:value'
647 __stg_heads () {
648 _alternative 'heads-local::__stg_heads_local' 'heads-remote::__stg_heads_remote'
651 __stg_heads_local () {
652 local f gitdir
653 declare -a heads
655 heads=(${(f)"$(_call_program headrefs git for-each-ref --format='"%(refname:short)"' refs/heads 2>/dev/null)"})
656 gitdir=$(_call_program gitdir git rev-parse --git-dir 2>/dev/null)
657 if __stg_git_command_successful $pipestatus; then
658 for f in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
659 [[ -f $gitdir/$f ]] && heads+=$f
660 done
661 [[ -f $gitdir/refs/stash ]] && heads+=stash
662 [[ -f $gitdir/refs/bisect/bad ]] && heads+=bisect/bad
665 __stg_git_describe_commit heads heads-local "local head" "$@"
668 __stg_heads_remote () {
669 declare -a heads
671 heads=(${(f)"$(_call_program headrefs git for-each-ref --format='"%(refname:short)"' refs/remotes 2>/dev/null)"})
673 __stg_git_describe_commit heads heads-remote "remote head" "$@"
676 __stg_git_command_successful () {
677 if (( ${#*:#0} > 0 )); then
678 _message 'not a git repository'
679 return 1
681 return 0
684 __stg_git_describe_commit () {
685 __stg_git_describe_branch $1 $2 $3 -M 'r:|/=* r:|=*' "${(@)argv[4,-1]}"
688 __stg_git_describe_branch () {
689 local __commits_in=$1
690 local __tag=$2
691 local __desc=$3
692 shift 3
694 integer maxverbose
695 if zstyle -s :completion:$curcontext: max-verbose maxverbose &&
696 (( ${compstate[nmatches]} <= maxverbose )); then
697 local __c
698 local -a __commits
699 for __c in ${(P)__commits_in}; do
700 __commits+=("${__c}:${$(_call_program describe git rev-list -1 --oneline $__c)//:/\\:}")
701 done
702 _describe -t $__tag $__desc __commits "$@"
703 else
704 local expl
705 _wanted $__tag expl $__desc compadd "$@" -a - $__commits_in
709 __stg_branch_stgit() {
710 declare -a stg_branches
711 stg_branches=(
712 ${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/stacks 2>/dev/null)"}#refs/stacks/}
714 local expl
715 _wanted -V branches expl "branch" compadd $stg_branches
718 __stg_files_relative() {
719 local prefix
720 prefix=$(_call_program gitprefix git rev-parse --show-prefix 2>/dev/null)
721 if (( $#prefix == 0 )); then
722 print $1
723 return
726 local file
727 local -a files
729 files=()
730 # Collapse "//" and "/./" into "/". Strip any remaining "/." and "/".
731 for file in ${${${${${(0)1}//\/\///}//\/.\///}%/.}%/}; do
732 integer i n
733 (( n = $#file > $#prefix ? $#file : $#prefix ))
734 for (( i = 1; i <= n; i++ )); do
735 if [[ $file[i] != $prefix[i] ]]; then
736 while (( i > 0 )) && [[ $file[i-1] != / ]]; do
737 (( i-- ))
738 done
739 break
741 done
743 files+=${(l@${#prefix[i,-1]//[^\/]}*3@@../@)}${file[i,-1]}
744 done
746 print ${(pj:\0:)files}
749 __stg_diff-index_files () {
750 local tree=$1 description=$2 tag=$3; shift 3
751 local files expl
753 # $tree needs to be escaped for _call_program; matters for $tree = "HEAD^"
754 files=$(_call_program files git diff-index -z --name-only --no-color --cached ${(q)tree} 2>/dev/null)
755 __stg_git_command_successful $pipestatus || return 1
756 files=(${(0)"$(__stg_files_relative $files)"})
757 __stg_git_command_successful $pipestatus || return 1
759 _wanted $tag expl $description _multi_parts $@ - / files
762 __stg_changed-in-index_files () {
763 __stg_diff-index_files HEAD 'changed in index file' changed-in-index-files "$@"
766 __stg_changed-in-working-tree_files () {
767 local files expl
769 files=$(_call_program changed-in-working-tree-files git diff -z --name-only --no-color 2>/dev/null)
770 __stg_git_command_successful $pipestatus || return 1
771 files=(${(0)"$(__stg_files_relative $files)"})
772 __stg_git_command_successful $pipestatus || return 1
774 _wanted changed-in-working-tree-files expl 'changed in working tree file' _multi_parts $@ -f - / files
777 __stg_changed_files () {
778 _alternative \
779 'changed-in-index-files::__stg_changed-in-index_files' \
780 'changed-in-working-tree-files::__stg_changed-in-working-tree_files'
783 __stg_files_known() {
784 declare -a known_files
785 known_files=(
786 ${(f)"$(_call_program known-files git ls-files 2>/dev/null)"}
788 local expl
789 _wanted -V known-files expl "known files" _multi_parts - / known_files
792 __stg_get_branch_opt() {
793 local short long i
794 short=${1:-'-b'}
795 long=${2:-'--branch'}
796 i=${words[(I)$short|$long(=*|)]}
797 case ${words[i]} in
798 $short|$long)
799 if (( i < $#words )); then
800 echo "--branch=${words[i + 1]}"
804 echo "--branch=${words[i]#*=}"
806 esac
809 __stg_want_patches() {
810 declare -a patches
811 local expl
812 patches=(
813 ${(f)"$(_call_program want-patches stg series --no-description --noprefix $@ 2>/dev/null)"}
815 _wanted -V all expl "patch" compadd ${patches:|words}
819 __stg_patches_all() {
820 __stg_want_patches $(__stg_get_branch_opt) --all
823 __stg_patches_all_allow_dups() {
824 declare -a patches
825 local expl branch_opt
826 branch_opt=$(__stg_get_branch_opt)
827 patches=(
828 ${(f)"$(_call_program all-patches stg series $branch_opt --no-description --noprefix --all 2>/dev/null)"}
830 _wanted -V all expl "patch" compadd ${patches}
833 __stg_patches_applied() {
834 __stg_want_patches $(__stg_get_branch_opt) --applied
837 __stg_patches_hidden() {
838 __stg_want_patches $(__stg_get_branch_opt) --hidden
841 __stg_patches_unapplied() {
842 __stg_want_patches $(__stg_get_branch_opt) --unapplied
845 __stg_patches_unhidden() {
846 __stg_want_patches $(__stg_get_branch_opt) --applied --unapplied
849 __stg_patches_refbranch() {
850 __stg_want_patches $(__stg_get_branch_opt -B --ref-branch) --all
853 __stg_remotes() {
854 local remotes expl
855 remotes=(${(f)"$(_call_program remotes git remote 2>/dev/null)"})
856 _wanted remotes expl remote compadd "$@" -a - remotes
859 __stg_subcommands() {
860 _describe -t commands 'stgit command' _stg_cmds
863 __stg_caching_policy() {
864 [[ =$service -nt $1 ]]
867 _stgit() {
868 local curcontext="$curcontext" state line expl ret=1
869 typeset -A opt_args
871 # Special cases for git aliases
872 case "$words[2]" in
873 (add|mv|rm|status)
874 words[1]=git
876 (resolved)
877 words[1]=git
878 words[2]=add
880 esac
881 if [ "$words[1]" = git ]; then
882 _normal && ret=0
883 return ret
886 local update_policy
887 zstyle -s ":completion:*:*:stg:*" cache-policy update_policy
888 if [[ -z "$update_policy" ]]; then
889 zstyle ":completion:*:*:stg:*" cache-policy __stg_caching_policy
892 _arguments -C -A "-*" \
893 '(-)--help[print help information]' \
894 '(*)--version[display version information]' \
895 '1: :->command' \
896 '*:: :->args' && ret=0
898 if [[ -n $state ]] && (( ! $+_stg_cmds )); then
899 typeset -a _stg_cmds
900 if _cache_invalid stg-cmds || ! _retrieve_cache stg-cmds; then
901 _stg_cmds=(
902 ${${${(M)${(f)"$(stg help 2> /dev/null)"}## *}# }/#(#b)([^[:space:]]##)[[:space:]]##(*)/$match[1]:$match[2]}
904 if (( $? == 0 )); then
905 _store_cache stg-cmds _stg_cmds
906 else
907 unset _stg_cmds
912 case $state in
913 (command)
914 __stg_subcommands && ret=0
916 (args)
917 local -a subcmd_args
918 local subcmd=$words[1]
919 curcontext="${curcontext%:*:*}:stg-$subcmd:"
920 if ! _call_function ret _stg-$subcmd; then
921 _message "unknown sub-command: $subcmd"
924 esac
926 return ret
929 _stgit "$@"