Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: 'cd' completion for git-worktree(1) - how to '_wanted $tag' defaulting to false?



On Mon, Sep 10, 2018 at 6:49 PM, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> Is it possible for «cd <TAB>» to offer git worktrees, when cwd is a git repo?
>
> Completing git worktrees is easy:
> .
>         # excerpted from _git
>         _wanted directories expl 'working tree' compadd -S ' ' -f -M 'r:|/=* r:|=*' \
>             ${${(M)${(f)"$(_call_program directories git worktree list --porcelain)"}:#worktree*}#* } && ret=0
>         .
>         # independent reimplementation
>         __git_worktree_directories() {
>           local -a expl targets=( ${${(f)"$(git worktree list)"}%% *} ) descs=( ${(f)"$(git worktree list)"} );
>           _wanted git-worktree-directories expl 'git worktree directories' compadd -ld descs -a targets
>         }
>
> I'm not sure how to hook that into _cd, though.  Checking whether we're
> inside a git repository requires disk accesses and/or exec'ing
> git-rev-parse(1), either of which may be expensive.  I suppose this
> needs to be opt-in behaviour then, but I'm not sure how to ask "Did the
> user opt-in to tag $foo?"; _requested assumes all tags are wanted except
> those the user has explicitly opted out of.

I would go with a separate function for this, like git-cd or
something. This would then also make the completer part trivial :).
Another option is to have another keybind than tab to offer git
worktrees (not just for cd). Or you can use the ~[] mechanism.

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author