3 # This script implements zsh completions for StGit (stg).
5 # To use these completions, copy to a directory in $fpath as _stgit.
9 # $ cp completion/stgit.zsh ~/.zsh.d/_stgit
12 # fpath=("$HOME/.zsh.d" $fpath)
13 # autoload -U compinit
20 '--list[list branches]'
22 '--merge[merge worktree changes into other branch]'
23 ':branch:__stg_branch_stgit'
25 '--create[create and switch to new branch]'
29 '--clone[clone current branch to new branch]'
32 '(-r --rename)'{-r,--rename}'[rename existing branch]'
33 ':branch:__stg_branch_stgit'
36 '(-p --protect)'{-p,--protect}'[prevent stg from modifying branch]'
37 ':branch:__stg_branch_stgit'
39 '(-u --unprotect)'{-u,--unprotect}'[allow stg to modify branch]'
40 ':branch:__stg_branch_stgit'
42 '--delete[delete branch]'
43 '--force[force delete when series is non-empty]'
44 ':branch:__stg_branch_stgit'
46 '--cleanup[cleanup stg metadata for branch]'
47 '--force[force cleanup when series is non-empty]'
48 ':branch:__stg_branch_stgit'
50 '(-d --description)'{-d,--description}'[set branch description]:description'
51 ':branch:__stg_branch_stgit'
53 _arguments
-s -S $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
71 _arguments
-s $subcmd_args
77 '--allow-empty[allow committing empty patches]'
79 '(-a --all)'{-a,--all}'[commit all unapplied patches]'
81 '(-n --number)'{-n+,--number=}'[commit specified number of patches]:number'
83 '*:applied patches:__stg_patches_applied'
85 _arguments
-s -S $subcmd_args
92 '--spill[spill patch contents to worktree and index]'
94 '(-t --top)'{-t,--top}'[delete top patch]'
96 '*:patches:__stg_patches_all'
98 _arguments
-s -S $subcmd_args
103 __stg_add_args_diffopts
105 '(-r --range)'{-r,--range=}'[show diff between revisions]: :__stg_patches_all'
106 '(-s --stat)'{-s,--stat}'[show stat instead of diff]'
107 '*:files:__stg_changed_files'
109 _arguments
-s -S $subcmd_args
114 __stg_add_args_author
115 __stg_add_args_diffopts
118 __stg_add_args_savetemplate
121 '(-d --diff)'{-d,--diff}'[edit patch diff]'
122 '(-t --set-tree)'{-t,--set-tree=}'[set git tree of patch]:treeish'
123 ':patch:__stg_patches_all'
125 __stg_add_args_message
126 _arguments
-s -S $subcmd_args
131 __stg_add_args_branch
132 __stg_add_args_diffopts
134 '(-d --dir)'{-d,--dir}'[export patches to directory]: :_directories'
135 '(-n --numbered)'{-n,--numbered}'[prefix patch names with order numbers]'
136 '(-s --stdout)'{-s,--stdout}'[dump patches to standard output]'
137 '(-t --template)'{-t,--template=}'[use template file]: :_files'
138 '*:patches:__stg_patches_unhidden'
140 '(-e --extension)'{-e,--extension=}'[extension to append to patch names]:extension'
141 '(-p --patch)'{-p,--patch}'[append .patch to patch names]'
143 _arguments
-s -S $subcmd_args
148 __stg_add_args_diffopts
150 '--bare[bare file names]'
151 '(-s --stat)'{-s,--stat}'[show diff stat]'
152 ':patches:__stg_patches_all'
154 _arguments
-s -S $subcmd_args
161 '--noapply[Reorder patches by floating without applying]'
162 '(-s --series)'{-s,--series=}'[arrange according to series file]: :_files'
163 '*:patches:__stg_patches_all'
165 _arguments
-s -S $subcmd_args
171 '(-b --base)'{-b,--base=}'[apply on base commit instead of HEAD]:commit'
172 '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num'
173 '-C=[ensure N lines of surrounding context for each change]:num'
174 '--reject[leave rejected hunks in .rej files]'
177 _arguments
-s -S $subcmd_args
183 __stg_add_args_merged
185 ':patches:__stg_patches_all'
187 _arguments
-s -S $subcmd_args
191 _arguments
-s ':commands:__stg_subcommands'
196 __stg_add_args_branch
198 ':patches:__stg_patches_unhidden'
200 _arguments
-s -S $subcmd_args
206 ':references:__stg_patches_all'
208 _arguments
-s -S $subcmd_args
213 __stg_add_args_author
217 '(-n --name)'{-n,--name}'[name for imported patch]'
218 '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num'
219 '(-t --stripname)'{-t,--stripname}'[strip number and extension from patch name]'
220 '-C=[ensure N lines of surrounding context for each change]:num'
221 '(-i --ignore)'{-i,--ignore}'[ingore applied patches in series]'
222 '--replace[replace unapplied patches in series]'
223 '--reject[leave rejected hunks in .rej files]'
224 '--keep-cr[do not remove CR from email lines ending with CRLF]'
225 '--message-id[create Message-Id trailer from email header]'
226 '(-d --showdiff)'{-d,--showdiff}'[show patch content in editor buffer]'
229 '(-m --mail)'{-m,--mail}'[import from standard email file]'
230 '(-M --mbox)'{-M,--mbox}'[import from mbox file]'
231 '(-s --series)'{-s,--series}'[import from series file]'
232 '(-u --url)'{-u,--url}'[import patch from URL]'
234 _arguments
-s -S $subcmd_args
239 _arguments
-s $subcmd_args
244 __stg_add_args_branch
246 '--clear[clear log history]'
247 '(-d --diff)'{-d,--diff}'[show refresh diffs]'
248 '(-f --full)'{-f,--full}'[show full commit ids]'
249 '(-g --graphical)'{-g,--graphical}'[show log in gitk]'
250 '(-n --number)'{-n+,--number=}'[limit to number of commits]'
251 '*:patches:__stg_patches_all'
253 _arguments
-s -S $subcmd_args
258 __stg_add_args_branch
259 __stg_add_args_diffopts
261 '--to=[add address to To: list]:address'
262 '--cc=[add address to Cc: list]:address'
263 '--bcc=[add address to Bcc: list]:address'
264 '--auto[automatically cc patch signers]'
265 '--no-thread[do not send subsequent messages as replies]'
266 '--unrelated[send patches without sequence numbers]'
267 '(-v --version)'{-v,--version=}'[add version to subject prefix]:version'
268 '--prefix=[add prefix to subject]:prefix'
269 '(-c --cover)'{-c,--cover=}'[cover message file]: :_files'
270 '(-e --edit-cover)'{-e,--edit-cover}'[edit cover message before sending]'
271 '(-E --edit-patches)'{-E,--edit-patches}'[edit patches before sending]'
272 '(-s --sleep)'{-s,--sleep=}'[seconds to sleep between sending emails]:seconds'
273 '--in-reply-to=[reply reference id]:refid'
274 '--domain=[domain to use for message ID]:domain'
275 '(-u --user)'{-u,--user=}'[username for SMTP authentication]:user'
276 '(-p --password)'{-p,--password=}'[password for SMTP authentication]:password'
277 '(-T --smtp-tls)'{-t,--smtp-tls}'[use TLS for SMTP authentication]'
279 '--git[use `git send-email`]'
280 '(-m --mbox)'{-m,--mbox}'[generate mbox file instead of sending]'
281 '--smtp-server=[server or command for sending email]'
283 '--attach[send patch as attachment]'
284 '--attach-inline[send patch as inline attachment]'
285 '(-t --template)'{-t,--template=}'[message template file]: :_files'
287 '(-a --all)'{-a,--all}'[email all applied patches]'
288 '*:patches:__stg_patches_all'
290 _arguments
-s -S $subcmd_args
295 __stg_add_args_author
298 __stg_add_args_savetemplate
300 '(-v --verbose)'{-v,--verbose}'[show diff of file changes]'
301 ':: :_guard "([^-]?#|)" name'
303 __stg_add_args_message
304 _arguments
-s -S $subcmd_args
309 __stg_add_args_branch
310 _arguments
-s -S $subcmd_args
315 __stg_add_args_branch
316 __stg_add_args_diffopts
318 '(-d --diff)'{-d,--diff}'[show diffs of given files]'
319 '*:files:__stg_files_known'
321 _arguments
-s -S $subcmd_args
325 # TODO: complete --parent commit id
328 '(-n --name)'{-n,--name=}'[name for picked patch]:name'
329 '(-B --ref-branch)'{-B,--ref-branch=}'[pick patches from branch]: :__stg_branch_stgit'
330 '(-r --revert)'{-r,--revert}'[revert given commit object]'
331 '(-p --parent=)'{-p,--parent}'[use commit id as parent]:commit'
332 '(-x --expose)'{-x,--expose}'[append imported commit id to patch log]'
333 '--noapply[keep patch unapplied]'
334 '*'{-f,--file=}'[only fold given file]: :_files'
335 '*:patches:__stg_patches_refbranch'
337 '--fold[fold the commit into current patch]'
338 '--update[fold limited to current patch files]'
340 _arguments
-s -S $subcmd_args
347 '(-s --spill)'{-s,--spill}'[pop a patch keeping its modifications in the tree]'
349 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
351 '(-a --all)'{-a,--all}'[push all unapplied patches]'
353 '*:applied patches:__stg_patches_applied'
355 _arguments
-s -S $subcmd_args
360 __stg_add_args_branch
361 _arguments
-s -S $subcmd_args
366 __stg_add_args_merged
368 '(-n --nopush)'{-n,--nopush}'[do not push patches after rebasing]'
369 ':repository:__stg_remotes'
371 _arguments
-s -S $subcmd_args
377 __stg_add_args_merged
379 '--reverse[push patches in reverse order]'
380 '--noapply[push without applying]'
381 '--set-tree[push patch with the original tree]'
383 '(-a --all)'{-a,--all}'[push all unapplied patches]'
385 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
387 '*:unapplied patches:__stg_patches_unapplied'
389 _arguments
-s -S $subcmd_args
394 __stg_add_args_merged
396 '(-n --nopush)'{-n,--nopush}'[do not push patches after rebasing]'
397 ':new-base-id:__stg_heads'
399 _arguments
-s -S $subcmd_args
405 '--hard[discard changes in index/worktree]'
406 '(-n --number)'{-n+,--number=}'[number of undos to redo]:number'
408 _arguments
-s -S $subcmd_args
413 __stg_add_args_author
417 __stg_add_args_diffopts
419 '(-a --annotate)'{-a,--annotate=}'[annotate patch log entry]:note'
420 '(-d --diff)'{-d,--diff}'[show diff when editing patch message]'
421 '(-F --force)'{-F,--force}'[force refresh even if index is dirty]'
422 '(-i --index)'{-i,--index}'[refresh from index instead of worktree]'
423 '(-p --patch)'{-p,--patch=}'[refresh patch other than top patch]: :__stg_patches_all'
424 '--spill[Spill patch contents to worktree and index, and erase patch content]'
426 '(-u --update)'{-u,--update}'[only update current patch files]'
427 '*:files:__stg_changed_files'
429 '(-s --submodules)'{-s,--submodules}'[include submodules in refresh]'
430 '--no-submodules[exclude submodules from refresh]'
432 __stg_add_args_message
433 _arguments
-s -S $subcmd_args
438 __stg_add_args_branch
440 ':old-patch:__stg_patches_all'
442 _arguments
-s -S $subcmd_args
447 _arguments
-s $subcmd_args
453 '--hard[discard changes in index/worktree]'
455 '*:patches:__stg_patches_all'
457 _arguments
-s -S $subcmd_args
462 __stg_add_args_branch
464 '--author[show the author name for each patch]'
465 '(-c --count)'{-c,--count}'[print number of patches]'
466 '(-d --description)'{-d,--description}'[show short descriptions]'
467 '--no-description[do not show patch descriptions]'
468 '(-e --empty)'{-e,--empty}'[identify empty patches]'
469 '(-m --missing)'{-m,--missing=}'[show patches from branch missing in current]: :__stg_branch_stgit'
470 '--noprefix[do not show the patch status prefix]'
471 '(-s --short)'{-s,--short}'[list just patches around the topmost patch]'
472 '--showbranch[show branch name of listed patches]'
474 '(-A --applied)'{-A,--applied}'[show applied patches]'
475 '(-H --hidden)'{-H,--hidden}'[show hidden patches]'
476 '(-U --unapplied)'{-U,--unapplied}'[show unapplied patches]'
478 '(-a --all)'{-a,--all}'[show all patches including hidden]'
480 ':patches:__stg_patches_all'
482 _arguments
-s -S $subcmd_args
487 __stg_add_args_branch
488 __stg_add_args_diffopts
490 '(-s --stat)'{-s,--stat}'[show diff stat]'
491 '*:patches:__stg_patches_all'
492 + '(patch-selection)'
493 '(-a --applied)'{-a,--applied}'[show applied patches]'
494 '(-u --unapplied)'{-u,--unapplied}'[show unapplied patches]'
496 _arguments
-s -S $subcmd_args
503 '(-n --nopush)'{-n,--nopush}'[do not push patches after sinking]'
504 '(-t --to)'{-t,--to=}'[sink patches below target patch]: :__stg_patches_applied'
505 '*:patches:__stg_patches_all'
507 _arguments
-s -S $subcmd_args
513 __stg_add_args_savetemplate
515 '(-n --name)'{-n,--name=}'[name for squashed patch]: :__stg_patches_all'
516 '*:patches:__stg_patches_all_allow_dups'
518 __stg_add_args_message
519 _arguments
-s -S $subcmd_args
526 '(-a --all)'{-a,--all}'[synchronize all applied patches]'
527 '*:patches:__stg_patches_refbranch'
529 '(-B --ref-branch)'{-B,--ref-branch}'[synchronize patches with branch]: :__stg_branch_stgit'
530 '(-s --series)'{-s,--series=}'[synchronize patches with series]: :_files'
532 _arguments
-s -S $subcmd_args
537 __stg_add_args_branch
538 _arguments
-s -S $subcmd_args
545 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
548 '(-t --to)'{-t,--to=}'[uncommit to the specified commit]:commit'
549 '(-x --exclusive)'{-x,--exclusive}'[exclude the commit specified by --to]'
551 '*: :_guard "([^-]?#|)" names'
553 _arguments
-s -S $subcmd_args
559 '--hard[discard changes in index/worktree]'
560 '(-n --number)'{-n+,--number=}'[number commands to undo]:number'
562 _arguments
-s -S $subcmd_args
567 __stg_add_args_branch
569 ':patches:__stg_patches_hidden'
571 _arguments
-s -S $subcmd_args
574 __stg_add_args_author
() {
576 '--author=[set author details]'
577 '--authdate=[set author date]:date'
578 '--authemail=[set author email]:email'
579 '--authname=[set author name]:name'
583 __stg_add_args_branch
() {
585 '(-b --branch)'{-b,--branch=}'[specify another branch]: :__stg_branch_stgit'
589 __stg_add_args_diffopts
() {
590 # TODO: complete diff-opts values (with separators?)
592 '(-O --diff-opts)'{-O+,--diff-opts=}'[extra options for git diff]:opts'
596 __stg_add_args_edit
() {
598 '(-e --edit)'{-e,--edit}'[invoke interactive editor]'
602 __stg_add_args_help
() {
604 '(- *)'{-h,--help}'[show help message and exit]'
608 __stg_add_args_hook
() {
610 '--no-verify[disable commit-msg hook]'
614 __stg_add_args_keep
() {
616 '(-k --keep)'{-k,--keep}'[keep local changes]'
620 __stg_add_args_merged
() {
622 '(-m --merged)'{-m,--merged}'[check for patches merged upstream]'
626 __stg_add_args_message
() {
629 '(-f --file)'{-f,--file=}'[use message file instead of invoking editor]: :_files'
630 '(-m --message)'{-m,--message=}'[specify message instead of invoking editor]:message'
634 __stg_add_args_savetemplate
() {
636 '--save-template=[save message template to file and exit]: :_files'
640 __stg_add_args_sign
() {
642 '--ack[add Acked-by trailer]'
643 '--ack-by[add Acked-by trailer]:value'
644 '--review[add Reviewed-by trailer]'
645 '--review-by[add Reviewed-by trailer]:value'
646 '--sign[add Signed-off-by trailer]'
647 '--sign-by[add Signed-off-by trailer]:value'
652 _alternative
'heads-local::__stg_heads_local' 'heads-remote::__stg_heads_remote'
655 __stg_heads_local
() {
659 heads
=(${(f)"$(_call_program headrefs git for-each-ref --format='"%(refname:short)"' refs/heads 2>/dev/null)"})
660 gitdir
=$
(_call_program gitdir git rev-parse
--git-dir 2>/dev
/null
)
661 if __stg_git_command_successful
$pipestatus; then
662 for f
in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD
; do
663 [[ -f $gitdir/$f ]] && heads
+=$f
665 [[ -f $gitdir/refs
/stash
]] && heads
+=stash
666 [[ -f $gitdir/refs
/bisect
/bad
]] && heads
+=bisect
/bad
669 __stg_git_describe_commit heads heads-local
"local head" "$@"
672 __stg_heads_remote
() {
675 heads
=(${(f)"$(_call_program headrefs git for-each-ref --format='"%(refname:short)"' refs/remotes 2>/dev/null)"})
677 __stg_git_describe_commit heads heads-remote
"remote head" "$@"
680 __stg_git_command_successful
() {
681 if (( ${#*:#0} > 0 )); then
682 _message
'not a git repository'
688 __stg_git_describe_commit
() {
689 __stg_git_describe_branch
$1 $2 $3 -M 'r:|/=* r:|=*' "${(@)argv[4,-1]}"
692 __stg_git_describe_branch
() {
693 local __commits_in
=$1
699 if zstyle
-s :completion
:$curcontext: max-verbose maxverbose
&&
700 (( ${compstate[nmatches]} <= maxverbose
)); then
703 for __c
in ${(P)__commits_in}; do
704 __commits
+=("${__c}:${$(_call_program describe git rev-list -1 --oneline $__c)//:/\\:}")
706 _describe
-t $__tag $__desc __commits
"$@"
709 _wanted
$__tag expl
$__desc compadd
"$@" -a - $__commits_in
713 __stg_branch_stgit
() {
714 declare -a stg_branches
716 ${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/stacks 2>/dev/null)"}#refs/stacks/}
719 _wanted
-V branches expl
"branch" compadd
$stg_branches
722 __stg_files_relative
() {
724 prefix
=$
(_call_program gitprefix git rev-parse
--show-prefix 2>/dev
/null
)
725 if (( $#prefix == 0 )); then
734 # Collapse "//" and "/./" into "/". Strip any remaining "/." and "/".
735 for file in ${${${${${(0)1}//\/\///}//\
/.\
///}%/.
}%/}; do
737 (( n
= $#file > $#prefix ?
$#file : $#prefix ))
738 for (( i
= 1; i
<= n
; i
++ )); do
739 if [[ $file[i
] != $prefix[i
] ]]; then
740 while (( i
> 0 )) && [[ $file[i-1
] != / ]]; do
747 files
+=${(l@${#prefix[i,-1]//[^\/]}*3@@../@)}${file[i,-1]}
750 print
${(pj:\0:)files}
753 __stg_diff-index_files
() {
754 local tree
=$1 description
=$2 tag
=$3; shift 3
757 # $tree needs to be escaped for _call_program; matters for $tree = "HEAD^"
758 files
=$
(_call_program files git diff-index
-z --name-only --no-color --cached ${(q)tree} 2>/dev
/null
)
759 __stg_git_command_successful
$pipestatus ||
return 1
760 files
=(${(0)"$(__stg_files_relative $files)"})
761 __stg_git_command_successful
$pipestatus ||
return 1
763 _wanted
$tag expl
$description _multi_parts $@
- / files
766 __stg_changed-in-index_files
() {
767 __stg_diff-index_files HEAD
'changed in index file' changed-in-index-files
"$@"
770 __stg_changed-in-working-tree_files
() {
773 files
=$
(_call_program changed-in-working-tree-files git
diff -z --name-only --no-color 2>/dev
/null
)
774 __stg_git_command_successful
$pipestatus ||
return 1
775 files
=(${(0)"$(__stg_files_relative $files)"})
776 __stg_git_command_successful
$pipestatus ||
return 1
778 _wanted changed-in-working-tree-files expl
'changed in working tree file' _multi_parts $@
-f - / files
781 __stg_changed_files
() {
783 'changed-in-index-files::__stg_changed-in-index_files' \
784 'changed-in-working-tree-files::__stg_changed-in-working-tree_files'
787 __stg_files_known
() {
788 declare -a known_files
790 ${(f)"$(_call_program known-files git ls-files 2>/dev/null)"}
793 _wanted
-V known-files expl
"known files" _multi_parts
- / known_files
796 __stg_get_branch_opt
() {
799 long
=${2:-'--branch'}
800 i
=${words[(I)$short|$long(=*|)]}
803 if (( i
< $#words )); then
804 echo "--branch=${words[i + 1]}"
808 echo "--branch=${words[i]#*=}"
813 __stg_want_patches
() {
817 ${(f)"$(_call_program want-patches stg series --no-description --noprefix $@ 2>/dev/null)"}
819 _wanted
-V all expl
"patch" compadd
${patches:|words}
823 __stg_patches_all
() {
824 __stg_want_patches $
(__stg_get_branch_opt
) --all
827 __stg_patches_all_allow_dups
() {
829 local expl branch_opt
830 branch_opt
=$
(__stg_get_branch_opt
)
832 ${(f)"$(_call_program all-patches stg series $branch_opt --no-description --noprefix --all 2>/dev/null)"}
834 _wanted
-V all expl
"patch" compadd
${patches}
837 __stg_patches_applied
() {
838 __stg_want_patches $
(__stg_get_branch_opt
) --applied
841 __stg_patches_hidden
() {
842 __stg_want_patches $
(__stg_get_branch_opt
) --hidden
845 __stg_patches_unapplied
() {
846 __stg_want_patches $
(__stg_get_branch_opt
) --unapplied
849 __stg_patches_unhidden
() {
850 __stg_want_patches $
(__stg_get_branch_opt
) --applied --unapplied
853 __stg_patches_refbranch
() {
854 __stg_want_patches $
(__stg_get_branch_opt
-B --ref-branch) --all
859 remotes
=(${(f)"$(_call_program remotes git remote 2>/dev/null)"})
860 _wanted remotes expl remote compadd
"$@" -a - remotes
863 __stg_subcommands
() {
864 _describe
-t commands
'stgit command' _stg_cmds
867 __stg_caching_policy
() {
868 [[ =$service -nt $1 ]]
872 local curcontext
="$curcontext" state line expl ret
=1
875 # Special cases for git aliases
885 if [ "$words[1]" = git
]; then
891 zstyle
-s ":completion:*:*:stg:*" cache-policy update_policy
892 if [[ -z "$update_policy" ]]; then
893 zstyle
":completion:*:*:stg:*" cache-policy __stg_caching_policy
896 _arguments
-C -A "-*" \
897 '(-)--help[print help information]' \
898 '(*)--version[display version information]' \
900 '*:: :->args' && ret
=0
902 if [[ -n $state ]] && (( ! $
+_stg_cmds
)); then
904 if _cache_invalid stg-cmds ||
! _retrieve_cache stg-cmds
; then
906 ${${${(M)${(f)"$(stg help 2> /dev/null)"}## *}# }/#(#b)([^[:space:]]##)[[:space:]]##(*)/$match[1]:$match[2]}
908 if (( $?
== 0 )); then
909 _store_cache stg-cmds _stg_cmds
918 __stg_subcommands
&& ret
=0
922 local subcmd
=$words[1]
923 curcontext
="${curcontext%:*:*}:stg-$subcmd:"
924 if ! _call_function ret _stg-
$subcmd; then
925 _message
"unknown sub-command: $subcmd"