NeogitOrg / neogit

An interactive and powerful Git interface for Neovim, inspired by Magit
MIT License
3.71k stars 221 forks source link

neogit leaves behind another empty buffer every time its open #1289

Closed beaumccartney closed 2 months ago

beaumccartney commented 2 months ago

Description

opening neogit adds an empty buffer, closing neogit doesn't remove this buffer. they accumulate during use of neogit. especially problematic with tabs or other UI that works with buffers

Neovim version

NVIM v0.10.0-dev-3107+g8c7a8be27 Build type: RelWithDebInfo LuaJIT 2.1.1713484068 Run "nvim -V1 -v" for more info

Operating system and version

macos 14.4.1

Steps to reproduce

open neovim with any minimal repro (including the one provided by the issue template)

:Neogit then close with q or any other way

:buffers shows a no name buffer, keep opening neogit to add more buffers

Expected behavior

no accumulation of no name buffers

Actual behavior

accumulation of no name buffers

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
beaumccartney commented 2 months ago

this only happens on the nightly branch. easiest way i found to get there was to not clone with depth=1 and just check it out manually

CKolkey commented 2 months ago

No worries, I found the issue.

parga commented 2 months ago

this is also happening after upgrading to neovim 0.10, thanks @CKolkey in advance and looking forward for the fix

ronisbr commented 2 months ago

Yes, I am having the same problem and it started when I upgraded to Neovim 0.10 (but it can be a coincidence). The temporary solution was to change kind to replace.

kitallen23 commented 2 months ago

I am also having this issue.

The temporary solution was to change kind to replace.

This only seems to help for the main neogit instance. If you e.g. open the commit editor, it still creates a blank buffer.

konosubakonoakua commented 2 months ago

Me toooo. Hope be solved soon~ :D

wkwhwh commented 2 months ago

Same, I tried the replace thing but it doesn't matter if you are doing a lot of git stuff as it creates buffers for anything you do inside Neogit.

konosubakonoakua commented 2 months ago
keymap('n', '<leader>b.', function()
    local buffers = vim.api.nvim_list_bufs()
    for _, buf in ipairs(buffers) do
        if vim.api.nvim_buf_is_valid(buf) then
          local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
          if #lines == 1 and #lines[1] == 0 then
          -- Buffer is empty, delete it
          vim.api.nvim_buf_delete(buf, { force = true })
        end
      end
    end
  end, { desc = "Delete all empty buffer", noremap = true, silent = true })

for emergency. :D

CKolkey commented 2 months ago

If anyone still experiences this issue with the above fix merged, please let me know here. That said, I can reproduce the no name issue before, but not after, that patch.

qRoC commented 2 months ago

@CKolkey same behavior after update

CKolkey commented 2 months ago

Shoot, really? With all kinds (split, tab, etc), or one specifically?

wkwhwh commented 2 months ago

Still experiencing the same issue with no name buffers. I am using the default tab one

CleanShot 2024-05-24 at 14 54 11

qRoC commented 2 months ago

kind=tab

CKolkey commented 2 months ago

Are you both using a buffer-line plugin? I'm using :buffers to check this.

qRoC commented 2 months ago

I use tabby.nvim

kitallen23 commented 2 months ago

I am using bufferline and I am experiencing the same. Note that most empty buffers are gone; only the one from the main neogit instance is hanging about.

qRoC commented 2 months ago

Note that most empty buffers are gone; only the one from the main neogit instance is hanging about.

Screenshot 2024-05-24 at 17 00 28
kitallen23 commented 2 months ago

@qRoC Are you using any special kind, or the default? In my case where I'm getting an empty buffer for the main tab only, I'm using defaults for all except:

    commit_editor = {
        kind = "vsplit"
    }
qRoC commented 2 months ago

default

kitallen23 commented 2 months ago

Ahh, I can in fact reproduce every time I open a new tab, e.g. log_view etc, apologies.

wkwhwh commented 2 months ago

I am using default kind (tab), and I have lualine for my buffers, removing the setting to displaying buffer from lualine config still shows empty buffers being created. when I search buffers via telescope. For me nothing changed I still see buffers being created whenever I do any git related stuff inside neogit.

CleanShot 2024-05-24 at 15 03 51@2x

CKolkey commented 2 months ago

If someone could give this branch a try and let me know if the issue persists for them, I'd appreciate it :) https://github.com/NeogitOrg/neogit/pull/1326

wkwhwh commented 2 months ago

unfortunately for me it's still behaving the same way on that branch

CleanShot 2024-05-24 at 15 15 03@2x

kitallen23 commented 2 months ago

If someone could give this branch a try and let me know if the issue persists for them, I'd appreciate it :) #1326

Sorry to say but yes I'm still having the same issues :(

Thank you for all your efforts, it's appreciated!

CKolkey commented 2 months ago

I tried setting up both Tabby and https://github.com/akinsho/bufferline.nvim and can't get it to show any noname buffers :\

Also tried with a very very minimal config and can't reproduce.

Happy to keep trying to fix this, but I think at this point I need a minimal reproduction config to actually work on it. If someone wants to make that, then I'll do what I can to fix it :)

kitallen23 commented 2 months ago

Here is mine, and for what it's worth, I'm on macOS.

require("neogit").setup {
    use_default_keymaps = true,
    integrations = {
        diffview = false,
    },
    commit_editor = {
        kind = "vsplit",
        show_staged_diff = false,
        staged_diff_split_kind = "split"
    },
}
CKolkey commented 2 months ago

Alright, found a way to reproduce it! Thanks for the help, I'll try to fix this tonight after the kiddo is asleep

ronisbr commented 2 months ago

~Here, the new commit does not create anymore the empty buffer when opening the popup, but it does show if we open the commit message buffer.~

Sorry, I posted before reading the new comments. The problem here continues if kind = tab.

slainn commented 2 months ago

I'm also having this issue with bufferline enabled on last update Disabling bufferline solves it (os - windows, kind - default, neovim 0.10)

@CKolkey thanks for trying to fix it

iH8c0ff33 commented 2 months ago

@CKolkey the empty buffer seems to be created by tabnew. Replacing the call of tabnew (in Buffer:show()) with tab sb <bufno> solved the problem for me.

diff --git a/lua/neogit/lib/buffer.lua b/lua/neogit/lib/buffer.lua
index 42c14789..c70f522e 100644
--- a/lua/neogit/lib/buffer.lua
+++ b/lua/neogit/lib/buffer.lua
@@ -258,8 +258,7 @@ function Buffer:show()
     api.nvim_set_current_buf(self.handle)
     win = api.nvim_get_current_win()
   elseif kind == "tab" then
-    vim.cmd("tabnew")
-    api.nvim_set_current_buf(self.handle)
+    vim.cmd("tab sb " .. self.handle)
     win = api.nvim_get_current_win()
   elseif kind == "split" then
     win = api.nvim_open_win(self.handle, true, { split = "below" })
wkwhwh commented 2 months ago

the fix from @iH8c0ff33 also solved the problem for me.

CKolkey commented 2 months ago

Awesome - I'll have it shipped in a few minutes. Thank you @iH8c0ff33 !

qRoC commented 2 months ago

@CKolkey new problem :)

  1. Run Neogit - this create new tab
  2. Close this tab - This close all buffers

https://github.com/NeogitOrg/neogit/assets/1789565/a4dcad38-7233-413b-9a2b-7f623b7cff31

qRoC commented 2 months ago

depends on CWD

qRoC commented 2 months ago

https://github.com/NeogitOrg/neogit/assets/1789565/ba35fb12-ae2d-49a3-82e0-bceecde2e3ec

CKolkey commented 2 months ago

guys, I've got a 2 year old and my wife is pregnant - feel free to submit a PR 😅

Anyways, I appreciate the patience otherwise :)

ilias777 commented 2 months ago

The fix works for me. No any empty buffers. 👍🏻