All of lore.kernel.org
 help / color / mirror / Atom feed
From: "SZEDER Gábor" <szeder.dev@gmail.com>
To: Denton Liu <liu.denton@gmail.com>
Cc: Git Mailing List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2 3/3] git-completion.bash: use __gitcomp_builtin() in _git_stash()
Date: Sun, 28 Mar 2021 13:04:27 +0200	[thread overview]
Message-ID: <20210328110427.GG2271@szeder.dev> (raw)
In-Reply-To: <680f3a3146355e6b09aa2e0f59471c7695954a1b.1616574955.git.liu.denton@gmail.com>

On Wed, Mar 24, 2021 at 01:36:29AM -0700, Denton Liu wrote:
> 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>
> ---
>  contrib/completion/git-completion.bash | 42 ++++++++++++--------------
>  1 file changed, 20 insertions(+), 22 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 8d4d8cc0fe..c926ca26c6 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -3013,26 +3013,19 @@ _git_sparse_checkout ()
>  
>  _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 subcommand="$(__git_find_on_cmdline "$subcommands save")"
> -	if [ -z "$subcommand" -a -n "$(__git_find_on_cmdline "-p")" ]; then
> -		subcommand="push"
> -	fi

I think it would be better to keep this condition ...

> +
>  	if [ -z "$subcommand" ]; then
> -		case "$cur" in
> -		--*)
> -			__gitcomp "$save_opts"
> +		case "$((cword - __git_subcommand_idx)),$cur" in

... and not bring in such magic with the indices here and ...

> +		*,--*)
> +			__gitcomp_builtin stash_push
>  			;;
> -		sa*)
> -			if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
> -				__gitcomp "save"
> -			fi
> +		1,sa*)
> +			__gitcomp "save"
>  			;;
> -		*)
> -			if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
> -				__gitcomp "$subcommands"
> -			fi
> +		1,*)
> +			__gitcomp "$subcommands"

... here in these two case arms, but instead handle the cases both
with and without a subcommand in a unified case statement as I
suggested in reply to the previous patch.

>  			;;
>  		esac
>  		return
> @@ -3040,24 +3033,29 @@ _git_stash ()
>  
>  	case "$subcommand,$cur" in
>  	push,--*)
> -		__gitcomp "$save_opts --message"
> +		__gitcomp_builtin stash_push
>  		;;
>  	save,--*)
> -		__gitcomp "$save_opts"
> +		__gitcomp_builtin stash_save
>  		;;
> -	apply,--*|pop,--*)
> -		__gitcomp "--index --quiet"
> +	pop,--*)
> +		__gitcomp_builtin stash_pop
> +		;;
> +	apply,--*)
> +		__gitcomp_builtin stash_apply
>  		;;
>  	drop,--*)
> -		__gitcomp "--quiet"
> +		__gitcomp_builtin stash_drop
>  		;;

These case arms are still quite repetitive, and could be handled by a
single arm like this:

        *,--*)
                __gitcomp_builtin stash_$subcommand
                ;;

Of course the more specific 'list,--*' and 'show,--*' cases should be
handled before.

The end result would look something like this (taken from a WIP patch
series of mine, which has been in a WIP state for about a year and a
half now...):

  https://github.com/szeder/git/commit/83a0e138767040280750062c5c0d43886796fcb1


>  	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,--*)
> -		__gitcomp "$__git_diff_common_options"
> +		__gitcomp_builtin stash_show "$__git_diff_common_options"
>  		;;
>  	branch,--*)
> +		__gitcomp_builtin stash_branch
>  		;;
>  	branch,*)
>  		if [ $cword -eq $((__git_subcommand_idx+2)) ]; then
> -- 
> 2.31.0.rc2.261.g7f71774620
> 

      reply	other threads:[~2021-03-28 11:05 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-16  0:54 [PATCH 0/3] git-completion.bash: improvements to _git_stash() Denton Liu
2021-03-16  0:54 ` [PATCH 1/3] git-completion.bash: extract from else in _git_stash() Denton Liu
2021-03-16  0:54 ` [PATCH 2/3] git-completion.bash: fix `git <args>... stash branch` bug Denton Liu
2021-03-16  0:54 ` [PATCH 3/3] git-completion.bash: use __gitcomp_builtin() in _git_stash() Denton Liu
2021-03-18  9:46 ` [RESEND PATCH 0/3] git-completion.bash: improvements to _git_stash() Denton Liu
2021-03-18  9:46   ` [RESEND PATCH 1/3] git-completion.bash: extract from else in _git_stash() Denton Liu
2021-03-18  9:46   ` [RESEND PATCH 2/3] git-completion.bash: fix `git <args>... stash branch` bug Denton Liu
2021-03-18 20:30     ` Junio C Hamano
2021-03-19  8:05       ` Denton Liu
2021-03-19 15:53         ` Junio C Hamano
2021-03-18  9:46   ` [RESEND PATCH 3/3] git-completion.bash: use __gitcomp_builtin() in _git_stash() Denton Liu
2021-03-18 21:58   ` [RESEND PATCH 0/3] git-completion.bash: improvements to _git_stash() Junio C Hamano
2021-03-19  8:09     ` Denton Liu
2021-03-19 15:57       ` Junio C Hamano
2021-03-24  8:36 ` [PATCH v2 " Denton Liu
2021-03-24  8:36   ` [PATCH v2 1/3] git-completion.bash: pass $__git_subcommand_idx from __git_main() Denton Liu
2021-03-27 18:35     ` SZEDER Gábor
2021-03-28 10:31     ` SZEDER Gábor
2021-03-24  8:36   ` [PATCH v2 2/3] git-completion.bash: extract from else in _git_stash() Denton Liu
2021-03-28 10:30     ` SZEDER Gábor
2021-03-24  8:36   ` [PATCH v2 3/3] git-completion.bash: use __gitcomp_builtin() " Denton Liu
2021-03-28 11:04     ` SZEDER Gábor [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210328110427.GG2271@szeder.dev \
    --to=szeder.dev@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=liu.denton@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.