Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments
- From
SZEDER Gábor <szeder@ira.uka.de>
- Date
- Aug 21, 2015, 20:49 UTC
- Message-ID
- <20150821224918.Horde.edB9u314lsP17FLUzwFsQA1@webmail.informatik.kit.edu>
- In-Reply-To
- <xmqq37zc7e2o.fsf@gitster.dls.corp.google.com>
Quoting Junio C Hamano <gitster@pobox.com>:
Show 15 quoted lines
> 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...
Show 5 quoted lines
>>> 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.
Show 19 quoted lines
>>>
>>> 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=2A more descriptive variable name for 'n' would be great.
Show 6 quoted lines
>>> + 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.
Show 11 quoted lines
>>> + ;;
>>> + add,*)
>>> + while [ $c -lt $cword ]; do
>>> + case "${words[c]}" in
>>> + --*) ;;
>>> + -[bB]) ((refpos++)) ;;
>>> + *) ((n++)) ;;
>>> + esac
>>> + ((c++))
>>> + done
>>> + if [ $n -eq $refpos ]; thenI 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.Show 15 quoted lines
>>> + __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