refactor(git): unify absent-side handling in commit views
This commit is contained in:
+20
-11
@@ -275,22 +275,16 @@ function M.read_uri(buf)
|
|||||||
vim.api.nvim_exec_autocmds("BufReadPost", { buffer = buf })
|
vim.api.nvim_exec_autocmds("BufReadPost", { buffer = buf })
|
||||||
end
|
end
|
||||||
|
|
||||||
---Buffer for the file at `<ref>:<path>`. A zero/nil blob (file absent
|
---Buffer for the file at `<ref>:<path>`. Returns nil for a zero/nil
|
||||||
---on this side of the diff) maps to `/dev/null`, mirroring git's diff
|
---blob (file absent on this side of the diff).
|
||||||
---headers.
|
|
||||||
---@param worktree string
|
---@param worktree string
|
||||||
---@param blob string?
|
---@param blob string?
|
||||||
---@param path string
|
---@param path string
|
||||||
---@param ref string the commit ref the blob represents (e.g. `<sha>` or `<sha>^`)
|
---@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)
|
local function blob_buf(worktree, blob, path, ref)
|
||||||
if is_zero(blob) then
|
if is_zero(blob) then
|
||||||
local buf = vim.fn.bufnr("/dev/null")
|
return nil
|
||||||
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
|
|
||||||
end
|
end
|
||||||
return M.buf_for(worktree, ref .. ":" .. path)
|
return M.buf_for(worktree, ref .. ":" .. path)
|
||||||
end
|
end
|
||||||
@@ -301,6 +295,10 @@ end
|
|||||||
---@param ref string
|
---@param ref string
|
||||||
local function load_blob(worktree, blob, path, ref)
|
local function load_blob(worktree, blob, path, ref)
|
||||||
local buf = blob_buf(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.cmd.normal({ "m'", bang = true })
|
||||||
vim.api.nvim_set_current_buf(buf)
|
vim.api.nvim_set_current_buf(buf)
|
||||||
end
|
end
|
||||||
@@ -317,7 +315,18 @@ local function open_section(ctx, section)
|
|||||||
blob_buf(ctx.worktree, section.pre_blob, section.pre_path, parent)
|
blob_buf(ctx.worktree, section.pre_blob, section.pre_path, parent)
|
||||||
local right =
|
local right =
|
||||||
blob_buf(ctx.worktree, section.post_blob, section.post_path, ctx.ref)
|
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
|
end
|
||||||
|
|
||||||
---@class ow.Git.OpenObjectOpts
|
---@class ow.Git.OpenObjectOpts
|
||||||
|
|||||||
Reference in New Issue
Block a user