Skip to content

Commit ae379a6

Browse files
committedDec 2, 2022
fix(git): fixed branch detection, get target commit from origin and always checkout a tag or commit so we dont need to use git merge
1 parent 3e143c6 commit ae379a6

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed
 

‎lua/lazy/manage/git.lua

+29-16
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,54 @@ function M.get_versions(repo, spec)
5151
end
5252

5353
---@param plugin LazyPlugin
54-
---@return {branch:string, commit?:string}?
54+
---@return string?
5555
function M.get_branch(plugin)
5656
if plugin.branch then
57-
return {
58-
branch = plugin.branch,
59-
commit = M.ref(plugin.dir, "heads/" .. plugin.branch),
60-
}
57+
return plugin.branch
6158
else
59+
-- we need to return the default branch
60+
-- Try origin first
6261
local main = M.ref(plugin.dir, "remotes/origin/HEAD")
6362
if main then
6463
local branch = main:match("ref: refs/remotes/origin/(.*)")
6564
if branch then
66-
return {
67-
branch = branch,
68-
commit = M.ref(plugin.dir, "heads/" .. branch),
69-
}
65+
return branch
7066
end
7167
end
68+
69+
-- fallback to local HEAD
70+
main = assert(Util.head(plugin.dir .. "/.git/HEAD"))
71+
return main and main:match("ref: refs/heads/(.*)")
72+
end
73+
end
74+
75+
-- Return the last commit for the given branch
76+
---@param repo string
77+
---@param branch string
78+
---@param origin? boolean
79+
function M.get_commit(repo, branch, origin)
80+
if origin then
81+
-- origin ref might not exist if it is the same as local
82+
return M.ref(repo, "remotes/origin", branch) or M.ref(repo, "heads", branch)
83+
else
84+
return M.ref(repo, "heads", branch)
7285
end
7386
end
7487

7588
---@param plugin LazyPlugin
7689
---@return GitInfo?
7790
function M.get_target(plugin)
78-
local branch = M.get_branch(plugin) or M.info(plugin.dir)
91+
local branch = assert(M.get_branch(plugin))
7992

8093
if plugin.commit then
8194
return {
82-
branch = branch and branch.branch,
95+
branch = branch,
8396
commit = plugin.commit,
8497
}
8598
end
8699
if plugin.tag then
87100
return {
88-
branch = branch and branch.branch,
101+
branch = branch,
89102
tag = plugin.tag,
90103
commit = M.ref(plugin.dir, "tags/" .. plugin.tag),
91104
}
@@ -95,19 +108,19 @@ function M.get_target(plugin)
95108
local last = Semver.last(M.get_versions(plugin.dir, version))
96109
if last then
97110
return {
98-
branch = branch and branch.branch,
111+
branch = branch,
99112
version = last,
100113
tag = last.tag,
101114
commit = M.ref(plugin.dir, "tags/" .. last.tag),
102115
}
103116
end
104117
end
105118
---@diagnostic disable-next-line: return-type-mismatch
106-
return branch
119+
return { branch = branch, commit = M.get_commit(plugin.dir, branch, true) }
107120
end
108121

109-
function M.ref(repo, ref)
110-
return Util.head(repo .. "/.git/refs/" .. ref)
122+
function M.ref(repo, ...)
123+
return Util.head(repo .. "/.git/refs/" .. table.concat({ ... }, "/"))
111124
end
112125

113126
return M

‎lua/lazy/manage/lock.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ function M.update()
2929
if not plugin._.is_local and plugin._.installed then
3030
local info = assert(Git.info(plugin.dir))
3131
if not info.branch then
32-
local branch = assert(Git.get_branch(plugin))
33-
info.branch = branch.branch
32+
info.branch = assert(Git.get_branch(plugin))
3433
end
3534
info.commit = info.commit
3635
-- f:write(([[ [%q] = { branch = %q, commit = %q },]]):format(name, info.branch, info.commit) .. "\n")

‎lua/lazy/manage/task/git.lua

+14-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ M.log = {
3030
elseif opts.check then
3131
local info = assert(Git.info(self.plugin.dir))
3232
local target = assert(Git.get_target(self.plugin))
33+
assert(target.commit, self.plugin.name .. " " .. target.branch)
3334
table.insert(args, info.commit .. ".." .. target.commit)
3435
else
3536
vim.list_extend(args, opts.args or Config.options.git.log)
@@ -76,22 +77,23 @@ M.clone = {
7677
end,
7778
}
7879

80+
-- setup origin branches if needed
81+
-- fetch will retrieve the data
7982
M.branch = {
8083
skip = function(plugin)
8184
if not plugin._.installed or plugin._.is_local then
8285
return true
8386
end
8487
local branch = assert(Git.get_branch(plugin))
85-
return branch and branch.commit
88+
return Git.get_commit(plugin.dir, branch)
8689
end,
8790
run = function(self)
88-
local branch = assert(Git.get_branch(self.plugin))
8991
local args = {
9092
"remote",
9193
"set-branches",
9294
"--add",
9395
"origin",
94-
branch.branch,
96+
assert(Git.get_branch(self.plugin)),
9597
}
9698

9799
self:spawn("git", {
@@ -101,6 +103,7 @@ M.branch = {
101103
end,
102104
}
103105

106+
-- fetches all needed origin branches
104107
M.fetch = {
105108
skip = function(plugin)
106109
return not plugin._.installed or plugin._.is_local
@@ -121,6 +124,8 @@ M.fetch = {
121124
end,
122125
}
123126

127+
-- checkout to the target commit
128+
-- branches will exists at this point, so so will the commit
124129
M.checkout = {
125130
skip = function(plugin)
126131
return not plugin._.installed or plugin._.is_local
@@ -131,21 +136,24 @@ M.checkout = {
131136
local info = assert(Git.info(self.plugin.dir))
132137
local target = assert(Git.get_target(self.plugin))
133138

134-
-- if the plugin is locked and we did not just clone it,
139+
-- if the plugin is pinned and we did not just clone it,
135140
-- then don't update
136141
if self.plugin.pin and not self.plugin._.cloned then
137142
target = info
138143
end
139144

140145
local lock
141146
if opts.lockfile then
147+
-- restore to the lock if it exists
142148
lock = Lock.get(self.plugin)
143149
if lock then
144150
---@diagnostic disable-next-line: cast-local-type
145151
target = lock
146152
end
147153
end
148154

155+
-- dont run checkout if target is already reached.
156+
-- unless we just clones, since then we won't have any data yet
149157
if not self.plugin._.cloned and info.commit == target.commit and info.branch == target.branch then
150158
self.plugin._.updated = {
151159
from = info.commit,
@@ -165,8 +173,8 @@ M.checkout = {
165173
table.insert(args, "tags/" .. target.tag)
166174
elseif self.plugin.commit then
167175
table.insert(args, self.plugin.commit)
168-
elseif target.branch then
169-
table.insert(args, target.branch)
176+
else
177+
table.insert(args, target.commit)
170178
end
171179

172180
self:spawn("git", {

0 commit comments

Comments
 (0)
Please sign in to comment.