refactor(git): unify absent-side handling in commit views

This commit is contained in:
2026-04-30 05:59:06 +02:00
parent 0fb0e4557e
commit 04c38d84ce
+20 -11
View File
@@ -275,22 +275,16 @@ function M.read_uri(buf)
vim.api.nvim_exec_autocmds("BufReadPost", { buffer = buf })
end
---Buffer for the file at `<ref>:<path>`. A zero/nil blob (file absent
---on this side of the diff) maps to `/dev/null`, mirroring git's diff
---headers.
---Buffer for the file at `<ref>:<path>`. Returns nil for a zero/nil
---blob (file absent on this side of the diff).
---@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
---@return integer?
local function blob_buf(worktree, blob, path, ref)
if is_zero(blob) then
local buf = vim.fn.bufnr("/dev/null")
if buf == -1 then
buf = vim.api.nvim_create_buf(true, true)
pcall(vim.api.nvim_buf_set_name, buf, "/dev/null")
end
return buf
return nil
end
return M.buf_for(worktree, ref .. ":" .. path)
end
@@ -301,6 +295,10 @@ end
---@param ref string
local function load_blob(worktree, blob, path, ref)
local buf = blob_buf(worktree, blob, path, ref)
if not buf then
util.warning("no content for %s at %s", path, ref)
return
end
vim.cmd.normal({ "m'", bang = true })
vim.api.nvim_set_current_buf(buf)
end
@@ -317,7 +315,18 @@ local function open_section(ctx, section)
blob_buf(ctx.worktree, section.pre_blob, section.pre_path, parent)
local right =
blob_buf(ctx.worktree, section.post_blob, section.post_path, ctx.ref)
diff.open(left, right, true)
if left and right then
diff.open(left, right, true)
return
end
if not left and not right then
util.warning("no content for %s", section.post_path)
return
end
local buf = left or right
---@cast buf -nil
vim.cmd.normal({ "m'", bang = true })
vim.api.nvim_set_current_buf(buf)
end
---@class ow.Git.OpenObjectOpts