perf(git): fold revspec validation into the cat-file -p the loader needs anyway
This commit is contained in:
+23
-6
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user