cohama / agit.vim

A powerful Git log viewer
234 stars 25 forks source link

fix "Agit: File not tracked: %" error in AgitFile #86

Closed syurazo closed 4 years ago

syurazo commented 4 years ago

If fugitive is available, s:get_git_root() returns path with .git removed from GitDir. That path is passed to the git -C option, but -C must specify the ROOT PATH where .git resides, not GitDir.

Use FugitiveWorkTree() instead of b:git_dir Fugitive's variable. FugitiveWorkTree() seems to return the intended GIT ROOT even in directories checked out by git worktree add

syurazo commented 4 years ago

original

not worktree

% cd /PATH/TO/agit.vim
agit.vim[master]%
agit.vim[master]% vim README.md -c 'echo b:git_dir'
  :
  :
"README.md" 42L, 847C
/PATH/TO/agit.vim/.git

when b:git_dir is "/PATH/TO/agit.vim/.git", s:get_git_root() returns "/PATH/TO/agit.vim" as expected

below the git clone repository

% cd /PATH/TO/agit.vim
agit.vim[master]%
agit.vim[master]% git worktree add .worktree/fix_agitfile_error fix_agitfile_error
agit.vim[master]% cd .worktree/fix_agitfile_error/
agit.vim[fix_agitfile_error]% vim README.md -c 'echo b:git_dir'
  :
  :
"README.md" 42L, 847C
/PATH/TO/agit.vim/.git/worktrees/fix_agitfile_error

when b:git_dir is "/PATH/TO/agit.vim/.git/worktrees/fix_agitfile_error", GITROOT expected "/PATH/TO/agit.vim/.wotktree/wfix_agitfile_error", but actually s:get_git_root() returns "/PATH/TO/agit.vim".

cohama commented 4 years ago

This error is caused by git worktree command, right?

syurazo commented 4 years ago

I should have consulted on Issues in advance...

This error is caused by git worktree command, right?

AgitFile will show an error only worktrees that have created by git worktree add.

The .git file of the worktree created by git worktree add is the same as the case where the working tree was separated using --separate-git-dir. #48

The .git file contains the PATH of the.git directory in the repository. The b:git_dir referenced when fugitive is enabled seems to be the same as gitdir: in the .git file. s:get_git_root() returns the PATH excluding .git.*

command b:git_dir matchstr(b:git_dir, '^.\+\ze\.git')
git clone ...agit.vim .../agit.vim/.git .../agit.vim/
git clone --separate-gir-dir <PATH> ...agit.vim PATH PATH/
git add wotktree <WTPATH> <BRANCH> .../agit.vim/.git/wotktree/BRANCH .../agit.vim/

agit#git#exec() is running git -C, so I expected to see the above <WTPATH>, but it actually points to the original repository.

tmp% git clone https://github.com/cohama/agit.vim.git
tmp% cd agit.vim/

agit.vim[master]% git branch feature
agit.vim[master]% git worktree add .feature feature
agit.vim[master]% cd .feature

.feature[feature]% cat .git
gitdir: /tmp/agit.vim/.git/worktrees/-feature

.feature[feature]% git -C /tmp/agit.vim/.git/worktrees/-feature ls-files /tmp/agit.vim/.feature/README.md    // NG
fatal: /tmp/agit.vim/.feature/README.md: '/tmp/agit.vim/.feature/README.md' is outside repository

.feature[feature]% git -C /tmp/agit.vim ls-files /tmp/agit.vim/.feature/README.md    // NG

.feature[feature]% git -C /tmp/agit.vim/.feature ls-files /tmp/agit.vim/.feature/README.md    // OK
README.md
cohama commented 4 years ago

Make sense. Thank you for your contribution!