使用 zsh 完成 git:带空格的文件名没有被正确转义

cbo*_*wns 22 zsh git autocomplete

Git完成:

我的系统上的 git 文件名自动完成有困难。我在 OS X (10.9.3) 上使用zsh(5.0.5) 和git(1.9.3)。双方zshgit已通过自制软件已经安装完毕。(完整版输出在帖子底部。)

git的文件名补全没有像我期望的那样插入空格。当我输入名称中带有空格的文件名时,shell 会插入文件名而不转义空格。zsh的内置完成不会这样做,但git's 做了。

这是我所看到的一个例子。

我有一个存储库,其中包含一些名称中带有空格的文件。

% ls -la
test
test four - latest.txt
test three.txt
test two
Run Code Online (Sandbox Code Playgroud)

当我使用制表符完成插入文件名时,shell 反斜杠会按预期对文件名进行转义。

% echo "testing" >> test<tab>
Run Code Online (Sandbox Code Playgroud)

点击标签三下后自动完成。

% echo "testing" >> test\ four\ -\ latest.txt
––– file
test                       test\ four\ -\ latest.txt  test\ three.txt            test\ two                
Run Code Online (Sandbox Code Playgroud)

git status 用引号显示这些文件名(它完全理解发生了什么):

% git status --short
 M test
 M "test four - latest.txt"
 M "test three.txt"
 M "test two"
Run Code Online (Sandbox Code Playgroud)

但是当我尝试git add使用选项卡自动完成功能时,它会横向移动。

% git add test<tab>
Run Code Online (Sandbox Code Playgroud)

点击标签三下后导致此结果:

% git add test four - latest.txt
test                    test four - latest.txt  test three.txt          test two
Run Code Online (Sandbox Code Playgroud)

我试过一点点回归:我的 dotfiles 处于版本控制中,所以我从一年前开始尝试zsh 4.3.15, git 1.8.3, 和我的 dotfiles ,当时我几乎可以肯定这是有效的。奇怪的是,这个设置仍然被破坏了。

已将其范围缩小到来自以下位置的_git完成文件/usr/local/share/zsh/site-functions

% echo $FPATH
/usr/local/share/zsh/site-functions:/usr/local/Cellar/zsh/5.0.5/share/zsh/functions
% ls -l /usr/local/share/zsh/site-functions
_git@ -> ../../../Cellar/git/1.9.3/share/zsh/site-functions/_git
_hg@ -> ../../../Cellar/mercurial/3.0/share/zsh/site-functions/_hg
_j@ -> ../../../Cellar/autojump/21.7.1/share/zsh/site-functions/_j
git-completion.bash@ -> ../../../Cellar/git/1.9.3/share/zsh/site-functions/git-completion.bash
go@ -> ../../../Cellar/go/HEAD/share/zsh/site-functions/go
Run Code Online (Sandbox Code Playgroud)

如果我$FPATH.zshrc运行之前手动更改compinit(或简单地删除/usr/local/share/zsh/site-functions/_git符号链接),则完成回退zsh并按预期工作。

zsh完成无_git

% git add test<tab>
Run Code Online (Sandbox Code Playgroud)

按三下选项卡会产生正确的结果:

% git add test\ four\ -\ latest.txt
––– modified file
test                       test\ four\ -\ latest.txt  test\ three.txt            test\ two                
Run Code Online (Sandbox Code Playgroud)

旁注:我尝试删除git-completion.bash链接,但它完全破坏了事情:

% git add test<tab>
Run Code Online (Sandbox Code Playgroud)

产生这种破坏性:

% git add test__git_zsh_bash_func:9: command not found: __git_aliased_command
    git add test
––– file
test                       test\ four\ -\ latest.txt  test\ three.txt            test\ two                
Run Code Online (Sandbox Code Playgroud)

真的想让它正常工作:其余的_git完成很棒,因为它们比zsh那些更能感知回购,但我需要带空格或其他特殊字符的文件名才能正确转义。


软件版本:

% zsh --version
zsh 5.0.5 (x86_64-apple-darwin13.0.0)

% git --version
git version 1.9.3

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.9.3
BuildVersion:   13D65
Run Code Online (Sandbox Code Playgroud)

我已经上传了_gitgit-completion.bash文件:git-completion.bash_git(重命名为_git.shCloudApp 将使其在浏览器中可见。)

小智 5

邮件列表中提到了这个错误。

修复方法是编辑文件git-completion.zsh-Qcompadd, in 中删除选项__gitcomp_file

--- i/contrib/completion/git-completion.zsh
+++ w/contrib/completion/git-completion.zsh
@@ -90,7 +90,7 @@ __gitcomp_file ()

    local IFS=$'\n'
    compset -P '*[=:]'
-   compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
+   compadd -p "${2-}" -f -- ${=1} && _ret=0
 }

 __git_zsh_bash_func ()
Run Code Online (Sandbox Code Playgroud)

此文件是从contrib/completion目录安装的,其路径可能因您的包管理器而异。如果你在 macOS 上安装自制软件,它位于/usr/local/Cellar/git/2.10.2/share/zsh/site-functions.