Zsh gitファイル名補完 "--git-dir=... --work-tree=...": gitリポジトリではありません。

Zsh gitファイル名補完

以下を使用してドットファイルを追跡します。

  • 裸の店はにあります$HOME/repos/dotfiles
  • 私のすべてのドットファイルは通常の場所にあります$HOME/.vim/vimrc$HOME/repos/dotfiles/vimrc
  • 私はgit --git-dir=$HOME/repos/dotfiles --work-tree=$HOME ...仕事を管理するために走っています。

(実際に.g()にあるとき、上記のコマンドに拡張される機能があり$HOME、そうでなければgit。)

それ以外は大丈夫です...

質問:Zsh gitファイル名の補完が機能しません。

例:

% pwd
/home/brian
% g status                                                                                                                                         ~
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .vim/vimrc
        modified:   .xmonad/xmonad.hs

no changes added to commit (use "git add" and/or "git commit -a")
% g add <Tab>
Completing not a git repository

(「完了中...」の内容が終了しましたzstyle ':completion:*' format $'%{\e[0;31m%}Completing %B%d%b%{\e[0m%}'。)

zshのgit完了に与えられた "move to / follow"値をあたかもそのディレクトリから呼び出されたかのよう--work-treeに知らせるだけでは不十分であることは注目に値しますgit。そうしても明示的に動作しないからです。

% cd repos/dotfiles
% g status
fatal: This operation must be run in a work tree
% g add <Tab>
Completing not a git repository

質問:zshのgitの完成をこの状況に拡張する簡単な方法はありますか?

ベストアンサー1

zsh残念ながら、これはgit完了プロセスのバグです。 "zsh"メーリングリストで議論を見つけることができます。ここ

Daniel Shahafは「_git」のパッチを提供しました。

diff --git a/Completion/Unix/Command/_git b/Completion/Unix/Command/_git
index 518e6d198..45a0fa622 100644
--- a/Completion/Unix/Command/_git
+++ b/Completion/Unix/Command/_git
@@ -6609,20 +6609,33 @@ __git_files_relative () {
 (( $+functions[__git_files] )) ||
 __git_files () {
   local compadd_opts opts tag description gitcdup gitprefix files expl
+  local pref

   zparseopts -D -E -a compadd_opts V: J: 1 2 n f X: M: P: S: r: R: q F:
   zparseopts -D -E -a opts -- -cached -deleted -modified -others -ignored -unmerged -killed x+: --exclude+:
   tag=$1 description=$2; shift 2

-  gitcdup=$(_call_program gitcdup git rev-parse --show-cdup 2>/dev/null)
-  __git_command_successful $pipestatus || return 1
+  case $(_call_program gitinworktree git rev-parse --is-inside-work-tree 2>/dev/null) in
+    (true)
+      gitcdup=$(_call_program gitcdup git rev-parse --show-cdup 2>/dev/null)
+      __git_command_successful $pipestatus || return 1

-  gitprefix=$(_call_program gitprefix git rev-parse --show-prefix 2>/dev/null)
-  __git_command_successful $pipestatus || return 1
+      gitprefix=$(_call_program gitprefix git rev-parse --show-prefix 2>/dev/null)
+      __git_command_successful $pipestatus || return 1
+
+      local pref=$gitcdup$gitprefix$PREFIX
+      ;;
+    (false)
+      local pref=
+      ;;
+    (*)
+      # XXX what to do?
+      return 1
+      ;;
+  esac

   # TODO: --directory should probably be added to $opts when --others is given.

-  local pref=$gitcdup$gitprefix$PREFIX

   # First allow ls-files to pattern-match in case of remote repository
   files=(${(0)"$(_call_program files git ls-files -z --exclude-standard ${(q)opts} -- ${(q)${pref:+$pref\\\*}} 2>/dev/null)"})
@@ -7585,7 +7598,8 @@ _git() {
         ;;
       (option-or-argument)
         curcontext=${curcontext%:*:*}:git-$words[1]:
-       (( $+opt_args[--git-dir] )) && local -x GIT_DIR=$opt_args[--git-dir]
+        (( $+opt_args[--git-dir] )) && local -x GIT_DIR=${(e)opt_args[--git-dir]}
+        (( $+opt_args[--work-tree] )) && local -x GIT_WORK_TREE=${(e)opt_args[--work-tree]}
        if ! _call_function ret _git-$words[1]; then
          if zstyle -T :completion:$curcontext: use-fallback; then
            _default && ret=0

それは明らかに動作しますzsh 5.4.1が、私には動作しません、YMMV。

問題が解決したら、この回答を更新します。

編集する:

上記のパッチを使用すると機能しますが、どこに配置するかがadd重要です。パッチは最後にする必要があります。

git --git-dir=$HOME/.dotfiles --work-tree=$HOME/ add

もう一つ注目すべき点は、速度が少し遅いことです。

おすすめ記事