如何在没有来源的Git上使用zsh制表符补全? [英] How to use zsh tab completion on Git without origin?

查看:148
本文介绍了如何在没有来源的Git上使用zsh制表符补全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 zsh Git .

I am using zsh with oh-my-zsh on Ubuntu 18.04.2. Currently, Git is installed at version 2.20.1.

每当我想从远程(origin)checkout本地分支时,我都会尝试在shell中使用制表符补全,如下所示:

Whenever I want to checkout a local branch from a remote (origin) I try to use the tab completion in the shell as follows:

git checkout fea<TAB>

选项卡完成结果为:

git checkout origin/feature

从制表符完成中我真正期望得到的是:

What I actually expect from the tab completion is:

git checkout feature

如何配置制表符完成以正确(?)完成远程分支-还是缺少某些内容?我的 dotfiles 是公开的.

How can I configure the tab completion to correctly (?) complete the remote branch - or am I missing something? My dotfiles are public.

回复Tarun Lalwani的评论:git branch -a的输出是:

Reply to the comment from Tarun Lalwani: The output of git branch -a is:

开发
*家政服务
遥控器/起源/HEAD->起源/开发
遥控器/原点/开发
遥控器/原点/主机
遥控器/原点/释放
遥控器/产地/功能

develop
* housekeeping
remotes/origin/HEAD -> origin/develop
remotes/origin/develop
remotes/origin/master
remotes/origin/release
remotes/origin/feature

推荐答案

经过深入研究,结果发现 oh-my-zsh 不能满足ZSH下git checkout的完成,但是从Shell设施随附的_git函数中获取.

After thorough research, it turns out that completion of git checkout under ZSH is not fulfilled by oh-my-zsh, but from the _git function provided with the shell facilities.

如评论中所述,我无法重现您遇到的问题.一切似乎都按预期进行.不过...

As stated in the comments, I couldn't reproduce the issue you experienced. Everything seems to work as expected. Nevertheless…

签出以下文件:

/usr/share/zsh/<5.x>/functions/_git

我的本​​地 zsh 版本是 5.2 .在 450 行附近,您可以看到:

My local zsh version is 5.2. Around row 450, you can see:

  case $state in
    (branch-or-tree-ish-or-file)
      # TODO: Something about *:: brings us here when we complete at "-".  I
      # guess that this makes sense in a way, as we might want to treat it as
      # an argument, but I can't find anything in the documentation about this
      # behavior.
      [[ $line[CURRENT] = -* ]] && return
      if (( CURRENT == 1 )) && [[ -z $opt_args[(I)--] ]]; then
        # TODO: Allow A...B
        local branch_arg='' \
              remote_branch_noprefix_arg='remote branches::__git_remote_branch_names_noprefix' \
              tree_ish_arg='tree-ishs::__git_tree_ishs' \
              file_arg='modified-files::__git_modified_files'

        if [[ -n ${opt_args[(I)-b|-B|--orphan|--detach]} ]]; then
          remote_branch_noprefix_arg=
          file_arg=
        elif [[ -n $opt_args[(I)--track] ]]; then
          branch_arg='remote-branches::__git_remote_branch_names'
          remote_branch_noprefix_arg=
          tree_ish_arg=
          file_arg=
        elif [[ -n ${opt_args[(I)--ours|--theirs|-m|--conflict|--patch]} ]]; then
          remote_branch_noprefix_arg=
        fi

        _alternative \
          $branch_arg \
          $remote_branch_noprefix_arg \
          $tree_ish_arg \
          $file_arg && ret=0

删除传递给_alternative的数组之一会更改在git checkout之后的分支名称时向您建议的内容.特别是,删除$remote_branch_noprefix_arg会带回以origin为前缀的远程分支名称或它们各自的远程存储库名称.

Removing one of the arrays passed to _alternative changes what's suggested to you when completing a branch name after git checkout. In particular, removing $remote_branch_noprefix_arg brings back remote branch names prefixed with origin or their respective remote repository name.

因此,升级您的Shell或降级到以前的版本可能是个好主意.

Therefore, upgrading your shell or downgrading to a former version may be a good idea.

一些细节呢:

  • 我看到您已经编辑了原始帖子,并且git push有充分的理由使工作与git checkout略有不同;
  • 远程分支的同系物本地分支可能不强制存在:featureorigin/feature不同,即使前者存在时通常被配置为跟踪后者;
  • 默认情况下,如果本地分支尚不存在,并且当未传递任何显式选项时,签出feature将创建一个配置为跟踪远程分支的同名本地分支,然后切换到该分支,同时签出origin/feature将使您进入分离模式,从而可以浏览此远程分支,但直接在其顶部进行提交.
  • I saw you've edited your original post and git push have good reasons to work slightly differently than git checkout ;
  • The homolog local branch of a remote one may not forcibly exists: feature is different from origin/feature, even when the former one, when it exists, is generally configured to track the latter ;
  • By default, if the local branch doesn't exist yet and when no explicit options are passed aside, checking out feature will create an eponym local branch configured to track the remote one, then switch to it, while checking out origin/feature will put you in detached mode, allowing to browse this remote branch but directly commit on top of it.

这篇关于如何在没有来源的Git上使用zsh制表符补全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆