From 148d1f5f6e737e8ec5122ad0f13c1fe340f1afba Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Tue, 28 Apr 2026 14:06:17 +0200 Subject: [PATCH] refactor(git): bracket-named scratches for :G, reuse on re-run --- lua/git/cmd.lua | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lua/git/cmd.lua b/lua/git/cmd.lua index 6270c96..7dbad16 100644 --- a/lua/git/cmd.lua +++ b/lua/git/cmd.lua @@ -109,8 +109,32 @@ local function run_in_split(worktree, args, conf) end end - local buf = git.new_scratch() + -- Bracket naming so the buffer name doesn't pretend to be a real + -- file path (and doesn't collide with the `git://` URI + -- form used by the BufReadCmd loader). Reused on re-run so a + -- second `:G show HEAD` refreshes the existing buffer in place + -- instead of leaving a nameless duplicate behind. + local name = "[git " .. table.concat(args, " ") .. "]" + local buf = vim.fn.bufnr(name) + if buf == -1 or not vim.api.nvim_buf_is_loaded(buf) then + buf = git.new_scratch() + pcall(vim.api.nvim_buf_set_name, buf, name) + else + local win_id = vim.fn.bufwinid(buf) + if win_id ~= -1 then + vim.api.nvim_set_current_win(win_id) + else + vim.api.nvim_open_win(buf, true, { + split = vim.o.splitbelow and "below" or "above", + }) + end + vim.bo[buf].modifiable = true + vim.api.nvim_buf_set_lines(buf, 0, -1, false, {}) + end + vim.b[buf].git_worktree = worktree + vim.b[buf].git_ref = nil + vim.b[buf].git_parent_ref = nil if conf.needs_ref then local user_ref = first_positional(args, 2) or "HEAD" local sha = repo.rev_parse(worktree, user_ref, true) @@ -119,17 +143,8 @@ local function run_in_split(worktree, args, conf) vim.b[buf].git_parent_ref = repo.rev_parse(worktree, user_ref .. "^", true) end - -- Per-subcommand scheme so `:G show ` doesn't collide with - -- the cat-file URI form `git://` used by the gitlog flow. - pcall( - vim.api.nvim_buf_set_name, - buf, - "git" .. args[1] .. "://" .. (sha or user_ref) - ) - vim.bo[buf].filetype = conf.ft - else - vim.bo[buf].filetype = conf.ft end + vim.bo[buf].filetype = conf.ft local cmd = { "git" } vim.list_extend(cmd, args)