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>

Denton Liu committed Mar 24, 2021 at 01:36 UTC 61318078640dc1bbe07c5d762f7a581b7408d623
1 file changed +20 -22
contrib/completion/git-completion.bash
+20 -22
@@ -3013,26 +3013,19 @@ _git_sparse_checkout ()
3013
3014 _git_stash ()
3015 {
3016 - local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
3016 local subcommands='push list show apply clear drop pop create branch'
3017 local subcommand="$(__git_find_on_cmdline "$subcommands save")"
3019 - if [ -z "$subcommand" -a -n "$(__git_find_on_cmdline "-p")" ]; then
3020 - subcommand="push"
3021 - fi
3018 +
3019 if [ -z "$subcommand" ]; then
3023 - case "$cur" in
3024 - --*)
3025 - __gitcomp "$save_opts"
3020 + case "$((cword - __git_subcommand_idx)),$cur" in
3021 + *,--*)
3022 + __gitcomp_builtin stash_push
3023 ;;
3027 - sa*)
3028 - if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
3029 - __gitcomp "save"
3030 - fi
3024 + 1,sa*)
3025 + __gitcomp "save"
3026 ;;
3032 - *)
3033 - if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
3034 - __gitcomp "$subcommands"
3035 - fi
3027 + 1,*)
3028 + __gitcomp "$subcommands"
3029 ;;
3030 esac
3031 return
@@ -3040,24 +3033,29 @@ _git_stash ()
3033
3034 case "$subcommand,$cur" in
3035 push,--*)
3043 - __gitcomp "$save_opts --message"
3036 + __gitcomp_builtin stash_push
3037 ;;
3038 save,--*)
3046 - __gitcomp "$save_opts"
3039 + __gitcomp_builtin stash_save
3040 + ;;
3041 + pop,--*)
3042 + __gitcomp_builtin stash_pop
3043 ;;
3048 - apply,--*|pop,--*)
3049 - __gitcomp "--index --quiet"
3044 + apply,--*)
3045 + __gitcomp_builtin stash_apply
3046 ;;
3047 drop,--*)
3052 - __gitcomp "--quiet"
3048 + __gitcomp_builtin stash_drop
3049 ;;
3050 list,--*)
3055 - __gitcomp "--name-status --oneline --patch-with-stat"
3051 + # NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
3052 + __gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
3053 ;;
3054 show,--*)
3058 - __gitcomp "$__git_diff_common_options"
3055 + __gitcomp_builtin stash_show "$__git_diff_common_options"
3056 ;;
3057 branch,--*)
3058 + __gitcomp_builtin stash_branch
3059 ;;
3060 branch,*)
3061 if [ $cword -eq $((__git_subcommand_idx+2)) ]; then