git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH] contrib: teach completion about git-worktree options and arguments
@ 2015-07-23 20:49 Eric Sunshine
  2015-08-06 16:29 ` Eric Sunshine
  0 siblings, 1 reply; 6+ messages in thread
From: Eric Sunshine @ 2015-07-23 20:49 UTC (permalink / raw)
  To: git; +Cc: Eric Sunshine

Complete subcommands 'add' and 'prune', as well as their respective
options --force, --detach, --dry-run, --verbose, and --expire. Also
complete 'refname' in "git worktree add [-b <newbranch>] <path>
<refname>".

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---

This is RFC since this is my first foray into the Git completion script,
and there are likely better and more idiomatic ways to accomplish this.

 contrib/completion/git-completion.bash | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c97c648..07c34ef 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2564,6 +2564,38 @@ _git_whatchanged ()
 	_git_log
 }
 
+_git_worktree ()
+{
+	local subcommands='add prune'
+	local subcommand="$(__git_find_on_cmdline "$subcommands")"
+	local c=2 n=0 refpos=2
+	if [ -z "$subcommand" ]; then
+		__gitcomp "$subcommands"
+	else
+		case "$subcommand,$cur" in
+		add,--*)
+			__gitcomp "--force --detach"
+			;;
+		add,*)
+			while [ $c -lt $cword ]; do
+				case "${words[c]}" in
+				--*) ;;
+				-[bB]) ((refpos++)) ;;
+				*) ((n++)) ;;
+				esac
+				((c++))
+			done
+			if [ $n -eq $refpos ]; then
+				__gitcomp_nl "$(__git_refs)"
+			fi
+			;;
+		prune,--*)
+			__gitcomp "--dry-run --verbose --expire"
+			;;
+		esac
+	fi
+}
+
 __git_main ()
 {
 	local i c=1 command __git_dir
-- 
2.5.0.rc3.407.g68aafd0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments
  2015-07-23 20:49 [RFC/PATCH] contrib: teach completion about git-worktree options and arguments Eric Sunshine
@ 2015-08-06 16:29 ` Eric Sunshine
  2015-08-21 17:21   ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Eric Sunshine @ 2015-08-06 16:29 UTC (permalink / raw)
  To: Git List; +Cc: Eric Sunshine, SZEDER Gábor

On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> Complete subcommands 'add' and 'prune', as well as their respective
> options --force, --detach, --dry-run, --verbose, and --expire. Also
> complete 'refname' in "git worktree add [-b <newbranch>] <path>
> <refname>".

Ping[1]?

[1]: http://article.gmane.org/gmane.comp.version-control.git/274526

> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
>
> This is RFC since this is my first foray into the Git completion script,
> and there are likely better and more idiomatic ways to accomplish this.
>
>  contrib/completion/git-completion.bash | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index c97c648..07c34ef 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -2564,6 +2564,38 @@ _git_whatchanged ()
>         _git_log
>  }
>
> +_git_worktree ()
> +{
> +       local subcommands='add prune'
> +       local subcommand="$(__git_find_on_cmdline "$subcommands")"
> +       local c=2 n=0 refpos=2
> +       if [ -z "$subcommand" ]; then
> +               __gitcomp "$subcommands"
> +       else
> +               case "$subcommand,$cur" in
> +               add,--*)
> +                       __gitcomp "--force --detach"
> +                       ;;
> +               add,*)
> +                       while [ $c -lt $cword ]; do
> +                               case "${words[c]}" in
> +                               --*) ;;
> +                               -[bB]) ((refpos++)) ;;
> +                               *) ((n++)) ;;
> +                               esac
> +                               ((c++))
> +                       done
> +                       if [ $n -eq $refpos ]; then
> +                               __gitcomp_nl "$(__git_refs)"
> +                       fi
> +                       ;;
> +               prune,--*)
> +                       __gitcomp "--dry-run --verbose --expire"
> +                       ;;
> +               esac
> +       fi
> +}
> +
>  __git_main ()
>  {
>         local i c=1 command __git_dir
> --
> 2.5.0.rc3.407.g68aafd0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments
  2015-08-06 16:29 ` Eric Sunshine
@ 2015-08-21 17:21   ` Junio C Hamano
  2015-08-21 20:49     ` SZEDER Gábor
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2015-08-21 17:21 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Git List, Eric Sunshine

Eric Sunshine <sunshine@sunshineco.com> writes:

> On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>> Complete subcommands 'add' and 'prune', as well as their respective
>> options --force, --detach, --dry-run, --verbose, and --expire. Also
>> complete 'refname' in "git worktree add [-b <newbranch>] <path>
>> <refname>".
>
> Ping[1]?
>
> [1]: http://article.gmane.org/gmane.comp.version-control.git/274526

Ping indeed?

>
>> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
>> ---
>>
>> This is RFC since this is my first foray into the Git completion script,
>> and there are likely better and more idiomatic ways to accomplish this.
>>
>>  contrib/completion/git-completion.bash | 32 ++++++++++++++++++++++++++++++++
>>  1 file changed, 32 insertions(+)
>>
>> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
>> index c97c648..07c34ef 100644
>> --- a/contrib/completion/git-completion.bash
>> +++ b/contrib/completion/git-completion.bash
>> @@ -2564,6 +2564,38 @@ _git_whatchanged ()
>>         _git_log
>>  }
>>
>> +_git_worktree ()
>> +{
>> +       local subcommands='add prune'
>> +       local subcommand="$(__git_find_on_cmdline "$subcommands")"
>> +       local c=2 n=0 refpos=2
>> +       if [ -z "$subcommand" ]; then
>> +               __gitcomp "$subcommands"
>> +       else
>> +               case "$subcommand,$cur" in
>> +               add,--*)
>> +                       __gitcomp "--force --detach"
>> +                       ;;
>> +               add,*)
>> +                       while [ $c -lt $cword ]; do
>> +                               case "${words[c]}" in
>> +                               --*) ;;
>> +                               -[bB]) ((refpos++)) ;;
>> +                               *) ((n++)) ;;
>> +                               esac
>> +                               ((c++))
>> +                       done
>> +                       if [ $n -eq $refpos ]; then
>> +                               __gitcomp_nl "$(__git_refs)"
>> +                       fi
>> +                       ;;
>> +               prune,--*)
>> +                       __gitcomp "--dry-run --verbose --expire"
>> +                       ;;
>> +               esac
>> +       fi
>> +}
>> +
>>  __git_main ()
>>  {
>>         local i c=1 command __git_dir
>> --
>> 2.5.0.rc3.407.g68aafd0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments
  2015-08-21 17:21   ` Junio C Hamano
@ 2015-08-21 20:49     ` SZEDER Gábor
  2015-08-21 20:50       ` [PoC PATCH] completion: support 'git worktree' SZEDER Gábor
  2015-08-21 21:59       ` [RFC/PATCH] contrib: teach completion about git-worktree options and arguments Eric Sunshine
  0 siblings, 2 replies; 6+ messages in thread
From: SZEDER Gábor @ 2015-08-21 20:49 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git List, Eric Sunshine


Quoting Junio C Hamano <gitster@pobox.com>:

> Eric Sunshine <sunshine@sunshineco.com> writes:
>
>> On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine  
>> <sunshine@sunshineco.com> wrote:
>>> Complete subcommands 'add' and 'prune', as well as their respective
>>> options --force, --detach, --dry-run, --verbose, and --expire. Also
>>> complete 'refname' in "git worktree add [-b <newbranch>] <path>
>>> <refname>".
>>
>> Ping[1]?
>>
>> [1]: http://article.gmane.org/gmane.comp.version-control.git/274526
>
> Ping indeed?
>

Yeah, right, sorry.  Non-subscribed occasional gmane-reader here amidst
job hunting, so thanks for the ping.  And the re-ping...

>>> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
>>> ---
>>>
>>> This is RFC since this is my first foray into the Git completion script,
>>> and there are likely better and more idiomatic ways to accomplish this.

Using __git_find_on_cmdline() to find subcommands and case
"$subcommand,$cur" to limit the number of nested case statements is as
idiomatic as you can get in the completion script.

And I hear you, that "<path> first, <refname> second" syntax makes it way
too complicated, especially since they can follow '-b <refname>'.
I wrote a completion function for 'git worktree' as well, turns out a week
or two before you posted this, but I never submitted it as it was way too
convoluted.  Anyway, will send it in reply to this, just for reference.

>>>
>>>  contrib/completion/git-completion.bash | 32  
>>> ++++++++++++++++++++++++++++++++
>>>  1 file changed, 32 insertions(+)
>>>
>>> diff --git a/contrib/completion/git-completion.bash  
>>> b/contrib/completion/git-completion.bash
>>> index c97c648..07c34ef 100644
>>> --- a/contrib/completion/git-completion.bash
>>> +++ b/contrib/completion/git-completion.bash
>>> @@ -2564,6 +2564,38 @@ _git_whatchanged ()
>>>         _git_log
>>>  }
>>>
>>> +_git_worktree ()
>>> +{
>>> +       local subcommands='add prune'
>>> +       local subcommand="$(__git_find_on_cmdline "$subcommands")"
>>> +       local c=2 n=0 refpos=2

A more descriptive variable name for 'n' would be great.

>>> +       if [ -z "$subcommand" ]; then
>>> +               __gitcomp "$subcommands"
>>> +       else
>>> +               case "$subcommand,$cur" in
>>> +               add,--*)
>>> +                       __gitcomp "--force --detach"

We usually don't offer '--force', because that option must be
handled with care.

>>> +                       ;;
>>> +               add,*)
>>> +                       while [ $c -lt $cword ]; do
>>> +                               case "${words[c]}" in
>>> +                               --*) ;;
>>> +                               -[bB]) ((refpos++)) ;;
>>> +                               *) ((n++)) ;;
>>> +                               esac
>>> +                               ((c++))
>>> +                       done
>>> +                       if [ $n -eq $refpos ]; then

I suppose here you wanted to calculate where (i.e. at which word on
the command line) we should offer refs and fall back to bash builtin
filename completion otherwise.  It works well in the common cases,
but:

   - it doesn't offer refs after -b or -B

   - it gets fooled by options to the git command, e.g. 'git
     --git-dir=.git worktree add <TAB>' offers refs instead of files,
     'git --git-dir=.git worktree add ../some/path <TAB>' offers
     refs, etc.

>>> +                               __gitcomp_nl "$(__git_refs)"
>>> +                       fi
>>> +                       ;;
>>> +               prune,--*)
>>> +                       __gitcomp "--dry-run --verbose --expire"
>>> +                       ;;
>>> +               esac
>>> +       fi
>>> +}
>>> +
>>>  __git_main ()
>>>  {
>>>         local i c=1 command __git_dir
>>> --
>>> 2.5.0.rc3.407.g68aafd0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PoC PATCH] completion: support 'git worktree'
  2015-08-21 20:49     ` SZEDER Gábor
@ 2015-08-21 20:50       ` SZEDER Gábor
  2015-08-21 21:59       ` [RFC/PATCH] contrib: teach completion about git-worktree options and arguments Eric Sunshine
  1 sibling, 0 replies; 6+ messages in thread
From: SZEDER Gábor @ 2015-08-21 20:50 UTC (permalink / raw)
  To: Junio C Hamano, Eric Sunshine; +Cc: git, SZEDER Gábor

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---

> I wrote a completion function for 'git worktree' as well, turns out a week
> or two before you posted this, but I never submitted it as it was way too
> convoluted.  Anyway, will send it in reply to this, just for reference.

And here it is.
From the number of indentation levels and comment lines you can see why
I haven't submitted this patch yet :)

OTOH it offers refs for -b and -B, and there are only fairly narrow
corner cases when 'git --options' can fool it (but that's a general
issue with __git_find_on_cmdline(), I wouldn't go into that).

 contrib/completion/git-completion.bash | 59 ++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index c97c648d7e..20a17e2c50 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2564,6 +2564,65 @@ _git_whatchanged ()
 	_git_log
 }
 
+_git_worktree ()
+{
+	local subcommand subcommand_idx sc c=1
+	local subcommands="add prune"
+
+	while [ $c -lt $cword ] && [ -z "$subcommand" ]; do
+		for sc in $subcommands; do
+			if [ "$sc" = "${words[c]}" ]; then
+				subcommand=$sc
+				subcommand_idx=$c
+				break
+			fi
+		done
+		((c++))
+	done
+
+	case "$subcommand,$cur" in
+	,*)
+		__gitcomp "$subcommands"
+		;;
+	add,--*)
+		__gitcomp "--detach"
+		;;
+	add,*)
+		case "$prev" in
+		-b|-B)
+			__gitcomp_nl "$(__git_refs)"
+			;;
+		-*)	# $prev is an option without argument: have to complete
+			# the path for the new worktree, fall back to bash
+			# filename completion
+			;;
+		*)	# $prev is not an option, so it must be either the
+			# 'add' subcommand, an argument of an option (e.g.
+			# branch for -b|-B), or the path for the new worktree
+			if [ $cword -eq $((subcommand_idx+1)) ]; then
+				# right after the 'add' subcommand, have to
+				# complete the path
+				:
+			else
+				case "${words[cword-2]}" in
+				-b|-B)	# after '-b <branch>', have to complete
+					# the path
+					;;
+				*)	# after the path, have to complete the
+					# branch to be checked out
+					__gitcomp_nl "$(__git_refs)"
+					;;
+				esac
+			fi
+			;;
+		esac
+		;;
+	prune,--*)
+		__gitcomp "--dry-run --verbose --expire"
+		;;
+	esac
+}
+
 __git_main ()
 {
 	local i c=1 command __git_dir
-- 
2.5.0.418.gdd37a9b

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments
  2015-08-21 20:49     ` SZEDER Gábor
  2015-08-21 20:50       ` [PoC PATCH] completion: support 'git worktree' SZEDER Gábor
@ 2015-08-21 21:59       ` Eric Sunshine
  1 sibling, 0 replies; 6+ messages in thread
From: Eric Sunshine @ 2015-08-21 21:59 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Junio C Hamano, Git List

On Fri, Aug 21, 2015 at 4:49 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
> Quoting Junio C Hamano <gitster@pobox.com>:
>> Eric Sunshine <sunshine@sunshineco.com> writes:
>>> On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine <sunshine@sunshineco.com>
>>> wrote:
>>>> Complete subcommands 'add' and 'prune', as well as their respective
>>>> options --force, --detach, --dry-run, --verbose, and --expire. Also
>>>> complete 'refname' in "git worktree add [-b <newbranch>] <path>
>>>> <refname>".
>>>
>>> Ping[1]?
>>
>> Ping indeed?
>
> Yeah, right, sorry.  Non-subscribed occasional gmane-reader here amidst
> job hunting, so thanks for the ping.  And the re-ping...

Thanks for the review.

>>>> +_git_worktree ()
>>>> +{
>>>> +       local subcommands='add prune'
>>>> +       local subcommand="$(__git_find_on_cmdline "$subcommands")"
>>>> +       local c=2 n=0 refpos=2
>
> A more descriptive variable name for 'n' would be great.

Indeed. I was planning on resubmitting with better variable names
(even if I didn't get any feedback)...

>>>> +       if [ -z "$subcommand" ]; then
>>>> +               __gitcomp "$subcommands"
>>>> +       else
>>>> +               case "$subcommand,$cur" in
>>>> +               add,--*)
>>>> +                       __gitcomp "--force --detach"
>
> We usually don't offer '--force', because that option must be
> handled with care.

As a person who never uses git-completion (or git-prompt), I wasn't
aware of that, so thanks for the heads-up. I only installed completion
(and prompt) when I decided to work on this (and I don't think I've
used tab-completion since then).

>>>> +                       ;;
>>>> +               add,*)
>>>> +                       while [ $c -lt $cword ]; do
>>>> +                               case "${words[c]}" in
>>>> +                               --*) ;;
>>>> +                               -[bB]) ((refpos++)) ;;
>>>> +                               *) ((n++)) ;;
>>>> +                               esac
>>>> +                               ((c++))
>>>> +                       done
>>>> +                       if [ $n -eq $refpos ]; then
>
> I suppose here you wanted to calculate where (i.e. at which word on
> the command line) we should offer refs and fall back to bash builtin
> filename completion otherwise.  It works well in the common cases,
> but:
>
>   - it doesn't offer refs after -b or -B

That was intentional since this is a new branch name, so it didn't
seem sensible to offer completion of existing refs. On the other hand,
it doesn't make much since to offer pathname completion either, but I
didn't see a better alternative. On reflection, I suppose ref
completion can make sense if the new branch name is going to be
similar to an existing one.

>   - it gets fooled by options to the git command, e.g. 'git
>     --git-dir=.git worktree add <TAB>' offers refs instead of files,
>     'git --git-dir=.git worktree add ../some/path <TAB>' offers
>     refs, etc.

This shortcoming could be addressed by computing relative to the
subcommand-index as your version does, correct?

I don't have enough background with the (git-specific) completion
facility to be able to judge if this patch and approach has merit and
ought to be pursued further, or if it would be better to drop in favor
of (some version of) your patch. I don't care strongly, and am fine
with dropping this patch if it's approach is suboptimal.

>>>> +                               __gitcomp_nl "$(__git_refs)"
>>>> +                       fi
>>>> +                       ;;
>>>> +               prune,--*)
>>>> +                       __gitcomp "--dry-run --verbose --expire"
>>>> +                       ;;
>>>> +               esac
>>>> +       fi
>>>> +}
>>>> +

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-21 21:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-23 20:49 [RFC/PATCH] contrib: teach completion about git-worktree options and arguments Eric Sunshine
2015-08-06 16:29 ` Eric Sunshine
2015-08-21 17:21   ` Junio C Hamano
2015-08-21 20:49     ` SZEDER Gábor
2015-08-21 20:50       ` [PoC PATCH] completion: support 'git worktree' SZEDER Gábor
2015-08-21 21:59       ` [RFC/PATCH] contrib: teach completion about git-worktree options and arguments Eric Sunshine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).