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
+30 -1
View File
@@ -68,10 +68,39 @@ local function first_positional(args, start)
end
end
---Open `<ref>:<path>` in a split via the `git://` BufReadCmd loader.
---Resolves to a sha first so the URI stays stable if the ref moves.
---@param worktree string
---@param user_ref string
---@param path string
local function show_file_in_split(worktree, user_ref, path)
repo.rev_parse(worktree, user_ref, true, function(sha)
local label = sha or user_ref
local uri = "git://" .. label .. "//" .. path
local buf = vim.fn.bufadd(uri)
vim.b[buf].git_worktree = worktree
vim.cmd("split " .. vim.fn.fnameescape(uri))
end)
end
---@param worktree string
---@param args string[]
---@param conf ow.Git.SplitHandler
local function run_in_split(worktree, args, conf)
-- `<ref>:<path>` is a file lookup; the URI must carry the path so
-- filetype detection has something to match against.
if args[1] == "show" then
local arg = first_positional(args, 2)
if arg then
local ref, path = arg:match("^(.-):(.+)$")
if ref then
---@cast path -nil
show_file_in_split(worktree, ref, path)
return
end
end
end
local buf = git.new_scratch()
vim.b[buf].git_worktree = worktree
if conf.needs_ref then
@@ -80,7 +109,7 @@ local function run_in_split(worktree, args, conf)
if not vim.api.nvim_buf_is_valid(buf) then
return
end
pcall(vim.api.nvim_buf_set_name, buf, "git://" .. label .. "/")
pcall(vim.api.nvim_buf_set_name, buf, "git://" .. label .. "//")
vim.bo[buf].filetype = conf.ft
end
repo.rev_parse(worktree, user_ref, true, function(sha)