feat(pack)!: update get() to return revision regardless of opts.info

Problem: The revision data is returned behind `opts.info` flag because
  it required extra Git calls. With lockfile it is not the case.

Solution: Use lockfile to always set `rev` field in output of `get()`.
This commit is contained in:
Evgeni Chasnovski
2025-10-04 16:13:38 +03:00
parent 2c0b70e559
commit a31f63f661
3 changed files with 14 additions and 13 deletions

View File

@@ -379,7 +379,7 @@ get({names}, {opts}) *vim.pack.get()*
• {branches}? (`string[]`) Available Git branches (first is default). • {branches}? (`string[]`) Available Git branches (first is default).
Missing if `info=false`. Missing if `info=false`.
• {path} (`string`) Plugin's path on disk. • {path} (`string`) Plugin's path on disk.
• {rev}? (`string`) Current Git revision. Missing if `info=false`. • {rev} (`string`) Current Git revision.
• {spec} (`vim.pack.SpecResolved`) A |vim.pack.Spec| with resolved • {spec} (`vim.pack.SpecResolved`) A |vim.pack.Spec| with resolved
`name`. `name`.
• {tags}? (`string[]`) Available Git tags. Missing if `info=false`. • {tags}? (`string[]`) Available Git tags. Missing if `info=false`.

View File

@@ -1080,7 +1080,7 @@ end
--- @field active boolean Whether plugin was added via |vim.pack.add()| to current session. --- @field active boolean Whether plugin was added via |vim.pack.add()| to current session.
--- @field branches? string[] Available Git branches (first is default). Missing if `info=false`. --- @field branches? string[] Available Git branches (first is default). Missing if `info=false`.
--- @field path string Plugin's path on disk. --- @field path string Plugin's path on disk.
--- @field rev? string Current Git revision. Missing if `info=false`. --- @field rev string Current Git revision.
--- @field spec vim.pack.SpecResolved A |vim.pack.Spec| with resolved `name`. --- @field spec vim.pack.SpecResolved A |vim.pack.Spec| with resolved `name`.
--- @field tags? string[] Available Git tags. Missing if `info=false`. --- @field tags? string[] Available Git tags. Missing if `info=false`.
@@ -1096,7 +1096,6 @@ local function add_p_data_info(p_data_list)
--- @async --- @async
funs[i] = function() funs[i] = function()
p_data.branches = git_get_branches(path) p_data.branches = git_get_branches(path)
p_data.rev = git_get_hash('HEAD', path)
p_data.tags = git_get_tags(path) p_data.tags = git_get_tags(path)
end end
end end
@@ -1127,8 +1126,11 @@ function M.get(names, opts)
local used_names = {} --- @type table<string,boolean> local used_names = {} --- @type table<string,boolean>
for i = 1, n_active_plugins do for i = 1, n_active_plugins do
if active[i] and (not names or vim.tbl_contains(names, active[i].spec.name)) then if active[i] and (not names or vim.tbl_contains(names, active[i].spec.name)) then
res[#res + 1] = { spec = vim.deepcopy(active[i].spec), path = active[i].path, active = true } local name = active[i].spec.name
used_names[active[i].spec.name] = true local spec = vim.deepcopy(active[i].spec)
local rev = (plugin_lock.plugins[name] or {}).rev
res[#res + 1] = { spec = spec, path = active[i].path, rev = rev, active = true }
used_names[name] = true
end end
end end
@@ -1138,7 +1140,7 @@ function M.get(names, opts)
local is_in_names = not names or vim.tbl_contains(names, name) local is_in_names = not names or vim.tbl_contains(names, name)
if not active_plugins[path] and is_in_names then if not active_plugins[path] and is_in_names then
local spec = { name = name, src = l_data.src, version = l_data.version } local spec = { name = name, src = l_data.src, version = l_data.version }
res[#res + 1] = { spec = spec, path = path, active = false } res[#res + 1] = { spec = spec, path = path, rev = l_data.rev, active = false }
used_names[name] = true used_names[name] = true
end end
end end

View File

@@ -686,9 +686,8 @@ describe('vim.pack', function()
eq({}, n.exec_lua('return { vim.g._plugin, vim.g._after_plugin }')) eq({}, n.exec_lua('return { vim.g._plugin, vim.g._after_plugin }'))
-- Plugins should still be marked as "active", since they were added -- Plugins should still be marked as "active", since they were added
plugindirs_data.active = true eq(true, exec_lua('return vim.pack.get({ "plugindirs" })[1].active'))
basic_data.active = true eq(true, exec_lua('return vim.pack.get({ "basic" })[1].active'))
eq({ plugindirs_data, basic_data }, exec_lua('return vim.pack.get(nil, { info = false })'))
end end
-- Works on initial install -- Works on initial install
@@ -1352,10 +1351,10 @@ describe('vim.pack', function()
local make_basic_data = function(active, info) local make_basic_data = function(active, info)
local spec = { name = 'basic', src = repos_src.basic, version = 'feat-branch' } local spec = { name = 'basic', src = repos_src.basic, version = 'feat-branch' }
local path = pack_get_plug_path('basic') local path = pack_get_plug_path('basic')
local res = { active = active, path = path, spec = spec } local rev = git_get_hash('feat-branch', 'basic')
local res = { active = active, path = path, spec = spec, rev = rev }
if info then if info then
res.branches = { 'main', 'feat-branch' } res.branches = { 'main', 'feat-branch' }
res.rev = git_get_hash('feat-branch', 'basic')
res.tags = { 'some-tag' } res.tags = { 'some-tag' }
end end
return res return res
@@ -1364,10 +1363,10 @@ describe('vim.pack', function()
local make_defbranch_data = function(active, info) local make_defbranch_data = function(active, info)
local spec = { name = 'defbranch', src = repos_src.defbranch } local spec = { name = 'defbranch', src = repos_src.defbranch }
local path = pack_get_plug_path('defbranch') local path = pack_get_plug_path('defbranch')
local res = { active = active, path = path, spec = spec } local rev = git_get_hash('dev', 'defbranch')
local res = { active = active, path = path, spec = spec, rev = rev }
if info then if info then
res.branches = { 'dev', 'main' } res.branches = { 'dev', 'main' }
res.rev = git_get_hash('dev', 'defbranch')
res.tags = {} res.tags = {}
end end
return res return res