fix(picker.actions): only allow stage/unstage/restore for some diffs

This commit is contained in:
Folke Lemaitre 2025-11-03 13:32:15 +01:00
parent b553c18c26
commit 9cde35b7b1
No known key found for this signature in database
GPG key ID: 9B52594D560070AB

View file

@ -339,19 +339,24 @@ end
function M.git_stage(picker)
local items = picker:selected({ fallback = true })
if items[1] and not items[1].status then
Snacks.notify.warn("Can't stage/unstage this change", { title = "Snacks Picker" })
local first = items[1]
if not first or not (first.status or (first.diff and first.staged ~= nil)) then
Snacks.notify.error("Can't stage/unstage this change", { title = "Snacks Picker" })
return
end
local done = 0
for _, item in ipairs(items) do
local opts = { cwd = item.cwd } ---@type snacks.picker.util.cmd.Opts
local cmd = item.status:sub(2) == " " and { "git", "restore", "--staged", item.file } or { "git", "add", item.file }
if item.diff then
local cmd ---@type string[]
if item.diff and item.staged ~= nil then
opts.input = item.diff
cmd = { "git", "apply", "--cached", item.staged and "--reverse" or nil }
elseif item.status then
cmd = item.status:sub(2) == " " and { "git", "restore", "--staged", item.file } or { "git", "add", item.file }
else
Snacks.notify.error("Can't stage/unstage this change", { title = "Snacks Picker" })
return
end
Snacks.picker.util.cmd(cmd, function()
done = done + 1
@ -368,7 +373,8 @@ function M.git_restore(picker)
return
end
if items[1] and not items[1].status then
local first = items[1]
if not first or not (first.status or (first.diff and first.staged ~= nil)) then
Snacks.notify.warn("Can't restore this change", { title = "Snacks Picker" })
return
end
@ -384,16 +390,21 @@ function M.git_restore(picker)
Snacks.picker.util.confirm(msg, function()
local done = 0
for _, item in ipairs(items) do
local cmd = { "git", "restore", item.file }
local cmd ---@type string[]
local opts = { cwd = item.cwd }
if item.diff then
if item.diff and item.staged ~= nil then
opts.input = item.diff
if item.staged then
cmd = { "git", "apply", "--reverse", "--cached" }
else
cmd = { "git", "apply", "--reverse" }
end
elseif item.status then
cmd = { "git", "restore", item.file }
else
Snacks.notify.error("Can't restore this change", { title = "Snacks Picker" })
return
end
Snacks.picker.util.cmd(cmd, function()