|
| 1 | +local Util = require("lazy.util") |
| 2 | +local Semver = require("lazy.manage.semver") |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +---@param details? boolean |
| 7 | +function M.info(repo, details) |
| 8 | + local line = Util.head(repo .. "/.git/HEAD") |
| 9 | + if line then |
| 10 | + ---@type string, string |
| 11 | + local ref, branch = line:match("ref: (refs/heads/(.*))") |
| 12 | + local ret = ref and { |
| 13 | + branch = branch, |
| 14 | + commit = Util.head(repo .. "/.git/" .. ref), |
| 15 | + } or { commit = line } |
| 16 | + |
| 17 | + if details then |
| 18 | + Util.ls(repo .. "/.git/refs/tags", function(_, name) |
| 19 | + if M.ref(repo, "tags/" .. name) == ret.commit then |
| 20 | + ret.tag = name |
| 21 | + ret.version = Semver.version(name) |
| 22 | + return false |
| 23 | + end |
| 24 | + end) |
| 25 | + end |
| 26 | + |
| 27 | + return ret |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | +---@class TaggedSemver: Semver |
| 32 | +---@field tag string |
| 33 | + |
| 34 | +---@param spec? string |
| 35 | +function M.get_versions(repo, spec) |
| 36 | + local range = Semver.range(spec or "*") |
| 37 | + ---@type TaggedSemver[] |
| 38 | + local versions = {} |
| 39 | + Util.ls(repo .. "/.git/refs/tags", function(_, name) |
| 40 | + local v = Semver.version(name) |
| 41 | + ---@cast v TaggedSemver |
| 42 | + if v and range:matches(v) then |
| 43 | + v.tag = name |
| 44 | + table.insert(versions, v) |
| 45 | + end |
| 46 | + end) |
| 47 | + return versions |
| 48 | +end |
| 49 | + |
| 50 | +---@param plugin LazyPlugin |
| 51 | +function M.get_branch(plugin) |
| 52 | + if plugin.branch then |
| 53 | + return { |
| 54 | + branch = plugin.branch, |
| 55 | + commit = M.ref(plugin.dir, "heads/" .. plugin.branch), |
| 56 | + } |
| 57 | + else |
| 58 | + local main = M.ref(plugin.dir, "remotes/origin/HEAD") |
| 59 | + if main then |
| 60 | + local branch = main:match("ref: refs/remotes/origin/(.*)") |
| 61 | + if branch then |
| 62 | + return { |
| 63 | + branch = branch, |
| 64 | + commit = M.ref(plugin.dir, "heads/" .. branch), |
| 65 | + } |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +---@param plugin LazyPlugin |
| 72 | +function M.get_target(plugin) |
| 73 | + local branch = M.get_branch(plugin) |
| 74 | + |
| 75 | + if plugin.commit then |
| 76 | + return { branch = branch, commit = plugin.commit } |
| 77 | + end |
| 78 | + if plugin.tag then |
| 79 | + return { branch = branch, tag = plugin.tag, commit = M.ref(plugin.dir, "tags/" .. plugin.tag) } |
| 80 | + end |
| 81 | + if plugin.version then |
| 82 | + local last = Semver.last(M.get_versions(plugin.dir, plugin.version)) |
| 83 | + if last then |
| 84 | + return { branch = branch, version = last, tag = last.tag, commit = M.ref(plugin.dir, "tags/" .. last.tag) } |
| 85 | + end |
| 86 | + end |
| 87 | + return { branch = branch, commit = branch.commit } |
| 88 | +end |
| 89 | + |
| 90 | +function M.ref(repo, ref) |
| 91 | + return Util.head(repo .. "/.git/refs/" .. ref) |
| 92 | +end |
| 93 | + |
| 94 | +return M |
0 commit comments