简体   繁体   English

自定义 git“bang”别名的 Zsh 完成 - Git 分支名称

[英]Zsh completion for custom git "bang" alias - Git branch name

I have a Git alias update that I would like to outfit with branch-name completion.我有一个 Git 别名update ,我想为其配备分支名称完成功能。 The alias is defined like so:别名定义如下:

[alias]
        update = "!f() { git push . origin/$1:$1; }; f"

(It updates a local tracking branch with its upstream version, without having to check out the branch. Not really important to the specific question, though.) (它使用其上游版本更新本地跟踪分支,而无需检查该分支。不过,对于特定问题并不重要。)

I would like the command to tab-complete existing branch names for its $1 argument.我希望该命令为其$1参数使用制表符补全现有分支名称。 I know I can define a function called _git-update to control completion, but I'm missing some pieces to get it to work:我知道我可以定义一个名为_git-update的函数来控制完成,但是我缺少一些使其工作的部分:

_git-update ()
{
  ***some-function-here*** "$(__git_branch_names)"
}

I am using the completions installed on OS X by brew install zsh-completions , which is the set at https://github.com/zsh-users/zsh-completions .我正在使用通过brew install zsh-completions completions 安装在 OS X 上brew install zsh-completions ,这是在https://github.com/zsh-users/zsh-completions设置的。

(This question is directly analogous to https://stackoverflow.com/a/41307951/169947 , but for Zsh instead of Bash.) (这个问题直接类似于https://stackoverflow.com/a/41307951/169947 ,但针对 Zsh 而不是 Bash。)

May be a bit preemptive, but this is working:可能有点先发制人,但这是有效的:

# provides completion options similar to git branch/rebase/log
_complete_like_git_branch() {
  __gitcomp_nl_append "FETCH_HEAD"
  __gitcomp_nl_append "HEAD"
  __gitcomp_nl_append "ORIG_HEAD"
  __gitcomp_nl_append "$(__git_heads)"
  __gitcomp_nl_append "$(__git_remote_heads)"
  __gitcomp_nl_append "$(__git_tags)"
  __gitcomp_nl_append "$(__git_complete_refs)"
}

_git_rebase_chain() { _complete_like_git_branch }

# my git "bang" alias of git log
_git_lgk() { _complete_like_git_branch }

reference: contrib/completion/git-completion.bash参考: contrib/completion/git-completion.bash

Possible improvements:可能的改进:

  • Is the above canonically correct?以上是规范正确的吗? ie Using global shell functions in ~/.zshrc ?即在~/.zshrc使用全局 shell 函数?
  • Choices are super-similar to git rebase and git log, but are they the same ?选择与 git rebase 和 git log 非常相似,但它们相同吗?

i use this function to append the working branch to my PS1:我使用此函数将工作分支附加到我的 PS1:

    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM