feat(git): open commits from gitlog in the same window
This commit is contained in:
+20
-9
@@ -127,9 +127,14 @@ local function show_diff(ctx, section)
|
||||
vim.api.nvim_set_current_win(left_win)
|
||||
end
|
||||
|
||||
---@class ow.Git.OpenCommitOpts
|
||||
---@field split (false|"above"|"below"|"left"|"right")? forwarded to `git.new_scratch`. Default opens a new horizontal split.
|
||||
|
||||
---@param worktree string
|
||||
---@param ref string
|
||||
function M.open_commit(worktree, ref)
|
||||
---@param opts ow.Git.OpenCommitOpts?
|
||||
function M.open_commit(worktree, ref, opts)
|
||||
local split = opts and opts.split
|
||||
repo.rev_parse(worktree, ref, true, function(resolved)
|
||||
local sha = resolved or ref
|
||||
local name = "git://" .. sha .. "//"
|
||||
@@ -137,13 +142,18 @@ function M.open_commit(worktree, ref)
|
||||
-- are immutable so the content is stable.
|
||||
local existing = vim.fn.bufnr(name)
|
||||
if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then
|
||||
vim.api.nvim_open_win(existing, true, {
|
||||
split = vim.o.splitbelow and "below" or "above",
|
||||
})
|
||||
if split == false then
|
||||
vim.cmd.normal({ "m'", bang = true })
|
||||
vim.api.nvim_set_current_buf(existing)
|
||||
else
|
||||
vim.api.nvim_open_win(existing, true, {
|
||||
split = split or (vim.o.splitbelow and "below" or "above"),
|
||||
})
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
local buf, win = git.new_scratch({ name = name })
|
||||
local buf, win = git.new_scratch({ name = name, split = split })
|
||||
vim.b[buf].git_worktree = worktree
|
||||
vim.b[buf].git_ref = sha
|
||||
|
||||
@@ -157,10 +167,11 @@ function M.open_commit(worktree, ref)
|
||||
ref,
|
||||
vim.trim(result.stderr or "")
|
||||
)
|
||||
-- Tear down the empty placeholder window+buffer so a
|
||||
-- retry runs a fresh fetch instead of hitting the
|
||||
-- cached-buffer branch and reopening an empty pane.
|
||||
if vim.api.nvim_win_is_valid(win) then
|
||||
-- Drop the empty placeholder so a retry runs a fresh
|
||||
-- fetch. With `split = false` the window falls back to
|
||||
-- its alternate buffer (the gitlog); for a real split
|
||||
-- we close the dedicated window to keep the layout tidy.
|
||||
if split ~= false and vim.api.nvim_win_is_valid(win) then
|
||||
pcall(vim.api.nvim_win_close, win, true)
|
||||
end
|
||||
if vim.api.nvim_buf_is_valid(buf) then
|
||||
|
||||
Reference in New Issue
Block a user