stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
3.4k stars 92 forks source link

bug: Error when using `git mv` on a directory without any git tracked files #358

Closed DavidGamba closed 2 months ago

DavidGamba commented 2 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

v0.10.0-dev-2976+g208852126

Operating system/version

MacOS 14.4.1

Describe the bug

[oil] Error applying actions: Error in git mv: fatal: source directory is empty, source=cue/external-secrets, destination=cue/third-party/external-secrets

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

  1. mkdir a b c within a git repo.
  2. touch a.txt
  3. Open oil and move b into c. MOVE b/ -> c/b/. Click OK:
    [oil] Error applying actions: Error in giv mv: fatal: source directory is empty, source=b, destination=c/b
  4. try to move a into c.
    [oil] Error applying actions: Error in giv mv: fatal: source directory is empty, source=a, destination=c/a
  5. git add a/a.txt
  6. move a into c. -> success

Expected Behavior

Move operations of files not tracked by git should work.

Directory structure

a/a.txt b/ c/

Repro

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
        "stevearc/oil.nvim",
        config = function()
            require("oil").setup({
              -- add any needed settings here
                -- EXPERIMENTAL support for performing file operations with git
                git = {
                  -- Return true to automatically git add/mv/rm files
                  add = function(path)
                   return false
                  end,
                  mv = function(src_path, dest_path)
                   return true
                  end,
                  rm = function(path)
                   return true
                  end,
                },
            })
        end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Did you check the bug with a clean config?

DavidGamba commented 2 months ago

git ls-files can be given the path of any file to determine if the source is tracked in git. Combined with --error-unmatch you can just check for the exit code.

$ git ls-files --error-unmatch a
a/a.txt

$ git ls-files --error-unmatch b
error: pathspec 'b' did not match any file(s) known to git
Did you forget to 'git add'?