-
-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Support package snapshot #298 #370
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
910b2db
feat(snapshot)
c3n21 daefee9
fix(snapshot)
c3n21 68c7212
fix(rollback)
c3n21 c1b6a25
fix(rollback): working
c3n21 6acfee3
modify(packer): packer.snapshot and packer.rollback use f-args
c3n21 f223a9b
refactor(packer)
c3n21 dced289
refactor: packer, snapshot, git
c3n21 71536aa
fix(git): revert_to
c3n21 3ea6b95
refactor(packer, snapshot)
c3n21 6832007
test(snapshot): fixed tests
c3n21 7b8aa42
test(snapshot): fix
c3n21 438c07a
doc(snapshot): update snapshot feature docs
c3n21 fd95c13
refactor(snapshot): rename PackerRollback, PackerDelete commands
c3n21 608cf8f
docs(snapshot): update commands name
c3n21 3554a8a
refactor(snapshot): add notify on snapshot/rollback complete
c3n21 2b198ff
refactor(snapshot): using JSON for snapshot files
c3n21 a32c180
fix(snapshot): automatically create stdpath('cache')/packer.nvim
c3n21 3183262
test(snapshot): WIP
c3n21 4e86269
fix(snapshot)
c3n21 0088f0f
test(snapshot): WIP
c3n21 26dbf38
test(snapshot): cleanup and fix
c3n21 2024edd
fix(snapshot): can't rollback if snapshot is older than repo
c3n21 63db854
Merge branch 'master' into snapshot
wbthomason abde04d
Update Neovim versions for testing
wbthomason e36f8ea
Attempt to fix snapshot tests by using proper async function
wbthomason 431a9dc
refactor(snapshot)
c3n21 85f5afe
refactor(snapshot)
c3n21 5ead3e4
chore: format with stylua
github-actions[bot] f840ff9
refactor(snapshot)
c3n21 c42345b
chore: format with stylua
github-actions[bot] 31c7cf2
refactor(snapshot)
c3n21 89d9b1a
refactor(snapshot)
c3n21 b4f45e7
refactor(snapshot): improved async logic of rollback
c3n21 4864933
build
c3n21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,21 @@ | ||
FROM archlinux | ||
RUN pacman -Syu --noconfirm && pacman -S --noconfirm git neovim python | ||
FROM archlinux:base-devel | ||
WORKDIR /setup | ||
RUN pacman -Sy git neovim python --noconfirm | ||
RUN useradd -m test | ||
|
||
USER test | ||
RUN git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim | ||
RUN mkdir -p /home/test/.cache/nvim/packer.nvim | ||
RUN touch /home/test/.cache/nvim/packer.nvim/test_completion{,1,2,3} | ||
|
||
USER test | ||
RUN mkdir -p /home/test/.local/share/nvim/site/pack/packer/start/packer.nvim/ | ||
WORKDIR /home/test/.local/share/nvim/site/pack/packer/start/packer.nvim/ | ||
COPY . ./ | ||
|
||
USER root | ||
RUN chmod 777 -R /home/test/.local/share/nvim/site/pack/packer/start/packer.nvim | ||
RUN touch /home/test/.cache/nvim/packer.nvim/not_writeable | ||
|
||
USER test | ||
ENTRYPOINT make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ local stdpath = vim.fn.stdpath | |
local packer = {} | ||
local config_defaults = { | ||
ensure_dependencies = true, | ||
snapshot = nil, | ||
snapshot_path = join_paths(stdpath 'cache', 'packer.nvim'), | ||
package_root = join_paths(stdpath 'data', 'site', 'pack'), | ||
compile_path = join_paths(stdpath 'config', 'plugin', 'packer_compiled.lua'), | ||
plugin_package = 'packer', | ||
|
@@ -38,6 +40,7 @@ local config_defaults = { | |
get_bodies = 'log --color=never --pretty=format:"===COMMIT_START===%h%n%s===BODY_START===%b" --no-show-signature HEAD@{1}...HEAD', | ||
submodules = 'submodule update --init --recursive --progress', | ||
revert = 'reset --hard HEAD@{1}', | ||
revert_to = 'reset --hard %s --', | ||
c3n21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tags_expand_fmt = 'tag -l %s --sort -version:refname', | ||
}, | ||
depth = 1, | ||
|
@@ -86,6 +89,7 @@ local configurable_modules = { | |
update = false, | ||
luarocks = false, | ||
log = false, | ||
snapshot = false, | ||
} | ||
|
||
local function require_and_configure(module_name) | ||
|
@@ -122,9 +126,16 @@ packer.init = function(user_config) | |
if not config.disable_commands then | ||
packer.make_commands() | ||
end | ||
|
||
if vim.fn.mkdir(config.snapshot_path, 'p') ~= 1 then | ||
vim.notify("Couldn't create " .. config.snapshot_path, vim.log.levels.WARN) | ||
end | ||
end | ||
|
||
packer.make_commands = function() | ||
vim.cmd [[command! -nargs=+ -complete=customlist,v:lua.require'packer.snapshot'.completion.create PackerSnapshot lua require('packer').snapshot(<f-args>)]] | ||
vim.cmd [[command! -nargs=+ -complete=customlist,v:lua.require'packer.snapshot'.completion.rollback PackerSnapshotRollback lua require('packer').rollback(<f-args>)]] | ||
vim.cmd [[command! -nargs=+ -complete=customlist,v:lua.require'packer.snapshot'.completion.snapshot PackerSnapshotDelete lua require('packer.snapshot').delete(<f-args>)]] | ||
vim.cmd [[command! -nargs=* -complete=customlist,v:lua.require'packer'.plugin_complete PackerInstall lua require('packer').install(<f-args>)]] | ||
vim.cmd [[command! -nargs=* -complete=customlist,v:lua.require'packer'.plugin_complete PackerUpdate lua require('packer').update(<f-args>)]] | ||
vim.cmd [[command! -nargs=* -complete=customlist,v:lua.require'packer'.plugin_complete PackerSync lua require('packer').sync(<f-args>)]] | ||
|
@@ -796,6 +807,136 @@ packer.plugin_complete = function(lead, _, _) | |
return completion_list | ||
end | ||
|
||
---Snapshots installed plugins | ||
---@param snapshot_name string absolute path or just a snapshot name | ||
packer.snapshot = function(snapshot_name, ...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: I think we should either print an error or choose a sane default if |
||
local async = require('packer.async').sync | ||
local await = require('packer.async').wait | ||
local snapshot = require 'packer.snapshot' | ||
local log = require_and_configure 'log' | ||
local args = { ... } | ||
snapshot_name = snapshot_name or require('os').date '%Y-%m-%d' | ||
local snapshot_path = vim.fn.expand(snapshot_name) | ||
|
||
local fmt = string.format | ||
log.debug(fmt('Taking snapshots of currently installed plugins to %s...', snapshot_name)) | ||
if vim.fn.fnamemodify(snapshot_name, ':p') ~= snapshot_path then -- is not absolute path | ||
if config.snapshot_path == nil then | ||
vim.notify('config.snapshot_path is not set', vim.log.levels.WARN) | ||
return | ||
else | ||
snapshot_path = util.join_paths(config.snapshot_path, snapshot_path) -- set to default path | ||
end | ||
end | ||
|
||
manage_all_plugins() | ||
|
||
local target_plugins = plugins | ||
if next(args) ~= nil then -- provided extra args | ||
target_plugins = vim.tbl_filter( -- filter plugins | ||
function(plugin) | ||
for k, plugin_shortname in pairs(args) do | ||
if plugin_shortname == plugin.short_name then | ||
args[k] = nil | ||
return true | ||
end | ||
end | ||
return false | ||
end, | ||
plugins | ||
) | ||
end | ||
|
||
local write_snapshot = true | ||
|
||
if vim.fn.filereadable(snapshot_path) == 1 then | ||
vim.ui.select( | ||
{ 'Replace', 'Cancel' }, | ||
{ prompt = fmt("Do you want to replace '%s'?", snapshot_path) }, | ||
function(_, idx) | ||
write_snapshot = idx == 1 | ||
end | ||
) | ||
end | ||
|
||
async(function() | ||
if write_snapshot then | ||
await(snapshot.create(snapshot_path, target_plugins)) | ||
:map_ok(function(ok) | ||
vim.notify(ok.message, vim.log.levels.INFO, { title = 'packer.nvim' }) | ||
|
||
if next(ok.failed) then | ||
vim.notify("Couldn't snapshot " .. vim.inspect(ok.failed), vim.log.levels.WARN, { title = 'packer.nvim' }) | ||
end | ||
end) | ||
:map_err(function(err) | ||
vim.notify(err.message, vim.log.levels.WARN, { title = 'packer.nvim' }) | ||
end) | ||
end | ||
end)() | ||
end | ||
|
||
---Instantly rolls back plugins to a previous state specified by `snapshot_name` | ||
---If `snapshot_name` doesn't exist an error will be displayed | ||
c3n21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
---@param snapshot_name string @name of the snapshot or the absolute path to the snapshot | ||
---@vararg string @ if provided, the only plugins to be rolled back, | ||
---otherwise all the plugins will be rolled back | ||
packer.rollback = function(snapshot_name, ...) | ||
local args = { ... } | ||
local a = require 'packer.async' | ||
local async = a.sync | ||
local await = a.wait | ||
local wait_all = a.wait_all | ||
local snapshot = require 'packer.snapshot' | ||
local log = require_and_configure 'log' | ||
local fmt = string.format | ||
|
||
async(function() | ||
manage_all_plugins() | ||
|
||
local snapshot_path = vim.loop.fs_realpath(util.join_paths(config.snapshot_path, snapshot_name)) | ||
or vim.loop.fs_realpath(snapshot_name) | ||
|
||
if snapshot_path == nil then | ||
local warn = fmt("Snapshot '%s' is wrong or doesn't exist", snapshot_name) | ||
log.warn(warn) | ||
vim.notify(warn, vim.log.levels.WARN) | ||
return | ||
end | ||
|
||
local target_plugins = plugins | ||
|
||
if next(args) ~= nil then -- provided extra args | ||
target_plugins = vim.tbl_filter(function(plugin) | ||
for _, plugin_sname in pairs(args) do | ||
if plugin_sname == plugin.short_name then | ||
return true | ||
end | ||
end | ||
return false | ||
end, plugins) | ||
end | ||
|
||
await(snapshot.rollback(snapshot_path, target_plugins)) | ||
:map_ok(function (ok) | ||
await(a.main) | ||
vim.notify('Rollback to "' .. snapshot_path .. '" completed', vim.log.levels.INFO, { title = 'packer.nvim' }) | ||
if next(ok.failed) then | ||
vim.notify( | ||
"Couldn't rollback " .. vim.inspect(ok.failed), | ||
vim.log.levels.INFO, { title = 'packer.nvim' } | ||
) | ||
end | ||
end) | ||
:map_err(function (err) | ||
await(a.main) | ||
vim.notify(err, vim.log.levels.ERROR, { title = 'packer.nvim' }) | ||
end) | ||
|
||
packer.on_complete() | ||
c3n21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end)() | ||
end | ||
|
||
packer.config = config | ||
|
||
--- Convenience function for simple setup | ||
|
@@ -852,6 +993,11 @@ packer.startup = function(spec) | |
end | ||
end | ||
|
||
require_and_configure 'snapshot' -- initialize snapshot config | ||
if config.snapshot ~= nil then | ||
packer.rollback(config.snapshot) | ||
end | ||
|
||
return packer | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be fixed for the other commands too (not as part of this PR), but we might want to specify the arguments accepted for the snapshot related commands (i.e. the snapshot file).