From 49e0ac726e3e4bf1dd7e337f693dec41821a5112 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Mon, 27 Apr 2026 13:51:44 +0200 Subject: [PATCH] refactor(git): share builder between git_show_buf and git_show_blob --- lua/git/diff.lua | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lua/git/diff.lua b/lua/git/diff.lua index 2d619fd..a0134e3 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -74,20 +74,23 @@ local function read_show(worktree, revspec) return util.split_lines(result.stdout or "") end +---Internal builder: run `git show `, 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