From 62b10daafd67f6ec5cf2950cf9550767bb23c0cc Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Thu, 19 Nov 2026 03:43:26 -0800 Subject: [PATCH] fix(explorer): reload buffers of restored files Signed-off-by: Sai Asish Y --- lua/codediff/ui/explorer/actions.lua | 42 +++++++++++++++---- .../integration/ui/explorer/staging_spec.lua | 26 ++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/lua/codediff/ui/explorer/actions.lua b/lua/codediff/ui/explorer/actions.lua index f06175d5..6542db59 100644 --- a/lua/codediff/ui/explorer/actions.lua +++ b/lua/codediff/ui/explorer/actions.lua @@ -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 @@ -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 diff --git a/tests/integration/ui/explorer/staging_spec.lua b/tests/integration/ui/explorer/staging_spec.lua index bb2b013d..1ad148f5 100644 --- a/tests/integration/ui/explorer/staging_spec.lua +++ b/tests/integration/ui/explorer/staging_spec.lua @@ -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")