perf(git): fold revspec validation into the cat-file -p the loader needs anyway

This commit is contained in:
2026-04-29 09:53:48 +02:00
parent 5c5da7a854
commit 590651e9d8
3 changed files with 42 additions and 28 deletions
+23 -6
View File
@@ -135,16 +135,29 @@ local function attach_index_writer(buf, worktree, path)
})
end
---Pre-fetched content keyed by bufnr. Set by `buf_for(_, _, content)`
---and consumed by the next `read_uri` dispatch on that buffer. Lets
---callers fold validation + content fetch + buffer load into one
---`cat-file` call instead of preflighting separately.
---@type table<integer, string>
local pending_content = {}
---Return a buffer holding the content addressed by a git revspec. The
---URI is `git://<revspec>` and BufReadCmd routes through `M.read_uri`,
---which loads via `git cat-file -p`.
---which loads via `git cat-file -p`. If `content` is given, primes a
---cache so the BufReadCmd handler reuses it instead of running another
---`cat-file -p`.
---@param worktree string
---@param revspec string any revspec git understands (e.g. `HEAD:foo`, `:foo`, `:1:foo`, `<sha>`, `<sha>:foo`)
---@param content string?
---@return integer
function M.buf_for(worktree, revspec)
function M.buf_for(worktree, revspec, content)
local name = "git://" .. revspec
local buf = vim.fn.bufadd(name)
vim.b[buf].git_worktree = worktree
if content then
pending_content[buf] = content
end
vim.fn.bufload(buf)
return buf
end
@@ -172,10 +185,14 @@ function M.read_uri(buf)
vim.bo[buf].swapfile = false
vim.bo[buf].bufhidden = "wipe"
local stdout = util.exec(
{ "git", "cat-file", "-p", revspec },
{ cwd = worktree }
)
local stdout = pending_content[buf]
pending_content[buf] = nil
if stdout == nil then
stdout = util.exec(
{ "git", "cat-file", "-p", revspec },
{ cwd = worktree }
)
end
if stdout then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, util.split_lines(stdout))
end