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
78 '(-a --all)'{-a,--all}'[commit all unapplied patches]'
80 '(-n --number)'{-n+,--number=}'[commit specified number of patches]:number'
82 '*:applied patches:__stg_patches_applied'
84 _arguments
-s -S $subcmd_args
91 '--spill[spill patch contents to worktree and index]'
93 '(-t --top)'{-t,--top}'[delete top patch]'
95 '*:patches:__stg_patches_all'
97 _arguments
-s -S $subcmd_args
102 __stg_add_args_diffopts
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
113 __stg_add_args_author
114 __stg_add_args_diffopts
117 __stg_add_args_savetemplate
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
130 __stg_add_args_branch
131 __stg_add_args_diffopts
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'
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
147 __stg_add_args_diffopts
149 '--bare[bare file names]'
150 '(-s --stat)'{-s,--stat}'[show diff stat]'
151 ':patches:__stg_patches_all'
153 _arguments
-s -S $subcmd_args
160 '--noapply[Reorder patches by floating without applying]'
161 '(-s --series)'{-s,--series=}'[arrange according to series file]: :_files'
162 '*:patches:__stg_patches_all'
164 _arguments
-s -S $subcmd_args
170 '(-b --base)'{-b,--base=}'[apply on base commit instead of HEAD]:commit'
171 '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num'
172 '-C=[ensure N lines of surrounding context for each change]:num'
173 '--reject[leave rejected hunks in .rej files]'
176 _arguments
-s -S $subcmd_args
182 __stg_add_args_merged
184 ':patches:__stg_patches_all'
186 _arguments
-s -S $subcmd_args
190 _arguments
-s ':commands:__stg_subcommands'
195 __stg_add_args_branch
197 ':patches:__stg_patches_unhidden'
199 _arguments
-s -S $subcmd_args
205 ':references:__stg_patches_all'
207 _arguments
-s -S $subcmd_args
212 __stg_add_args_author
216 '(-n --name)'{-n,--name}'[name for imported patch]'
217 '(-p --strip)'{-p+,--strip=}'[remove N leading directories from diff paths]:num'
218 '(-t --stripname)'{-t,--stripname}'[strip number and extension from patch name]'
219 '-C=[ensure N lines of surrounding context for each change]:num'
220 '(-i --ignore)'{-i,--ignore}'[ingore applied patches in series]'
221 '--replace[replace unapplied patches in series]'
222 '--reject[leave rejected hunks in .rej files]'
223 '--keep-cr[do not remove CR from email lines ending with CRLF]'
224 '(-d --showdiff)'{-d,--showdiff}'[show patch content in editor buffer]'
227 '(-m --mail)'{-m,--mail}'[import from standard email file]'
228 '(-M --mbox)'{-M,--mbox}'[import from mbox file]'
229 '(-s --series)'{-s,--series}'[import from series file]'
230 '(-u --url)'{-u,--url}'[import patch from URL]'
232 _arguments
-s -S $subcmd_args
237 _arguments
-s $subcmd_args
242 __stg_add_args_branch
244 '--clear[clear log history]'
245 '(-d --diff)'{-d,--diff}'[show refresh diffs]'
246 '(-f --full)'{-f,--full}'[show full commit ids]'
247 '(-g --graphical)'{-g,--graphical}'[show log in gitk]'
248 '(-n --number)'{-n+,--number=}'[limit to number of commits]'
249 '*:patches:__stg_patches_all'
251 _arguments
-s -S $subcmd_args
256 __stg_add_args_branch
257 __stg_add_args_diffopts
259 '--to=[add address to To: list]:address'
260 '--cc=[add address to Cc: list]:address'
261 '--bcc=[add address to Bcc: list]:address'
262 '--auto[automatically cc patch signers]'
263 '--no-thread[do not send subsequent messages as replies]'
264 '--unrelated[send patches without sequence numbers]'
265 '(-v --version)'{-v,--version=}'[add version to subject prefix]:version'
266 '--prefix=[add prefix to subject]:prefix'
267 '(-c --cover)'{-c,--cover=}'[cover message file]: :_files'
268 '(-e --edit-cover)'{-e,--edit-cover}'[edit cover message before sending]'
269 '(-E --edit-patches)'{-E,--edit-patches}'[edit patches before sending]'
270 '(-s --sleep)'{-s,--sleep=}'[seconds to sleep between sending emails]:seconds'
271 '--in-reply-to=[reply reference id]:refid'
272 '--domain=[domain to use for message ID]:domain'
273 '(-u --user)'{-u,--user=}'[username for SMTP authentication]:user'
274 '(-p --password)'{-p,--password=}'[password for SMTP authentication]:password'
275 '(-T --smtp-tls)'{-t,--smtp-tls}'[use TLS for SMTP authentication]'
277 '--git[use `git send-email`]'
278 '(-m --mbox)'{-m,--mbox}'[generate mbox file instead of sending]'
279 '--smtp-server=[server or command for sending email]'
281 '--attach[send patch as attachment]'
282 '--attach-inline[send patch as inline attachment]'
283 '(-t --template)'{-t,--template=}'[message template file]: :_files'
285 '(-a --all)'{-a,--all}'[email all applied patches]'
286 '*:patches:__stg_patches_all'
288 _arguments
-s -S $subcmd_args
293 __stg_add_args_author
296 __stg_add_args_savetemplate
298 '(-v --verbose)'{-v,--verbose}'[show diff of file changes]'
299 ':: :_guard "([^-]?#|)" name'
301 __stg_add_args_message
302 _arguments
-s -S $subcmd_args
307 __stg_add_args_branch
308 _arguments
-s -S $subcmd_args
313 __stg_add_args_branch
314 __stg_add_args_diffopts
316 '(-d --diff)'{-d,--diff}'[show diffs of given files]'
317 '*:files:__stg_files_known'
319 _arguments
-s -S $subcmd_args
323 # TODO: complete --parent commit id
326 '(-n --name)'{-n,--name=}'[name for picked patch]:name'
327 '(-B --ref-branch)'{-B,--ref-branch=}'[pick patches from branch]: :__stg_branch_stgit'
328 '(-r --revert)'{-r,--revert}'[revert given commit object]'
329 '(-p --parent=)'{-p,--parent}'[use commit id as parent]:commit'
330 '(-x --expose)'{-x,--expose}'[append imported commit id to patch log]'
331 '--unapplied[keep patch unapplied]'
332 '*'{-f,--file=}'[only fold given file]: :_files'
333 '*:patches:__stg_patches_refbranch'
335 '--fold[fold the commit into current patch]'
336 '--update[fold limited to current patch files]'
338 _arguments
-s -S $subcmd_args
345 '(-s --spill)'{-s,--spill}'[pop a patch keeping its modifications in the tree]'
347 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
349 '(-a --all)'{-a,--all}'[push all unapplied patches]'
351 '*:applied patches:__stg_patches_applied'
353 _arguments
-s -S $subcmd_args
358 __stg_add_args_branch
359 _arguments
-s -S $subcmd_args
364 __stg_add_args_merged
366 '(-n --nopush)'{-n,--nopush}'[do not push patches after rebasing]'
367 ':repository:__stg_remotes'
369 _arguments
-s -S $subcmd_args
375 __stg_add_args_merged
377 '--reverse[push patches in reverse order]'
378 '--set-tree[push patch with the original tree]'
380 '(-a --all)'{-a,--all}'[push all unapplied patches]'
382 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
384 '*:unapplied patches:__stg_patches_unapplied'
386 _arguments
-s -S $subcmd_args
391 __stg_add_args_merged
393 '(-n --nopush)'{-n,--nopush}'[do not push patches after rebasing]'
394 ':new-base-id:__stg_heads'
396 _arguments
-s -S $subcmd_args
402 '--hard[discard changes in index/worktree]'
403 '(-n --number)'{-n+,--number=}'[number of undos to redo]:number'
405 _arguments
-s -S $subcmd_args
410 __stg_add_args_author
414 __stg_add_args_diffopts
416 '(-a --annotate)'{-a,--annotate=}'[annotate patch log entry]:note'
417 '(-d --diff)'{-d,--diff}'[show diff when editing patch message]'
418 '(-F --force)'{-F,--force}'[force refresh even if index is dirty]'
419 '(-i --index)'{-i,--index}'[refresh from index instead of worktree]'
420 '(-p --patch)'{-p,--patch=}'[refresh patch other than top patch]: :__stg_patches_applied'
421 '--spill[Spill patch contents to worktree and index, and erase patch content]'
423 '(-u --update)'{-u,--update}'[only update current patch files]'
424 '*:files:__stg_changed_files'
426 '(-s --submodules)'{-s,--submodules}'[include submodules in refresh]'
427 '--no-submodules[exclude submodules from refresh]'
429 __stg_add_args_message
430 _arguments
-s -S $subcmd_args
435 __stg_add_args_branch
437 ':old-patch:__stg_patches_all'
439 _arguments
-s -S $subcmd_args
444 _arguments
-s $subcmd_args
450 '--hard[discard changes in index/worktree]'
452 '*:patches:__stg_patches_all'
454 _arguments
-s -S $subcmd_args
459 __stg_add_args_branch
461 '--author[show the author name for each patch]'
462 '(-c --count)'{-c,--count}'[print number of patches]'
463 '(-d --description)'{-d,--description}'[show short descriptions]'
464 '--no-description[do not show patch descriptions]'
465 '(-e --empty)'{-e,--empty}'[identify empty patches]'
466 '(-m --missing)'{-m,--missing=}'[show patches from branch missing in current]: :__stg_branch_stgit'
467 '--noprefix[do not show the patch status prefix]'
468 '(-s --short)'{-s,--short}'[list just patches around the topmost patch]'
469 '--showbranch[show branch name of listed patches]'
471 '(-A --applied)'{-A,--applied}'[show applied patches]'
472 '(-H --hidden)'{-H,--hidden}'[show hidden patches]'
473 '(-U --unapplied)'{-U,--unapplied}'[show unapplied patches]'
475 '(-a --all)'{-a,--all}'[show all patches including hidden]'
477 ':patches:__stg_patches_all'
479 _arguments
-s -S $subcmd_args
484 __stg_add_args_branch
485 __stg_add_args_diffopts
487 '(-s --stat)'{-s,--stat}'[show diff stat]'
488 '*:patches:__stg_patches_all'
489 + '(patch-selection)'
490 '(-a --applied)'{-a,--applied}'[show applied patches]'
491 '(-u --unapplied)'{-u,--unapplied}'[show unapplied patches]'
493 _arguments
-s -S $subcmd_args
500 '(-n --nopush)'{-n,--nopush}'[do not push patches after sinking]'
501 '(-t --to)'{-t,--to=}'[sink patches below target patch]: :__stg_patches_applied'
502 ':patches:__stg_patches_all'
504 _arguments
-s -S $subcmd_args
510 __stg_add_args_savetemplate
512 '(-n --name)'{-n,--name=}'[name for squashed patch]: :__stg_patches_all'
513 '*:patches:__stg_patches_all_allow_dups'
515 __stg_add_args_message
516 _arguments
-s -S $subcmd_args
523 '(-a --all)'{-a,--all}'[synchronize all applied patches]'
524 '*:patches:__stg_patches_refbranch'
526 '(-B --ref-branch)'{-B,--ref-branch}'[synchronize patches with branch]: :__stg_branch_stgit'
527 '(-s --series)'{-s,--series=}'[synchronize patches with series]: :_files'
529 _arguments
-s -S $subcmd_args
534 __stg_add_args_branch
535 _arguments
-s -S $subcmd_args
542 '(-n --number)'{-n+,--number=}'[push specified number of patches]:number'
545 '(-t --to)'{-t,--to=}'[uncommit to the specified commit]:commit'
546 '(-x --exclusive)'{-x,--exclusive}'[exclude the commit specified by --to]'
548 '*: :_guard "([^-]?#|)" names'
550 _arguments
-s -S $subcmd_args
556 '--hard[discard changes in index/worktree]'
557 '(-n --number)'{-n+,--number=}'[number commands to undo]:number'
559 _arguments
-s -S $subcmd_args
564 __stg_add_args_branch
566 ':patches:__stg_patches_hidden'
568 _arguments
-s -S $subcmd_args
571 __stg_add_args_author
() {
573 '--author=[set author details]'
574 '--authdate=[set author date]:date'
575 '--authemail=[set author email]:email'
576 '--authname=[set author name]:name'
580 __stg_add_args_branch
() {
582 '(-b --branch)'{-b,--branch=}'[specify another branch]: :__stg_branch_stgit'
586 __stg_add_args_diffopts
() {
587 # TODO: complete diff-opts values (with separators?)
589 '(-O --diff-opts)'{-O+,--diff-opts=}'[extra options for git diff]:opts'
593 __stg_add_args_edit
() {
595 '(-e --edit)'{-e,--edit}'[invoke interactive editor]'
599 __stg_add_args_help
() {
601 '(- *)'{-h,--help}'[show help message and exit]'
605 __stg_add_args_hook
() {
607 '--no-verify[disable commit-msg hook]'
611 __stg_add_args_keep
() {
613 '(-k --keep)'{-k,--keep}'[keep local changes]'
617 __stg_add_args_merged
() {
619 '(-m --merged)'{-m,--merged}'[check for patches merged upstream]'
623 __stg_add_args_message
() {
626 '(-f --file)'{-f,--file=}'[use message file instead of invoking editor]: :_files'
627 '(-m --message)'{-m,--message=}'[specify message instead of invoking editor]:message'
631 __stg_add_args_savetemplate
() {
633 '--save-template=[save message template to file and exit]: :_files'
637 __stg_add_args_sign
() {
639 '--ack[add Acked-by trailer]'
640 '--ack-by[add Acked-by trailer]:value'
641 '--review[add Reviewed-by trailer]'
642 '--review-by[add Reviewed-by trailer]:value'
643 '--sign[add Signed-off-by trailer]'
644 '--sign-by[add Signed-off-by trailer]:value'
649 _alternative
'heads-local::__stg_heads_local' 'heads-remote::__stg_heads_remote'
652 __stg_heads_local
() {
656 heads
=(${(f)"$(_call_program headrefs git for-each-ref --format='"%(refname:short)"' refs/heads 2>/dev/null)"})
657 gitdir
=$
(_call_program gitdir git rev-parse
--git-dir 2>/dev
/null
)
658 if __stg_git_command_successful
$pipestatus; then
659 for f
in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD
; do
660 [[ -f $gitdir/$f ]] && heads
+=$f
662 [[ -f $gitdir/refs
/stash
]] && heads
+=stash
663 [[ -f $gitdir/refs
/bisect
/bad
]] && heads
+=bisect
/bad
666 __stg_git_describe_commit heads heads-local
"local head" "$@"
669 __stg_heads_remote
() {
672 heads
=(${(f)"$(_call_program headrefs git for-each-ref --format='"%(refname:short)"' refs/remotes 2>/dev/null)"})
674 __stg_git_describe_commit heads heads-remote
"remote head" "$@"
677 __stg_git_command_successful
() {
678 if (( ${#*:#0} > 0 )); then
679 _message
'not a git repository'
685 __stg_git_describe_commit
() {
686 __stg_git_describe_branch
$1 $2 $3 -M 'r:|/=* r:|=*' "${(@)argv[4,-1]}"
689 __stg_git_describe_branch
() {
690 local __commits_in
=$1
696 if zstyle
-s :completion
:$curcontext: max-verbose maxverbose
&&
697 (( ${compstate[nmatches]} <= maxverbose
)); then
700 for __c
in ${(P)__commits_in}; do
701 __commits
+=("${__c}:${$(_call_program describe git rev-list -1 --oneline $__c)//:/\\:}")
703 _describe
-t $__tag $__desc __commits
"$@"
706 _wanted
$__tag expl
$__desc compadd
"$@" -a - $__commits_in
710 __stg_branch_stgit
() {
711 declare -a stg_branches
713 ${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/stacks 2>/dev/null)"}#refs/stacks/}
716 _wanted
-V branches expl
"branch" compadd
$stg_branches
719 __stg_files_relative
() {
721 prefix
=$
(_call_program gitprefix git rev-parse
--show-prefix 2>/dev
/null
)
722 if (( $#prefix == 0 )); then
731 # Collapse "//" and "/./" into "/". Strip any remaining "/." and "/".
732 for file in ${${${${${(0)1}//\/\///}//\
/.\
///}%/.
}%/}; do
734 (( n
= $#file > $#prefix ?
$#file : $#prefix ))
735 for (( i
= 1; i
<= n
; i
++ )); do
736 if [[ $file[i
] != $prefix[i
] ]]; then
737 while (( i
> 0 )) && [[ $file[i-1
] != / ]]; do
744 files
+=${(l@${#prefix[i,-1]//[^\/]}*3@@../@)}${file[i,-1]}
747 print
${(pj:\0:)files}
750 __stg_diff-index_files
() {
751 local tree
=$1 description
=$2 tag
=$3; shift 3
754 # $tree needs to be escaped for _call_program; matters for $tree = "HEAD^"
755 files
=$
(_call_program files git diff-index
-z --name-only --no-color --cached ${(q)tree} 2>/dev
/null
)
756 __stg_git_command_successful
$pipestatus ||
return 1
757 files
=(${(0)"$(__stg_files_relative $files)"})
758 __stg_git_command_successful
$pipestatus ||
return 1
760 _wanted
$tag expl
$description _multi_parts $@
- / files
763 __stg_changed-in-index_files
() {
764 __stg_diff-index_files HEAD
'changed in index file' changed-in-index-files
"$@"
767 __stg_changed-in-working-tree_files
() {
770 files
=$
(_call_program changed-in-working-tree-files git
diff -z --name-only --no-color 2>/dev
/null
)
771 __stg_git_command_successful
$pipestatus ||
return 1
772 files
=(${(0)"$(__stg_files_relative $files)"})
773 __stg_git_command_successful
$pipestatus ||
return 1
775 _wanted changed-in-working-tree-files expl
'changed in working tree file' _multi_parts $@
-f - / files
778 __stg_changed_files
() {
780 'changed-in-index-files::__stg_changed-in-index_files' \
781 'changed-in-working-tree-files::__stg_changed-in-working-tree_files'
784 __stg_files_known
() {
785 declare -a known_files
787 ${(f)"$(_call_program known-files git ls-files 2>/dev/null)"}
790 _wanted
-V known-files expl
"known files" _multi_parts
- / known_files
793 __stg_get_branch_opt
() {
796 long
=${2:-'--branch'}
797 i
=${words[(I)$short|$long(=*|)]}
800 if (( i
< $#words )); then
801 echo "--branch=${words[i + 1]}"
805 echo "--branch=${words[i]#*=}"
810 __stg_want_patches
() {
814 ${(f)"$(_call_program want-patches stg series --no-description --noprefix $@ 2>/dev/null)"}
816 _wanted
-V all expl
"patch" compadd
${patches:|words}
820 __stg_patches_all
() {
821 __stg_want_patches $
(__stg_get_branch_opt
) --all
824 __stg_patches_all_allow_dups
() {
826 local expl branch_opt
827 branch_opt
=$
(__stg_get_branch_opt
)
829 ${(f)"$(_call_program all-patches stg series $branch_opt --no-description --noprefix --all 2>/dev/null)"}
831 _wanted
-V all expl
"patch" compadd
${patches}
834 __stg_patches_applied
() {
835 __stg_want_patches $
(__stg_get_branch_opt
) --applied
838 __stg_patches_hidden
() {
839 __stg_want_patches $
(__stg_get_branch_opt
) --hidden
842 __stg_patches_unapplied
() {
843 __stg_want_patches $
(__stg_get_branch_opt
) --unapplied
846 __stg_patches_unhidden
() {
847 __stg_want_patches $
(__stg_get_branch_opt
) --applied --unapplied
850 __stg_patches_refbranch
() {
851 __stg_want_patches $
(__stg_get_branch_opt
-B --ref-branch) --all
856 remotes
=(${(f)"$(_call_program remotes git remote 2>/dev/null)"})
857 _wanted remotes expl remote compadd
"$@" -a - remotes
860 __stg_subcommands
() {
861 _describe
-t commands
'stgit command' _stg_cmds
864 __stg_caching_policy
() {
865 [[ =$service -nt $1 ]]
869 local curcontext
="$curcontext" state line expl ret
=1
872 # Special cases for git aliases
882 if [ "$words[1]" = git
]; then
888 zstyle
-s ":completion:*:*:stg:*" cache-policy update_policy
889 if [[ -z "$update_policy" ]]; then
890 zstyle
":completion:*:*:stg:*" cache-policy __stg_caching_policy
893 _arguments
-C -A "-*" \
894 '(-)--help[print help information]' \
895 '(*)--version[display version information]' \
897 '*:: :->args' && ret
=0
899 if [[ -n $state ]] && (( ! $
+_stg_cmds
)); then
901 if _cache_invalid stg-cmds ||
! _retrieve_cache stg-cmds
; then
903 ${${${(M)${(f)"$(stg help 2> /dev/null)"}## *}# }/#(#b)([^[:space:]]##)[[:space:]]##(*)/$match[1]:$match[2]}
905 if (( $?
== 0 )); then
906 _store_cache stg-cmds _stg_cmds
915 __stg_subcommands
&& ret
=0
919 local subcmd
=$words[1]
920 curcontext
="${curcontext%:*:*}:stg-$subcmd:"
921 if ! _call_function ret _stg-
$subcmd; then
922 _message
"unknown sub-command: $subcmd"