Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions lua/codediff/ui/explorer/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,28 @@ function M.unstage_all(explorer)
end)
end

local function real_path(file)
local resolved = vim.loop.fs_realpath(file)
return resolved and resolved:gsub("\\", "/")
end

-- Reload clean buffers of restored files, since Neovim does not see the git
-- write until focus returns or :checktime runs
local function reload_restored_buffers(git_root, entry_path)
local target = real_path(git_root .. "/" .. entry_path)
if not target then
return
end
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) and vim.bo[buf].buftype == "" and not vim.bo[buf].modified then
local file = real_path(vim.api.nvim_buf_get_name(buf))
if file and (file == target or vim.startswith(file, target .. "/")) then
pcall(vim.cmd, "silent! checktime " .. buf)
end
end
end
end

-- Restore/discard changes to the selected file or directory
function M.restore_entry(explorer, tree)
if not explorer or not explorer.data.git_root then
Expand Down Expand Up @@ -463,21 +485,25 @@ function M.restore_entry(explorer, tree)
-- Run git restore for tracked changes, then git clean for untracked
git.restore_file(explorer.data.git_root, entry_path, explorer.data.base_revision, function(restore_err)
git.delete_untracked(explorer.data.git_root, entry_path, function(clean_err)
if restore_err and clean_err then
vim.schedule(function()
vim.schedule(function()
if restore_err and clean_err then
vim.notify("Failed to restore: " .. restore_err, vim.log.levels.ERROR)
end)
end
else
reload_restored_buffers(explorer.data.git_root, entry_path)
end
end)
end)
end)
else
-- Restore tracked file
git.restore_file(explorer.data.git_root, entry_path, explorer.data.base_revision, function(err)
if err then
vim.schedule(function()
vim.schedule(function()
if err then
vim.notify(err, vim.log.levels.ERROR)
end)
end
else
reload_restored_buffers(explorer.data.git_root, entry_path)
end
end)
end)
end
end
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/ui/explorer/staging_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,32 @@ describe("Explorer review-flow re-selection (issue #347)", function()
assert.equals("unstaged", explorer.data.current_file_group, "a.txt should stay in the unstaged group")
end)

it("reloads the buffer of a restored file that is not shown in the diff", function()
vim.cmd("edit " .. repo.path("b.txt"))
local b_buf = vim.api.nvim_get_current_buf()
local ready, explorer = open_explorer("a.txt")
assert.is_true(ready, "explorer should open")

local line = line_for(explorer, "b.txt", "unstaged")
assert.is_not_nil(line, "b.txt should appear in unstaged")
vim.api.nvim_set_current_win(explorer.winid)
vim.api.nvim_win_set_cursor(explorer.winid, { line, 0 })

local original_confirm = vim.fn.confirm
vim.fn.confirm = function()
return 1
end
explorer_actions.restore_entry(explorer, explorer.tree)
vim.fn.confirm = original_confirm

assert.is_true(
vim.wait(4000, function()
return vim.api.nvim_buf_get_lines(b_buf, 0, -1, false)[1] == "original b.txt"
end, 50),
"b.txt buffer should show the restored content, got " .. vim.inspect(vim.api.nvim_buf_get_lines(b_buf, 0, -1, false))
)
end)

it("keeps the staged file selected when no unstaged files remain", function()
local ready, explorer = open_explorer("a.txt")
assert.is_true(ready, "explorer should open")
Expand Down