jreybert / vimagit

Ease your git workflow within Vim
1.31k stars 49 forks source link

Amend mode does not load the previous message when the edited file was opened from a path outide the repository. #200

Open bonderado opened 2 years ago

bonderado commented 2 years ago

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

jreybert commented 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.

jreybert commented 2 years ago

requires #202

bonderado commented 2 years ago

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.

v-slava commented 1 year ago

IMHO it is better to use git rev-parse --absolute-git-dir instead of git rev-parse --git-dir.