neogit icon indicating copy to clipboard operation
neogit copied to clipboard

regression in `:Neogit cwd=<cwd>`

Open swarn opened this issue 1 year ago • 5 comments

Description

Starting with commit bcb4b3ea, when running :Neogit cwd=... in a directory that is not under version control, but with cwd being directory that is under version control, various errors with reported warnings

Tue Jun 25 15:28:27 2024 Notifications | WARN
git stash list:

    fatal: not a git repository (or any of the parent directories): .git

An error occurred.
Tue Jun 25 15:28:27 2024 Notifications | WARN
git status --porcelain=2 -b:

    fatal: not a git repository (or any of the parent directories): .git
...

Git bisect leads me to the commit above, though I the errors I get on that commit are different than the errors I get at HEAD (currently d27a3600).

Neovim version

NVIM v0.11.0-dev-310+g9e436251d Build type: RelWithDebInfo LuaJIT 2.1.1716656478

Operating system and version

macOS 14.4.1

Steps to reproduce

mkdir temp
touch temp/foo.txt
git init temp
nvim -u minimal.lua temp/foo.txt
:Neogit cwd=%:p:h

Expected behavior

No response

Actual behavior

Errors described above.

Minimal config

-- NOTE: See the end of this file if you are reporting an issue, etc. Ignore all the "scary" functions up top, those are
-- used for setup and other operations.
local M = {}

local base_root_path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h") .. "/.min"
function M.root(path)
  return base_root_path .. "/" .. (path or "")
end

function M.load_plugin(plugin_name, plugin_url)
  local package_root = M.root("plugins/")
  local install_destination = package_root .. plugin_name
  vim.opt.runtimepath:append(install_destination)

  if not vim.loop.fs_stat(package_root) then
    vim.fn.mkdir(package_root, "p")
  end

  if not vim.loop.fs_stat(install_destination) then
    print(string.format("> Downloading plugin '%s' to '%s'", plugin_name, install_destination))
    vim.fn.system({
      "git",
      "clone",
      "--depth=1",
      plugin_url,
      install_destination,
    })
    if vim.v.shell_error > 0 then
      error(string.format("> Failed to clone plugin: '%s' in '%s'!", plugin_name, install_destination),
        vim.log.levels.ERROR)
    end
  end
end

---@alias PluginName string The plugin name, will be used as part of the git clone destination
---@alias PluginUrl string The git url at which a plugin is located, can be a path. See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols for details
---@alias MinPlugins table<PluginName, PluginUrl>

---Do the initial setup. Downloads plugins, ensures the minimal init does not pollute the filesystem by keeping
---everything self contained to the CWD of the minimal init file. Run prior to running tests, reproducing issues, etc.
---@param plugins? table<PluginName, PluginUrl>
function M.setup(plugins)
  vim.opt.packpath = {}                      -- Empty the package path so we use only the plugins specified
  vim.opt.runtimepath:append(M.root(".min")) -- Ensure the runtime detects the root min dir

  -- Install required plugins
  if plugins ~= nil then
    for plugin_name, plugin_url in pairs(plugins) do
      M.load_plugin(plugin_name, plugin_url)
    end
  end

  vim.env.XDG_CONFIG_HOME = M.root("xdg/config")
  vim.env.XDG_DATA_HOME = M.root("xdg/data")
  vim.env.XDG_STATE_HOME = M.root("xdg/state")
  vim.env.XDG_CACHE_HOME = M.root("xdg/cache")

  -- NOTE: Cleanup the xdg cache on exit so new runs of the minimal init doesn't share any previous state, e.g. shada
  vim.api.nvim_create_autocmd("VimLeave", {
    callback = function()
      vim.fn.system({
        "rm",
        "-r",
        "-f",
        M.root("xdg")
      })
    end
  })
end

-- NOTE: If you have additional plugins you need to install to reproduce your issue, include them in the plugins
-- table within the setup call below.
M.setup({
  plenary = "https://github.com/nvim-lua/plenary.nvim.git",
  telescope = "https://github.com/nvim-telescope/telescope.nvim",
  diffview = "https://github.com/sindrets/diffview.nvim",
  neogit = "https://github.com/NeogitOrg/neogit"
})
-- WARN: Do all plugin setup, test runs, reproductions, etc. AFTER calling setup with a list of plugins!
-- Basically, do all that stuff AFTER this line.
require("neogit").setup({}) -- For instance, setup Neogit

swarn avatar Jun 25 '24 20:06 swarn

I believe the issue also happens when running :Neogit cwd=%:p:h, for checking the status on the current file!

Qkessler avatar Jul 10 '24 15:07 Qkessler

I can confirm this is happenning to me too, it is limiting the usability of the whole thing, since very rarely do I edit files in the root of the git repo. It's sad to see it was reported 2 weeks ago.

svatem1 avatar Jul 11 '24 09:07 svatem1

We should be mindful that there's a lot of issues created and removing noise takes time from maintainers! Will CC @CKolkey for triage!

Qkessler avatar Jul 11 '24 10:07 Qkessler

I've mentioned this in a few other issues, but I don't mind saying it again: I have next to no time for this project right now, as my wife is about 5 months pregnant with kids #2 + #3.

If anyone feels like taking a swing at this issue, I will try to review it in a timely manner or offer advice, but otherwise y'all will just have to be patient :)

CKolkey avatar Jul 11 '24 12:07 CKolkey

Makes sense. Definitely take the time and enjoy the time with the wife! Congrats both!

If you have some spare minutes, do you have some thoughts for the next person to potentially pick this up? Do you recommend a rollback, do you have any specific pointers or tribal knowledge to solve the issue?

Thanks!

Qkessler avatar Jul 11 '24 13:07 Qkessler

I found the issue. It is at lua/neogit/lib/git/repository.lua:185, and the bad commit is 5b6788590, not bcb4b3e. Look at this snippet:

function Repo.instance(dir)
  dir = dir or vim.uv.cwd()
  assert(dir, "cannot create a repo without a cwd")

  local cwd = vim.fs.normalize(dir)
  if not instances[cwd] then
    logger.debug("[REPO]: Registered Repository for: " .. cwd)
    instances[cwd] = Repo.new(cwd)
    instances[cwd]:dispatch_refresh()
  end

  return instances[cwd]
end

If dir arguments is passed (and it is passed when you open neogit), it creates repository instance at path that you passed. But when no argument passed, it tries to use vim.uv.cmd as path, i.e. your current working directory, not directory that you actually specified.

Since I am not one of developers of neogit I don't really know how to fix it without breaking everything else :sweat_smile:, but I will try to poke around and see if I will come up with something

Strongleong avatar Sep 27 '24 01:09 Strongleong

@CKolkey I made a quick fix of this issue. Can you please check it out? I relly depend on cwd argument in my work and this issue drives me nuts

Strongleong avatar Sep 27 '24 04:09 Strongleong

fwiw, I tested this branch and happy to report that this worked on my machine. Thanks @Strongleong!

0xjairo avatar Sep 27 '24 15:09 0xjairo