Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bonderado opened this issue Aug 1, 2022 · 4 comments

Comments

@bonderado
Copy link

bonderado commented Aug 1, 2022

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: 88720f4

  • 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

@jreybert
Copy link
Owner

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
Copy link
Owner

requires #202

@bonderado
Copy link
Author

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
Copy link

v-slava commented Jan 24, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants