Skip to content

Commit

Permalink
feat: git restore using git_status picker
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenithFlux committed Jan 5, 2025
1 parent 2eca9ba commit 398281f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,34 @@ actions.git_staging_toggle = function(prompt_bufnr)
end
end

--- Restore a file from the git index
---@param prompt_bufnr number: The prompt bufnr
actions.git_restore_to_index = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_restore_to_index"
return
end
if selection.status:sub(2) == " " then
utils.notify("actions.git_restore_to_index", {
msg = "Nothing to restore",
level = "WARN",
})
return
elseif selection.status:sub(2) == "?" then
utils.notify("action.git_restore_to_index", {
msg = "File not present at HEAD or index",
level = "ERROR",
})
return
end
if not ask_to_confirm("All worktree changes to the file will be lost. Proceed? [y/n] ", "y") then
return
end
utils.get_os_command_output({ "git", "restore", selection.value }, cwd)
end

local entry_to_qf = function(entry)
local text = entry.text

Expand Down
16 changes: 16 additions & 0 deletions lua/telescope/builtin/__git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,24 @@ git.status = function(opts)
picker:refresh(gen_new_finder(), { reset_prompt = false })
end,
}
actions.git_restore_to_index:enhance {
post = function()
local picker = action_state.get_current_picker(prompt_bufnr)

-- temporarily register a callback which keeps selection on refresh
local selection = picker:get_selection_row()
local callbacks = { unpack(picker._completion_callbacks) } -- shallow copy
picker:register_completion_callback(function(self)
self:set_selection(selection)
self._completion_callbacks = callbacks
end)

picker:refresh(gen_new_finder(), { reset_prompt = false })
end
}

map({ "i", "n" }, "<tab>", actions.git_staging_toggle)
map({ "i", "n" }, "<c-r>", actions.git_restore_to_index)
return true
end,
})
Expand Down

0 comments on commit 398281f

Please sign in to comment.