git-completion.bash: use __gitcomp_builtin() in _git_stash()

The completion for 'git stash' has not changed in a major way since it
was converted from shell script to builtin. Now that it's a builtin, we
can take advantage of the groundwork laid out by parse-options and use
the generated options.

Rewrite _git_stash() to take use __gitcomp_builtin() to generate
completions for subcommands.

The main `git stash` command does not take any arguments directly. If no
subcommand is given, it automatically defaults to `git stash push`. This
means that we can simplify the logic for when no subcommands have been
given yet. We only have to offer subcommand completions when we're
completing a non-option after "stash".

One area that this patch could improve upon is that the `git stash list`
command accepts log-options. It would be nice if the completion for this
were unified with that of _git_log() and _git_show() which would allow
completions to be provided for options such as `--pretty` but that is
outside the scope of this patch.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Denton Liu
2021-03-24 01:36:29 -07:00
committed by Junio C Hamano
parent 42b30bcbb7
commit 6131807864

View File

@ -3013,26 +3013,19 @@ _git_sparse_checkout ()
_git_stash () _git_stash ()
{ {
local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
local subcommands='push list show apply clear drop pop create branch' local subcommands='push list show apply clear drop pop create branch'
local subcommand="$(__git_find_on_cmdline "$subcommands save")" local subcommand="$(__git_find_on_cmdline "$subcommands save")"
if [ -z "$subcommand" -a -n "$(__git_find_on_cmdline "-p")" ]; then
subcommand="push"
fi
if [ -z "$subcommand" ]; then if [ -z "$subcommand" ]; then
case "$cur" in case "$((cword - __git_subcommand_idx)),$cur" in
--*) *,--*)
__gitcomp "$save_opts" __gitcomp_builtin stash_push
;; ;;
sa*) 1,sa*)
if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then __gitcomp "save"
__gitcomp "save"
fi
;; ;;
*) 1,*)
if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then __gitcomp "$subcommands"
__gitcomp "$subcommands"
fi
;; ;;
esac esac
return return
@ -3040,24 +3033,29 @@ _git_stash ()
case "$subcommand,$cur" in case "$subcommand,$cur" in
push,--*) push,--*)
__gitcomp "$save_opts --message" __gitcomp_builtin stash_push
;; ;;
save,--*) save,--*)
__gitcomp "$save_opts" __gitcomp_builtin stash_save
;; ;;
apply,--*|pop,--*) pop,--*)
__gitcomp "--index --quiet" __gitcomp_builtin stash_pop
;;
apply,--*)
__gitcomp_builtin stash_apply
;; ;;
drop,--*) drop,--*)
__gitcomp "--quiet" __gitcomp_builtin stash_drop
;; ;;
list,--*) list,--*)
__gitcomp "--name-status --oneline --patch-with-stat" # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
__gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
;; ;;
show,--*) show,--*)
__gitcomp "$__git_diff_common_options" __gitcomp_builtin stash_show "$__git_diff_common_options"
;; ;;
branch,--*) branch,--*)
__gitcomp_builtin stash_branch
;; ;;
branch,*) branch,*)
if [ $cword -eq $((__git_subcommand_idx+2)) ]; then if [ $cword -eq $((__git_subcommand_idx+2)) ]; then