2022-11-28 18:36:12 +08:00
|
|
|
local Util = require("lazy.util")
|
|
|
|
local Semver = require("lazy.manage.semver")
|
2022-12-01 06:12:00 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-28 18:36:12 +08:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2022-11-29 05:03:44 +08:00
|
|
|
---@alias GitInfo {branch?:string, commit?:string, tag?:string, version?:Semver}
|
|
|
|
|
2022-11-28 18:36:12 +08:00
|
|
|
---@param details? boolean
|
2022-11-29 05:03:44 +08:00
|
|
|
---@return GitInfo?
|
2022-11-28 18:36:12 +08:00
|
|
|
function M.info(repo, details)
|
|
|
|
local line = Util.head(repo .. "/.git/HEAD")
|
|
|
|
if line then
|
|
|
|
---@type string, string
|
|
|
|
local ref, branch = line:match("ref: (refs/heads/(.*))")
|
|
|
|
local ret = ref and {
|
|
|
|
branch = branch,
|
|
|
|
commit = Util.head(repo .. "/.git/" .. ref),
|
|
|
|
} or { commit = line }
|
|
|
|
|
|
|
|
if details then
|
|
|
|
Util.ls(repo .. "/.git/refs/tags", function(_, name)
|
|
|
|
if M.ref(repo, "tags/" .. name) == ret.commit then
|
|
|
|
ret.tag = name
|
|
|
|
ret.version = Semver.version(name)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@class TaggedSemver: Semver
|
|
|
|
---@field tag string
|
|
|
|
|
|
|
|
---@param spec? string
|
|
|
|
function M.get_versions(repo, spec)
|
|
|
|
local range = Semver.range(spec or "*")
|
|
|
|
---@type TaggedSemver[]
|
|
|
|
local versions = {}
|
|
|
|
Util.ls(repo .. "/.git/refs/tags", function(_, name)
|
|
|
|
local v = Semver.version(name)
|
|
|
|
---@cast v TaggedSemver
|
|
|
|
if v and range:matches(v) then
|
|
|
|
v.tag = name
|
|
|
|
table.insert(versions, v)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
return versions
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2022-12-02 18:26:07 +08:00
|
|
|
---@return string?
|
2022-11-28 18:36:12 +08:00
|
|
|
function M.get_branch(plugin)
|
|
|
|
if plugin.branch then
|
2022-12-02 18:26:07 +08:00
|
|
|
return plugin.branch
|
2022-11-28 18:36:12 +08:00
|
|
|
else
|
2022-12-02 18:26:07 +08:00
|
|
|
-- we need to return the default branch
|
|
|
|
-- Try origin first
|
2022-11-28 18:36:12 +08:00
|
|
|
local main = M.ref(plugin.dir, "remotes/origin/HEAD")
|
|
|
|
if main then
|
|
|
|
local branch = main:match("ref: refs/remotes/origin/(.*)")
|
|
|
|
if branch then
|
2022-12-02 18:26:07 +08:00
|
|
|
return branch
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
|
|
|
end
|
2022-12-02 18:26:07 +08:00
|
|
|
|
|
|
|
-- fallback to local HEAD
|
|
|
|
main = assert(Util.head(plugin.dir .. "/.git/HEAD"))
|
|
|
|
return main and main:match("ref: refs/heads/(.*)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return the last commit for the given branch
|
|
|
|
---@param repo string
|
|
|
|
---@param branch string
|
|
|
|
---@param origin? boolean
|
|
|
|
function M.get_commit(repo, branch, origin)
|
|
|
|
if origin then
|
|
|
|
-- origin ref might not exist if it is the same as local
|
|
|
|
return M.ref(repo, "remotes/origin", branch) or M.ref(repo, "heads", branch)
|
|
|
|
else
|
|
|
|
return M.ref(repo, "heads", branch)
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
2022-11-29 05:03:44 +08:00
|
|
|
---@return GitInfo?
|
2022-11-28 18:36:12 +08:00
|
|
|
function M.get_target(plugin)
|
2022-12-02 18:26:07 +08:00
|
|
|
local branch = assert(M.get_branch(plugin))
|
2022-11-28 18:36:12 +08:00
|
|
|
|
|
|
|
if plugin.commit then
|
2022-11-29 05:03:44 +08:00
|
|
|
return {
|
2022-12-02 18:26:07 +08:00
|
|
|
branch = branch,
|
2022-11-29 05:03:44 +08:00
|
|
|
commit = plugin.commit,
|
|
|
|
}
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
|
|
|
if plugin.tag then
|
2022-11-29 05:03:44 +08:00
|
|
|
return {
|
2022-12-02 18:26:07 +08:00
|
|
|
branch = branch,
|
2022-11-29 05:03:44 +08:00
|
|
|
tag = plugin.tag,
|
|
|
|
commit = M.ref(plugin.dir, "tags/" .. plugin.tag),
|
|
|
|
}
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
2022-12-01 06:12:00 +08:00
|
|
|
local version = plugin.version or Config.options.defaults.version
|
|
|
|
if version then
|
|
|
|
local last = Semver.last(M.get_versions(plugin.dir, version))
|
2022-11-28 18:36:12 +08:00
|
|
|
if last then
|
2022-11-29 05:03:44 +08:00
|
|
|
return {
|
2022-12-02 18:26:07 +08:00
|
|
|
branch = branch,
|
2022-11-29 05:03:44 +08:00
|
|
|
version = last,
|
|
|
|
tag = last.tag,
|
|
|
|
commit = M.ref(plugin.dir, "tags/" .. last.tag),
|
|
|
|
}
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
|
|
|
end
|
2022-11-29 05:03:44 +08:00
|
|
|
---@diagnostic disable-next-line: return-type-mismatch
|
2022-12-02 18:26:07 +08:00
|
|
|
return { branch = branch, commit = M.get_commit(plugin.dir, branch, true) }
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
|
|
|
|
2022-12-02 18:26:07 +08:00
|
|
|
function M.ref(repo, ...)
|
|
|
|
return Util.head(repo .. "/.git/refs/" .. table.concat({ ... }, "/"))
|
2022-11-28 18:36:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|