2022-11-28 23:15:13 +00:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Git = require("lazy.manage.git")
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2024-07-05 08:01:01 +01:00
|
|
|
---@alias LazyLockfile table<string, {commit:string, branch:string}>
|
|
|
|
---@type LazyLockfile
|
2022-11-28 23:15:13 +00:00
|
|
|
M.lock = {}
|
|
|
|
M._loaded = false
|
|
|
|
|
|
|
|
function M.update()
|
2024-07-05 08:01:01 +01:00
|
|
|
M.load()
|
2022-12-21 22:22:04 +00:00
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(Config.options.lockfile, ":p:h"), "p")
|
2023-10-09 15:17:42 +01:00
|
|
|
local f = assert(io.open(Config.options.lockfile, "wb"))
|
2022-11-28 23:15:13 +00:00
|
|
|
f:write("{\n")
|
|
|
|
|
2024-07-05 08:01:01 +01:00
|
|
|
-- keep disabled and cond plugins
|
|
|
|
for name in pairs(M.lock) do
|
|
|
|
if not (Config.spec.disabled[name] or Config.spec.ignore_installed[name]) then
|
|
|
|
M.lock[name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
|
|
|
if not plugin._.is_local and plugin._.installed then
|
|
|
|
local info = assert(Git.info(plugin.dir))
|
|
|
|
M.lock[plugin.name] = {
|
|
|
|
branch = info.branch or assert(Git.get_branch(plugin)),
|
|
|
|
commit = assert(info.commit, "commit is nil"),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2022-11-28 23:15:13 +00:00
|
|
|
|
|
|
|
---@type string[]
|
2024-07-05 08:01:01 +01:00
|
|
|
local names = vim.tbl_keys(M.lock)
|
2022-11-28 23:15:13 +00:00
|
|
|
table.sort(names)
|
|
|
|
|
|
|
|
for n, name in ipairs(names) do
|
2024-07-05 08:01:01 +01:00
|
|
|
local info = M.lock[name]
|
|
|
|
f:write(([[ %q: { "branch": %q, "commit": %q }]]):format(name, info.branch, info.commit))
|
|
|
|
if n ~= #names then
|
|
|
|
f:write(",\n")
|
2022-11-28 23:15:13 +00:00
|
|
|
end
|
|
|
|
end
|
2024-07-13 08:51:44 +01:00
|
|
|
f:write("\n}\n")
|
2022-11-28 23:15:13 +00:00
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.load()
|
2024-07-05 08:01:01 +01:00
|
|
|
if M._loaded then
|
|
|
|
return
|
|
|
|
end
|
2022-11-28 23:15:13 +00:00
|
|
|
M.lock = {}
|
|
|
|
M._loaded = true
|
|
|
|
local f = io.open(Config.options.lockfile, "r")
|
|
|
|
if f then
|
|
|
|
---@type string
|
|
|
|
local data = f:read("*a")
|
|
|
|
local ok, lock = pcall(vim.json.decode, data)
|
|
|
|
if ok then
|
|
|
|
M.lock = lock
|
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@return {commit:string, branch:string}
|
|
|
|
function M.get(plugin)
|
2024-07-05 08:01:01 +01:00
|
|
|
M.load()
|
2022-11-28 23:15:13 +00:00
|
|
|
return M.lock[plugin.name]
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|