Open bonderado opened 2 years ago
Thanks for such a complete report and for the PR.
I am currently reviving the project, more specifically the CI, which has not been run/updated since github abandoned travis. I plan to finish this next week.
Your issue seems to be a valid case to be fixed, but it touches fundamental vimagit operations, so I prefer to ensure your fix passes the testsuite.
requires #202
In fact, I was a bit surprised at not getting a test-suite result: by all means, make sure I did not break anything.
Thank you for your work on this marvelous plugin.
IMHO it is better to use git rev-parse --absolute-git-dir
instead of git rev-parse --git-dir
.
Short description
When (neo)vim is used to open a tracked file from outside the git repository (e.g.: one directory above), vimagit manages to perform normal commits ('CC') correctly, however, when trying to amend a commit ('CA'), the previous commit message is not loaded.
Steps to reproduce
To reproduce the issue:
1) Setup a test repo typing the following commands: $ cd ~ $ mkdir gitest $ cd gitest $ git init $ mkdir sub $ touch readme.txt $ git commit -m 'initial commit'
2) get outside the repository and open "readme.txt" $ cd .. $ nvim readme.txt
3) from within (neo)vim: :Magit CA
4) Observe the issue
Expected result: vimagit switches to amend mode and the previous commit message is shown in the 'Commit message' section.
Actual result: vimagit switches to amend mode, but no text appears in the 'Commit message' section.
The cause seems to be that vimagit executes: $ git rev-parse --git-dir in the edited file directory which in this case is the top level of the repository, where the command returns a relative path to the .git directory, that is then stored in "b:magit_git_dir". Later, when vimagit tries to open the file '.git/COMMIT_EDITMSG', the relative path will point to a non-existing file, since the editor's CWD is "gitest/.." instead of "gitest".
NOTE: opening the file from the top level dir of the repository as in: $ cd $HOME/gitest $ nvim readme.txt would work.
NOTE: runnig the command: $ git rev-parse --git-dir in a subfolder of the repository: $ mkdir sub $ cd sub $ git rev-parse --git-dir would result in an absolute path.
Solution:
Since vimagit invokes git rev-parse --git-dir from the path of the edited file, using fnamemodify(git_dir, ':p') would solve the issue, since we are either in the top dir and the relative path is correctly turned to an absolute one, or we are in a subdir and the path is already absolute.
Environment
neovim version: NVIM v0.7.2 Build type: Release LuaJIT 2.1.0-beta3 Compiled by team+vim@tracker.debian.org
git version 2.35.1
current vimagit: 88720f45b8201718b101a17d72d2fef5ede4ce2e
OS: Linux 5.18.0-2-amd64 # 1 SMP PREEMPT_DYNAMIC Debian 5.18.5-1 (2022-06-16) x86_64 GNU/Linux
both terminal nvim and nvim-qt are affected