From 04c38d84ce40fbcf481c4b32127fc1441622c64b Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Thu, 30 Apr 2026 05:59:06 +0200 Subject: [PATCH] refactor(git): unify absent-side handling in commit views --- lua/git/object.lua | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lua/git/object.lua b/lua/git/object.lua index 6a9847b..5aea6e3 100644 --- a/lua/git/object.lua +++ b/lua/git/object.lua @@ -275,22 +275,16 @@ function M.read_uri(buf) vim.api.nvim_exec_autocmds("BufReadPost", { buffer = buf }) end ----Buffer for the file at `:`. 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 `:`. 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. `` or `^`) ----@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