refactor(git): bracket-named scratches for :G, reuse on re-run

This commit is contained in:
2026-04-28 14:06:17 +02:00
parent 03c6cb9ddc
commit 148d1f5f6e
+26 -11
View File
@@ -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://<revspec>` 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 <sha>` doesn't collide with
-- the cat-file URI form `git://<sha>` 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)