diff --git a/lua/git/diff.lua b/lua/git/diff.lua index e9e14b3..2d619fd 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -110,14 +110,26 @@ function M.git_show_blob(worktree, blob) return buf end +---@class ow.Git.EmptyBufOpts +---@field name string? +---@field bufhidden ("hide"|"wipe")? defaults to "wipe" + +---Build a read-only scratch buffer, optionally naming it via +---`nvim_buf_set_name` (silently no-op if a buffer with that name +---already exists). +---@param opts ow.Git.EmptyBufOpts? ---@return integer -function M.empty_buf() +function M.empty_buf(opts) + opts = opts or {} local buf = vim.api.nvim_create_buf(false, true) vim.bo[buf].buftype = "nofile" - vim.bo[buf].bufhidden = "wipe" + vim.bo[buf].bufhidden = opts.bufhidden or "wipe" vim.bo[buf].swapfile = false vim.bo[buf].modifiable = false vim.bo[buf].modified = false + if opts.name then + pcall(vim.api.nvim_buf_set_name, buf, opts.name) + end return buf end diff --git a/lua/git/show.lua b/lua/git/show.lua index 6632222..23625a4 100644 --- a/lua/git/show.lua +++ b/lua/git/show.lua @@ -77,19 +77,6 @@ local function is_zero(sha) return sha == nil or sha:match("^0+$") ~= nil end ----@param ref string buffer-name ref segment ----@param path string ----@return integer -local function empty_buf(ref, path) - local buf = vim.api.nvim_create_buf(false, true) - vim.bo[buf].buftype = "nofile" - vim.bo[buf].bufhidden = "hide" - vim.bo[buf].swapfile = false - vim.bo[buf].modifiable = false - pcall(vim.api.nvim_buf_set_name, buf, "git://" .. ref .. "/" .. path) - return buf -end - ---Build a buffer holding the file's content at a given blob, named after the ---commit ref it corresponds to (so the name lines up with `git log` output ---instead of an opaque blob hash). @@ -100,7 +87,10 @@ end ---@return integer local function blob_buf(worktree, blob, path, ref) if is_zero(blob) then - return empty_buf(ref, path) + return diff.empty_buf({ + name = "git://" .. ref .. "/" .. path, + bufhidden = "hide", + }) end ---@cast blob string local buf = diff.git_show_blob(worktree, blob)