Skip to content

Commit 86eaa11

Browse files
committed
fix(git): dereference tag refs. Fixes #54
1 parent e95da35 commit 86eaa11

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

lua/lazy/manage/git.lua

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
local Util = require("lazy.util")
22
local Semver = require("lazy.manage.semver")
33
local Config = require("lazy.core.config")
4+
local Process = require("lazy.manage.process")
45

56
local M = {}
67

78
---@alias GitInfo {branch?:string, commit?:string, tag?:string, version?:Semver}
89

9-
---@param details? boolean
10+
---@param repo string
11+
---@param details? boolean Fetching details is slow! Don't loop over a plugin to fetch all details!
1012
---@return GitInfo?
1113
function M.info(repo, details)
1214
local line = Util.head(repo .. "/.git/HEAD")
@@ -19,13 +21,13 @@ function M.info(repo, details)
1921
} or { commit = line }
2022

2123
if details then
22-
Util.ls(repo .. "/.git/refs/tags", function(_, name)
23-
if M.ref(repo, "tags/" .. name) == ret.commit then
24-
ret.tag = name
25-
ret.version = Semver.version(name)
26-
return false
24+
for tag, tag_ref in pairs(M.get_tag_refs(repo)) do
25+
if tag_ref == ret.commit then
26+
ret.tag = tag
27+
ret.version = Semver.version(tag)
28+
break
2729
end
28-
end)
30+
end
2931
end
3032
return ret
3133
end
@@ -120,7 +122,34 @@ function M.get_target(plugin)
120122
end
121123

122124
function M.ref(repo, ...)
123-
return Util.head(repo .. "/.git/refs/" .. table.concat({ ... }, "/"))
125+
local ref = table.concat({ ... }, "/")
126+
127+
-- if this is a tag ref, then dereference it instead
128+
if ref:find("tags/", 1, true) == 1 then
129+
local tags = M.get_tag_refs(repo, ref)
130+
for _, tag_ref in pairs(tags) do
131+
return tag_ref
132+
end
133+
end
134+
135+
-- otherwise just get the ref
136+
return Util.head(repo .. "/.git/refs/" .. ref)
137+
end
138+
139+
-- this is slow, so don't use on a loop over all plugins!
140+
---@param tagref string?
141+
function M.get_tag_refs(repo, tagref)
142+
tagref = tagref or "--tags"
143+
---@type table<string,string>
144+
local tags = {}
145+
local lines = Process.exec({ "git", "show-ref", "-d", tagref }, { cwd = repo })
146+
for _, line in ipairs(lines) do
147+
local ref, tag = line:match("^(%w+) refs/tags/([^%^]+)%^?{?}?$")
148+
if ref then
149+
tags[tag] = ref
150+
end
151+
end
152+
return tags
124153
end
125154

126155
return M

0 commit comments

Comments
 (0)