Skip to content

Commit 2abdc68

Browse files
committed
feat: new git module to work with branches, tags & versions
1 parent 073b5e3 commit 2abdc68

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

Diff for: lua/lazy/manage/git.lua

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Diff for: lua/lazy/manage/task/git.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Util = require("lazy.util")
2+
local Git = require("lazy.manage.git")
23

34
---@type table<string, LazyTaskDef>
45
local M = {}
@@ -51,17 +52,17 @@ M.update = {
5152
"--update-shallow",
5253
"--progress",
5354
}
54-
local git = assert(Util.git_info(self.plugin.dir))
55+
local git = assert(Git.info(self.plugin.dir))
5556

5657
self:spawn("git", {
5758
args = args,
5859
cwd = self.plugin.dir,
5960
on_exit = function(ok)
6061
if ok then
61-
local git_new = assert(Util.git_info(self.plugin.dir))
62+
local git_new = assert(Git.info(self.plugin.dir))
6263
self.plugin._.updated = {
63-
from = git.hash,
64-
to = git_new.hash,
64+
from = git.commit,
65+
to = git_new.commit,
6566
}
6667
self.plugin._.dirty = not vim.deep_equal(git, git_new)
6768
end

Diff for: lua/lazy/view/render.lua

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local Util = require("lazy.util")
33
local Sections = require("lazy.view.sections")
44
local Handler = require("lazy.core.handler")
55
local Plugin = require("lazy.core.plugin")
6+
local Git = require("lazy.manage.git")
67

78
local Text = require("lazy.view.text")
89

@@ -294,10 +295,18 @@ function M:details(plugin)
294295
---@type string[][]
295296
local props = {}
296297
table.insert(props, { "uri", (plugin.uri:gsub("%.git$", "")), "@text.reference" })
297-
local git = Util.git_info(plugin.dir)
298+
local git = Git.info(plugin.dir, true)
298299
if git then
299-
table.insert(props, { "commit", git.hash:sub(1, 7), "LazyCommit" })
300-
table.insert(props, { "branch", git.branch })
300+
if git.version then
301+
table.insert(props, { "version", tostring(git.version) })
302+
end
303+
if git.tag then
304+
table.insert(props, { "tag", git.tag })
305+
end
306+
if git.branch then
307+
table.insert(props, { "branch", git.branch })
308+
end
309+
table.insert(props, { "commit", git.commit:sub(1, 7), "LazyCommit" })
301310
end
302311
if Util.file_exists(plugin.dir .. "/README.md") then
303312
table.insert(props, { "readme", "README.md" })

0 commit comments

Comments
 (0)