Skip to content

Commit 1283c2b

Browse files
committed
feat(restore): you can now restore a plugin to a certain commit. Fixes #234
1 parent 4b29f7e commit 1283c2b

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -483,21 +483,21 @@ Any operation can be started from the UI, with a sub command or an API function:
483483

484484
<!-- commands:start -->
485485

486-
| Command | Lua | Description |
487-
| ------------------------- | -------------------------------- | --------------------------------------------------------------------------------------------- |
488-
| `:Lazy check [plugins]` | `require("lazy").check(opts?)` | Check for updates and show the log (git fetch) |
489-
| `:Lazy clean [plugins]` | `require("lazy").clean(opts?)` | Clean plugins that are no longer needed |
490-
| `:Lazy clear` | `require("lazy").clear()` | Clear finished tasks |
491-
| `:Lazy debug` | `require("lazy").debug()` | Show debug information |
492-
| `:Lazy help` | `require("lazy").help()` | Toggle this help page |
493-
| `:Lazy home` | `require("lazy").home()` | Go back to plugin list |
494-
| `:Lazy install [plugins]` | `require("lazy").install(opts?)` | Install missing plugins |
495-
| `:Lazy load {plugins}` | `require("lazy").load(opts)` | Load a plugin that has not been loaded yet. Similar to `:packadd`. Like `:Lazy load foo.nvim` |
496-
| `:Lazy log [plugins]` | `require("lazy").log(opts?)` | Show recent updates |
497-
| `:Lazy profile` | `require("lazy").profile()` | Show detailed profiling |
498-
| `:Lazy restore [plugins]` | `require("lazy").restore(opts?)` | Updates all plugins to the state in the lockfile |
499-
| `:Lazy sync [plugins]` | `require("lazy").sync(opts?)` | Run install, clean and update |
500-
| `:Lazy update [plugins]` | `require("lazy").update(opts?)` | Update plugins. This will also update the lockfile |
486+
| Command | Lua | Description |
487+
| ------------------------- | -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
488+
| `:Lazy check [plugins]` | `require("lazy").check(opts?)` | Check for updates and show the log (git fetch) |
489+
| `:Lazy clean [plugins]` | `require("lazy").clean(opts?)` | Clean plugins that are no longer needed |
490+
| `:Lazy clear` | `require("lazy").clear()` | Clear finished tasks |
491+
| `:Lazy debug` | `require("lazy").debug()` | Show debug information |
492+
| `:Lazy help` | `require("lazy").help()` | Toggle this help page |
493+
| `:Lazy home` | `require("lazy").home()` | Go back to plugin list |
494+
| `:Lazy install [plugins]` | `require("lazy").install(opts?)` | Install missing plugins |
495+
| `:Lazy load {plugins}` | `require("lazy").load(opts)` | Load a plugin that has not been loaded yet. Similar to `:packadd`. Like `:Lazy load foo.nvim` |
496+
| `:Lazy log [plugins]` | `require("lazy").log(opts?)` | Show recent updates |
497+
| `:Lazy profile` | `require("lazy").profile()` | Show detailed profiling |
498+
| `:Lazy restore [plugins]` | `require("lazy").restore(opts?)` | Updates all plugins to the state in the lockfile. For a single plugin: restore it to the state in the lockfile or to a given commit under the cursor |
499+
| `:Lazy sync [plugins]` | `require("lazy").sync(opts?)` | Run install, clean and update |
500+
| `:Lazy update [plugins]` | `require("lazy").update(opts?)` | Update plugins. This will also update the lockfile |
501501

502502
<!-- commands:end -->
503503

lua/lazy/view/config.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ M.commands = {
9696
},
9797
restore = {
9898
button = true,
99-
desc = "Updates all plugins to the state in the lockfile",
100-
desc_plugin = "Restore a plugin to the state in the lockfile",
99+
desc = "Updates all plugins to the state in the lockfile. For a single plugin: restore it to the state in the lockfile or to a given commit under the cursor",
100+
desc_plugin = "Restore a plugin to the state in the lockfile or to a given commit under the cursor",
101101
id = 8,
102102
key = "R",
103103
key_plugin = "r",

lua/lazy/view/init.lua

+20-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ function M:setup_patterns()
164164
self:diff({ commit = hash })
165165
end,
166166
}, self.diff)
167+
self:on_pattern(ViewConfig.commands.restore.key_plugin, {
168+
[commit_pattern] = function(hash)
169+
self:restore({ commit = hash })
170+
end,
171+
}, self.restore)
172+
end
173+
174+
---@param opts? {commit:string}
175+
function M:restore(opts)
176+
opts = opts or {}
177+
local Lockfile = require("lazy.manage.lock")
178+
local Commands = require("lazy.view.commands")
179+
local plugin = self.render:get_plugin()
180+
if plugin then
181+
if opts.commit then
182+
Lockfile.get(plugin).commit = opts.commit
183+
end
184+
Commands.cmd("restore", { plugins = { plugin } })
185+
end
167186
end
168187

169188
function M:hover()
@@ -246,7 +265,7 @@ function M:setup_modes()
246265
Commands.cmd(name)
247266
end, m.desc)
248267
end
249-
if m.key_plugin then
268+
if m.key_plugin and name ~= "restore" then
250269
self:on_key(m.key_plugin, function()
251270
local plugin = self.render:get_plugin()
252271
if plugin then

0 commit comments

Comments
 (0)