Skip to content

Commit 0fb4d16

Browse files
committed
feat: extend restore_entry to work on directories (sindrets#186)
The restore_entry action (X) now restores all files in a directory when the cursor is on a folder. Files with unsaved changes are skipped with a warning.
1 parent e41367c commit 0fb4d16

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

lua/diffview/scene/views/diff/listeners.lua

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,47 @@ return function(view)
263263
commit = view.left.commit
264264
end
265265

266-
local file = view:infer_cur_file()
267-
if not file then return end
266+
local item = view:infer_cur_file(true)
267+
if not item then return end
268+
269+
-- Check if item is a directory.
270+
if type(item.collapsed) == "boolean" then
271+
---@cast item DirData
272+
local node = item._node
273+
if not node then return end
274+
275+
-- Get all files under this directory.
276+
local leaves = node:leaves()
277+
local restored_count = 0
278+
for _, leaf in ipairs(leaves) do
279+
local file = leaf.data
280+
if file and file.path then
281+
local bufid = utils.find_file_buffer(file.path)
282+
if bufid and vim.bo[bufid].modified then
283+
utils.warn(("Skipping '%s': file has unsaved changes."):format(file.path))
284+
else
285+
await(vcs_utils.restore_file(view.adapter, file.path, file.kind, commit))
286+
restored_count = restored_count + 1
287+
end
288+
end
289+
end
268290

269-
local bufid = utils.find_file_buffer(file.path)
291+
if restored_count > 0 then
292+
utils.info(("Restored %d file(s)."):format(restored_count))
293+
end
294+
else
295+
-- Single file restore.
296+
local file = item
297+
local bufid = utils.find_file_buffer(file.path)
270298

271-
if bufid and vim.bo[bufid].modified then
272-
utils.err("The file is open with unsaved changes! Aborting file restoration.")
273-
return
299+
if bufid and vim.bo[bufid].modified then
300+
utils.err("The file is open with unsaved changes! Aborting file restoration.")
301+
return
302+
end
303+
304+
await(vcs_utils.restore_file(view.adapter, file.path, file.kind, commit))
274305
end
275306

276-
await(vcs_utils.restore_file(view.adapter, file.path, file.kind, commit))
277307
view:update_files()
278308
end),
279309
listing_style = function()

0 commit comments

Comments
 (0)