refactor(git): share builder between git_show_buf and git_show_blob

This commit is contained in:
2026-04-27 13:51:44 +02:00
parent bbaa0b4a6d
commit 49e0ac726e
+19 -15
View File
@@ -74,20 +74,23 @@ local function read_show(worktree, revspec)
return util.split_lines(result.stdout or "")
end
---Internal builder: run `git show <revspec>`, drop the result into a fresh
---scratch buffer, and (when `is_index` is true) wire up the BufWriteCmd
---that writes back to the git index for `index_path`.
---@param worktree string
---@param ref string '' for index, 'HEAD' or a sha for committed refs
---@param path string
---@param is_index boolean? true to hook :w to update the git index
---@param revspec string
---@param is_index boolean
---@param index_path string? required when is_index is true
---@return integer
function M.git_show_buf(worktree, ref, path, is_index)
local lines = read_show(worktree, ref .. ":" .. path)
local function build_show_buf(worktree, revspec, is_index, index_path)
local lines = read_show(worktree, revspec)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].buftype = is_index and "acwrite" or "nofile"
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].swapfile = false
if is_index then
attach_index_writer(buf, worktree, path)
attach_index_writer(buf, worktree, assert(index_path))
else
vim.bo[buf].modifiable = false
end
@@ -95,19 +98,20 @@ function M.git_show_buf(worktree, ref, path, is_index)
return buf
end
---@param worktree string
---@param ref string '' for index, 'HEAD' or a sha for committed refs
---@param path string
---@param is_index boolean? true to hook :w to update the git index
---@return integer
function M.git_show_buf(worktree, ref, path, is_index)
return build_show_buf(worktree, ref .. ":" .. path, is_index or false, path)
end
---@param worktree string
---@param blob string the blob SHA (full or abbreviated)
---@return integer
function M.git_show_blob(worktree, blob)
local lines = read_show(worktree, blob)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].buftype = "nofile"
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].swapfile = false
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
return buf
return build_show_buf(worktree, blob, false, nil)
end
---@class ow.Git.EmptyBufOpts