refactor(git): consolidate empty_buf with optional name + bufhidden

This commit is contained in:
2026-04-27 13:50:01 +02:00
parent b4c166e86c
commit bbaa0b4a6d
2 changed files with 18 additions and 16 deletions
+14 -2
View File
@@ -110,14 +110,26 @@ function M.git_show_blob(worktree, blob)
return buf return buf
end 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 ---@return integer
function M.empty_buf() function M.empty_buf(opts)
opts = opts or {}
local buf = vim.api.nvim_create_buf(false, true) local buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].buftype = "nofile" 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].swapfile = false
vim.bo[buf].modifiable = false vim.bo[buf].modifiable = false
vim.bo[buf].modified = false vim.bo[buf].modified = false
if opts.name then
pcall(vim.api.nvim_buf_set_name, buf, opts.name)
end
return buf return buf
end end
+4 -14
View File
@@ -77,19 +77,6 @@ local function is_zero(sha)
return sha == nil or sha:match("^0+$") ~= nil return sha == nil or sha:match("^0+$") ~= nil
end 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 ---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 ---commit ref it corresponds to (so the name lines up with `git log` output
---instead of an opaque blob hash). ---instead of an opaque blob hash).
@@ -100,7 +87,10 @@ end
---@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
return empty_buf(ref, path) return diff.empty_buf({
name = "git://" .. ref .. "/" .. path,
bufhidden = "hide",
})
end end
---@cast blob string ---@cast blob string
local buf = diff.git_show_blob(worktree, blob) local buf = diff.git_show_blob(worktree, blob)