Skip to content

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

Description

@bonderado

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 gitest/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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions