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 "") return util.split_lines(result.stdout or "")
end 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 worktree string
---@param ref string '' for index, 'HEAD' or a sha for committed refs ---@param revspec string
---@param path string ---@param is_index boolean
---@param is_index boolean? true to hook :w to update the git index ---@param index_path string? required when is_index is true
---@return integer ---@return integer
function M.git_show_buf(worktree, ref, path, is_index) local function build_show_buf(worktree, revspec, is_index, index_path)
local lines = read_show(worktree, ref .. ":" .. path) local lines = read_show(worktree, revspec)
local buf = vim.api.nvim_create_buf(false, true) local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].buftype = is_index and "acwrite" or "nofile" vim.bo[buf].buftype = is_index and "acwrite" or "nofile"
vim.bo[buf].bufhidden = "wipe" vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].swapfile = false vim.bo[buf].swapfile = false
if is_index then if is_index then
attach_index_writer(buf, worktree, path) attach_index_writer(buf, worktree, assert(index_path))
else else
vim.bo[buf].modifiable = false vim.bo[buf].modifiable = false
end end
@@ -95,19 +98,20 @@ function M.git_show_buf(worktree, ref, path, is_index)
return buf return buf
end 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 worktree string
---@param blob string the blob SHA (full or abbreviated) ---@param blob string the blob SHA (full or abbreviated)
---@return integer ---@return integer
function M.git_show_blob(worktree, blob) function M.git_show_blob(worktree, blob)
local lines = read_show(worktree, blob) return build_show_buf(worktree, blob, false, nil)
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
end end
---@class ow.Git.EmptyBufOpts ---@class ow.Git.EmptyBufOpts