简体   繁体   English

如何将自定义git命令添加到zsh完成?

[英]How to add custom git command to zsh completion?

I've read a few guides on zsh completion, but I am still confused. 我已经阅读了一些关于zsh完成的指南,但我仍然感到困惑。 In our development environment we have a custom Git command called git new-branch . 在我们的开发环境中,我们有一个名为git new-branch的自定义Git命令。 I'd like zsh to auto-complete it for me after typing just git ne and a Tab . 在输入git neTab之后,我希望zsh为我自动完成它。 How can I do that? 我怎样才能做到这一点?

The default git completion is extendible : 默认的git完成是可扩展的

Say you got your own git sub-commands (git will run a program `git-foo' when you run "git foo") and you want "git f" to complete that sub commands name for you. 假设您有自己的git子命令(当您运行“git foo”时,git将运行程序`git-foo')并且您希望“git f”为您完成该子命令名称。 You can make that sub-command known to the completion via the user-command style: 您可以通过用户命令样式使完成时知道该子命令:

 % zstyle ':completion:*:*:git:*' user-commands foo:'description for foo' 

`user-commands' is a list style, so you can add any number of programs there. `user-commands'是一种列表样式,因此您可以在其中添加任意数量的程序。 The :description part is optional, so you could add all git-* programs from your $path like this: :description部分是可选的,所以你可以像这样添加$ path中的所有git- *程序:

 % zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git-*}/git-/} 

That is, it suffices to add 也就是说,添加就足够了

zstyle ':completion:*:*:git:*' user-commands new-branch:'custom new branch function'

to your zshrc . 到你的zshrc

If you would like to handle parameters to your custom command as well, then it is a better solution to use a custom compdef file. 如果您还希望处理自定义命令的参数,那么使用自定义compdef文件是一个更好的解决方案。 The file referenced above has some details on that as well: Just create a standard definition file defining a git-<yourcommand> function, the default git completion will use it automatically when needed. 上面引用的文件也有一些细节:只需创建一个定义git-<yourcommand>函数的标准定义文件,默认git完成将在需要时自动使用它。

With Git 2.18 (q2 2018), you have a new possibility, which applies not just for zsh: The command line completion mechanism (in contrib/ ) has learned to load a custom completion file for " git $command " where $command is a custom " git-$command " that the end user has on the $PATH when using newer version of bash. 使用Git 2.18(q2 2018),你有了一个新的可能性,它不仅适用于zsh:命令行完成机制(在contrib/ )学会了加载“ git $command ”的自定义完成文件,其中$command是一个自定义“ git-$command ”,当使用更新版本的bash时,最终用户在$ PATH上。

See commit 085e2ee (29 Apr 2018) by Florian Gamböck ( FloGa ) . FlorianGamböck( FloGa 提交085e2ee (2018年4月29日
(Merged by Junio C Hamano -- gitster -- in commit fb3a0ca , 23 May 2018) (由Junio C gitster合并- gitster -提交fb3a0ca ,2018年5月23日)

completion: load completion file for external subcommand 完成:外部子命令的加载完成文件

Adding external subcommands to Git is as easy as to put an executable file git-foo into PATH . 将外部子命令添加到Git就像将可执行文件git-foo放入PATH
Packaging such subcommands for a Linux distribution can be achieved by unpacking the executable into /usr/bin of the user's system. 可以通过将可执行文件解压缩到用户系统的/usr/bin来实现打包Linux发行版的子命令。
Adding system-wide completion scripts for new subcommands, however, can be a bit tricky. 但是,为新的子命令添加系统范围的完成脚本可能有点棘手。

Since bash-completion started to use dynamical loading of completion scripts since v1.90 (preview of v2.0), it is no longer sufficient to drop a completion script of a subcommand into the standard completions path, /usr/share/bash-completion/completions , since this script will not be loaded if called as a git subcommand . 由于bash-completion从v1.90(v2.0的预览版)开始使用动态加载完成脚本,因此将子命令的完成脚本放入标准完成路径/usr/share/bash-completion/completions ,因为如果调用git subcommand则不会加载此脚本。

For example, look at https://bugs.gentoo.org/544722 . 例如,请查看https://bugs.gentoo.org/544722
To give a short summary: The popular git-flow subcommand provides a completion script, which gets installed as /usr/share/bash-completion/completions/git-flow . 简要总结:流行的git-flow subcommand提供了一个完成脚本,它安装为/usr/share/bash-completion/completions/git-flow

If you now type into a Bash shell: 如果您现在键入Bash shell:

 git flow <TAB> 

You will not get any completions, because bash-completion only loads completions for git and git has no idea that git-flow is defined in another file. 你不会得到任何完成,因为bash-completion只加载git的完成,而git不知道git-flow是在另一个文件中定义的。
You have to load this script manually or trigger the dynamic loader with: 您必须手动加载此脚本或触发动态加载程序:

 `git-flow <TAB>` # Please notice the dash instead of whitespace 

This will not complete anything either, because it only defines a Bash function, without generating completions. 这也不会完成任何事情,因为它只定义了Bash函数,而没有生成完成。
But now the correct completion script has been loaded and the first command can use the completions. 但是现在加载了正确的完成脚本,第一个命令可以使用完成。

So, the goal is now to teach the git completion script to consider the possibility of external completion scripts for subcommands , but of course without breaking current workflows. 因此, 现在的目标是教git完成脚本以考虑子命令的外部完成脚本的可能性 ,但当然不会破坏当前的工作流程。

How? 怎么样? This is what Git 2.18 proposes: 这就是Git 2.18提出的建议:

I think the easiest method is to use a function that was defined by bash-completion v1.90, namely _completion_loader . 我认为最简单的方法是使用由bash-completion v1.90定义的函数,即_completion_loader
It will take care of loading the correct script if present. 如果存在,它将负责加载正确的脚本。
Afterwards, the git completion script behaves as usual. 之后,git完成脚本会像往常一样运行。

_completion_loader was introduced in commit 20c05b43 of scop/bash-completion (the programmable completion functions for bash) back in 2011, so it should be available in even older LTS distributions. _completion_loader是在2011年scop/bash-completionscop/bash-completion的可编程完成函数)的提交20c05b43中引入的,所以它应该在更老的LTS发行版中可用。
This function searches for external completion scripts not only in the default path /usr/share/bash-completion/completions , but also in the user's home directory via $XDG_DATA_HOME and in a user specified directory via $BASH_COMPLETION_USER_DIR . 该功能将搜索外部完成脚本,不仅在默认路径/usr/share/bash-completion/completions ,而且在通过用户的主目录$XDG_DATA_HOME并通过用户指定的目录$BASH_COMPLETION_USER_DIR


univerio adds in the comments : univerio 在评论中添加:

It turns out that there are two different completion functions: 事实证明,有两种不同的完成功能:

univerio adds: univerio补充道:

  • The zsh-provided function is the default on Debian (and Ubuntu, Mint, etc), and zsh提供的函数是Debian(以及Ubuntu,Mint等)的默认函数
  • the git-provided function is the default on homebrew-installed git on macOS. git-provided函数是macOS上homebrew-installed git的默认函数。

Super confusing. 超级混乱。 Not sure which one is better. 不确定哪一个更好。

This particular answer works only for the git-provided function, while the accepted answer works only for the zsh-provided function. 这个特殊的答案仅适用于git提供的函数,而接受的答案仅适用于zsh提供的函数。

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

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