feat(pack): vim.pack.get() gets VCS info #35631

Problem:
Force resolve `spec.version` overrides the information about whether
a user supplied `version` or not. Knowing it might be useful in some use
cases (like comparing to previously set `spec` to detect if it has
changed).

Solution:
Do not resolve `spec.version`. This also improves speed when triggering
events and calling `get()`.
- Place default branch first when listing all branches.
- Use correct terminology in `get_hash` helper.
- Do not return `{ '' }` if there are no tags.

Problem:
There is no way to get more information about installed plugins, like
current revision or default branch (necessary if resolving default
`spec.version` manually). As computing Git data migth take some time,
also allow `get()` to limit output to only necessary set of plugins.

Solution:
- introduce arguments to `get(names, opts)`, which follows other
  `vim.pack` functions. Plugin extra info is returned by default and
  should be opt-out via `opts.info = false`.
  - Examples:
    - Get current revision: `get({ 'plug-name' })[1].rev`
    - Get default branch: `get({ 'plug_name' })[1].branches[1]`
- `update()` and `del()` act on plugins in the same order their names
  are supplied. This is less surprising.
- default `opts.info` to `true` since this simplifies logic for the
  common user, while still leaving the door open for a faster `get()` if
  needed.
This commit is contained in:
Evgeni Chasnovski
2025-09-07 19:59:31 +03:00
committed by GitHub
parent fd59e72b47
commit 7853cde29a
3 changed files with 170 additions and 104 deletions

View File

@@ -465,7 +465,7 @@ describe('vim.pack', function()
watch_events({ 'PackChangedPre', 'PackChanged' })
exec_lua(function()
-- Should provide event-data respecting manual and inferred default `version`
-- Should provide event-data respecting manual `version` without inferring default
vim.pack.add({ { src = repos_src.basic, version = 'feat-branch' }, repos_src.defbranch })
end)
@@ -473,11 +473,11 @@ describe('vim.pack', function()
local installpre_basic = find_in_log(log, 'PackChangedPre', 'install', 'basic', 'feat-branch')
local installpre_defbranch = find_in_log(log, 'PackChangedPre', 'install', 'defbranch', nil)
local updatepre_basic = find_in_log(log, 'PackChangedPre', 'update', 'basic', 'feat-branch')
local updatepre_defbranch = find_in_log(log, 'PackChangedPre', 'update', 'defbranch', 'dev')
local updatepre_defbranch = find_in_log(log, 'PackChangedPre', 'update', 'defbranch', nil)
local update_basic = find_in_log(log, 'PackChanged', 'update', 'basic', 'feat-branch')
local update_defbranch = find_in_log(log, 'PackChanged', 'update', 'defbranch', 'dev')
local update_defbranch = find_in_log(log, 'PackChanged', 'update', 'defbranch', nil)
local install_basic = find_in_log(log, 'PackChanged', 'install', 'basic', 'feat-branch')
local install_defbranch = find_in_log(log, 'PackChanged', 'install', 'defbranch', 'dev')
local install_defbranch = find_in_log(log, 'PackChanged', 'install', 'defbranch', nil)
eq(8, #log)
-- NOTE: There is no guaranteed installation order among separate plugins (as it is async)
@@ -571,11 +571,9 @@ describe('vim.pack', function()
eq({}, n.exec_lua('return { vim.g._plugin, vim.g._after_plugin }'))
-- Plugins should still be marked as "active", since they were added
plugindirs_data.spec.version = 'main'
plugindirs_data.active = true
basic_data.spec.version = 'main'
basic_data.active = true
eq({ plugindirs_data, basic_data }, n.exec_lua('return vim.pack.get()'))
eq({ plugindirs_data, basic_data }, exec_lua('return vim.pack.get(nil, { info = false })'))
end
-- Works on initial install
@@ -623,7 +621,8 @@ describe('vim.pack', function()
'`basic`:\n',
-- Should report available branches and tags if revision is absent
'`wrong%-version`',
'Available:\nTags: some%-tag\nBranches: feat%-branch, main',
-- Should list default branch first
'Available:\nTags: some%-tag\nBranches: main, feat%-branch',
-- Should report available branches and versions if no constraint match
'`semver`',
'Available:\nVersions: v1%.0%.0, v0%.4, 0%.3%.1, v0%.3%.0.*\nBranches: main\n',
@@ -1143,8 +1142,8 @@ describe('vim.pack', function()
-- Should trigger relevant events only for actually updated plugins
n.exec('write')
local log = exec_lua('return _G.event_log')
eq(1, find_in_log(log, 'PackChangedPre', 'update', 'fetch', 'main'))
eq(2, find_in_log(log, 'PackChanged', 'update', 'fetch', 'main'))
eq(1, find_in_log(log, 'PackChangedPre', 'update', 'fetch', nil))
eq(2, find_in_log(log, 'PackChanged', 'update', 'fetch', nil))
eq(2, #log)
end)
@@ -1180,7 +1179,7 @@ describe('vim.pack', function()
vim.pack.add({ repos_src.basic })
end)
validate('The following plugins are not installed: aaa, ccc', { 'aaa', 'basic', 'ccc' })
validate('Plugin `ccc` is not installed', { 'ccc', 'basic', 'aaa' })
-- Empty list is allowed with warning
n.exec('messages clear')
@@ -1192,39 +1191,73 @@ describe('vim.pack', function()
end)
describe('get()', function()
local basic_spec = { name = 'basic', src = repos_src.basic, version = 'main' }
local basic_path = pack_get_plug_path('basic')
local defbranch_spec = { name = 'defbranch', src = repos_src.defbranch, version = 'dev' }
local defbranch_path = pack_get_plug_path('defbranch')
local make_basic_data = function(active, info)
local spec = { name = 'basic', src = repos_src.basic, version = 'feat-branch' }
local path = pack_get_plug_path('basic')
local res = { active = active, path = path, spec = spec }
if info then
res.branches = { 'main', 'feat-branch' }
res.rev = git_get_hash('feat-branch', 'basic')
res.tags = { 'some-tag' }
end
return res
end
local make_defbranch_data = function(active, info)
local spec = { name = 'defbranch', src = repos_src.defbranch }
local path = pack_get_plug_path('defbranch')
local res = { active = active, path = path, spec = spec }
if info then
res.branches = { 'dev', 'main' }
res.rev = git_get_hash('dev', 'defbranch')
res.tags = {}
end
return res
end
it('returns list with necessary data', function()
local basic_data, defbranch_data
it('returns list of available plugins', function()
-- Should work just after installation
exec_lua(function()
vim.pack.add({ repos_src.defbranch, repos_src.basic })
vim.pack.add({ repos_src.defbranch, { src = repos_src.basic, version = 'feat-branch' } })
end)
eq({
-- Should preserve order in which plugins were `vim.pack.add()`ed
{ active = true, path = defbranch_path, spec = defbranch_spec },
{ active = true, path = basic_path, spec = basic_spec },
}, exec_lua('return vim.pack.get()'))
defbranch_data = make_defbranch_data(true, true)
basic_data = make_basic_data(true, true)
-- Should preserve order in which plugins were `vim.pack.add()`ed
eq({ defbranch_data, basic_data }, exec_lua('return vim.pack.get()'))
-- Should also list non-active plugins
n.clear()
exec_lua(function()
vim.pack.add({ repos_src.basic })
vim.pack.add({ { src = repos_src.basic, version = 'feat-branch' } })
end)
eq({
-- Should first list active, then non-active
{ active = true, path = basic_path, spec = basic_spec },
{ active = false, path = defbranch_path, spec = defbranch_spec },
}, exec_lua('return vim.pack.get()'))
defbranch_data = make_defbranch_data(false, true)
basic_data = make_basic_data(true, true)
-- Should first list active, then non-active
eq({ basic_data, defbranch_data }, exec_lua('return vim.pack.get()'))
-- Should respect `names` for both active and not active plugins
eq({ basic_data }, exec_lua('return vim.pack.get({ "basic" })'))
eq({ defbranch_data }, exec_lua('return vim.pack.get({ "defbranch" })'))
eq({ defbranch_data, basic_data }, exec_lua('return vim.pack.get({ "defbranch", "basic" })'))
local bad_get_cmd = 'return vim.pack.get({ "ccc", "basic", "aaa" })'
matches('Plugin `ccc` is not installed', pcall_err(exec_lua, bad_get_cmd))
-- Should respect `opts.info`
defbranch_data = make_defbranch_data(false, false)
basic_data = make_basic_data(true, false)
eq({ basic_data, defbranch_data }, exec_lua('return vim.pack.get(nil, { info = false })'))
eq({ basic_data }, exec_lua('return vim.pack.get({ "basic" }, { info = false })'))
eq({ defbranch_data }, exec_lua('return vim.pack.get({ "defbranch" }, { info = false })'))
end)
it('respects `data` field', function()
local out = exec_lua(function()
vim.pack.add({
{ src = repos_src.basic, data = { test = 'value' } },
{ src = repos_src.basic, version = 'feat-branch', data = { test = 'value' } },
{ src = repos_src.defbranch, data = 'value' },
})
local plugs = vim.pack.get()
@@ -1236,7 +1269,7 @@ describe('vim.pack', function()
it('works with `del()`', function()
exec_lua(function()
vim.pack.add({ repos_src.defbranch, repos_src.basic })
vim.pack.add({ repos_src.defbranch, { src = repos_src.basic, version = 'feat-branch' } })
end)
exec_lua(function()
@@ -1251,15 +1284,9 @@ describe('vim.pack', function()
-- Should not include removed plugins immediately after they are removed,
-- while still returning list without holes
exec_lua('vim.pack.del({ "defbranch" })')
eq({
{
{ active = true, path = defbranch_path, spec = defbranch_spec },
{ active = true, path = basic_path, spec = basic_spec },
},
{
{ active = true, path = basic_path, spec = basic_spec },
},
}, exec_lua('return _G.get_log'))
local defbranch_data = make_defbranch_data(true, true)
local basic_data = make_basic_data(true, true)
eq({ { defbranch_data, basic_data }, { basic_data } }, exec_lua('return _G.get_log'))
end)
end)
@@ -1281,16 +1308,16 @@ describe('vim.pack', function()
eq(false, pack_exists('plugindirs'))
eq(
"vim.pack: Removed plugin 'plugindirs'\nvim.pack: Removed plugin 'basic'",
"vim.pack: Removed plugin 'basic'\nvim.pack: Removed plugin 'plugindirs'",
n.exec_capture('messages')
)
-- Should trigger relevant events in order as specified in `vim.pack.add()`
local log = exec_lua('return _G.event_log')
eq(1, find_in_log(log, 'PackChangedPre', 'delete', 'plugindirs', 'main'))
eq(2, find_in_log(log, 'PackChanged', 'delete', 'plugindirs', 'main'))
eq(3, find_in_log(log, 'PackChangedPre', 'delete', 'basic', 'feat-branch'))
eq(4, find_in_log(log, 'PackChanged', 'delete', 'basic', 'feat-branch'))
eq(1, find_in_log(log, 'PackChangedPre', 'delete', 'basic', 'feat-branch'))
eq(2, find_in_log(log, 'PackChanged', 'delete', 'basic', 'feat-branch'))
eq(3, find_in_log(log, 'PackChangedPre', 'delete', 'plugindirs', nil))
eq(4, find_in_log(log, 'PackChanged', 'delete', 'plugindirs', nil))
eq(4, #log)
end)
@@ -1310,7 +1337,7 @@ describe('vim.pack', function()
vim.pack.add({ repos_src.basic })
end)
validate('The following plugins are not installed: aaa, ccc', { 'aaa', 'basic', 'ccc' })
validate('Plugin `ccc` is not installed', { 'ccc', 'basic', 'aaa' })
eq(true, pack_exists('basic'))
-- Empty list is allowed with warning