refactor(git): load git:// URI buffers via BufReadCmd

This commit is contained in:
2026-04-28 07:34:07 +02:00
parent 4f7edfa184
commit f8cf18a2c0
5 changed files with 127 additions and 95 deletions
+5 -16
View File
@@ -80,30 +80,19 @@ local function is_zero(sha)
return sha == nil or sha:match("^0+$") ~= nil
end
---Build a buffer holding the file's content at a given blob, named after the
---commit ref it corresponds to (so the name lines up with `git log` output
---instead of an opaque blob hash).
---Buffer for the file at `<ref>:<path>`. A zero/nil blob (file absent on
---this side of the diff) yields an empty placeholder.
---@param worktree string
---@param blob string?
---@param path string
---@param ref string the commit ref the blob represents (e.g. `<sha>` or `<sha>^`)
---@return integer
local function blob_buf(worktree, blob, path, ref)
local name = "git://" .. ref .. "/" .. path
-- Reuse an existing buffer with this name (commit refs and blobs are
-- immutable, so the content is stable); avoids E95 collisions and
-- prevents accumulating duplicate buffers as the user navigates.
local existing = vim.fn.bufnr(name)
if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then
return existing
end
local name = "git://" .. ref .. "//" .. path
if is_zero(blob) then
return diff.empty_buf({ name = name, bufhidden = "hide" })
end
---@cast blob string
local buf = diff.git_show_blob(worktree, blob)
diff.set_buf_name_and_filetype(buf, name)
return buf
return diff.git_show_buf(worktree, ref, path)
end
---@param worktree string
@@ -143,7 +132,7 @@ end
function M.open_commit(worktree, ref)
repo.rev_parse(worktree, ref, true, function(resolved)
local sha = resolved or ref
local name = "git://" .. sha .. "/"
local name = "git://" .. sha .. "//"
-- Reuse a previously-opened buffer for the same commit; commit SHAs
-- are immutable so the content is stable.
local existing = vim.fn.bufnr(name)