Skip to content

Commit 615781a

Browse files
committed
feat(git): lazy now detects origin changes and will fix it on update. Fixes #346. Fixes #331
1 parent a39fa0f commit 615781a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lua/lazy/manage/init.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function M.update(opts)
9494
opts = M.opts(opts, { mode = "update" })
9595
return M.run({
9696
pipeline = {
97+
"git.origin",
9798
"git.branch",
9899
"git.fetch",
99100
{ "git.checkout", lockfile = opts.lockfile },
@@ -123,6 +124,7 @@ function M.check(opts)
123124
opts = opts or {}
124125
return M.run({
125126
pipeline = {
127+
{ "git.origin", check = true },
126128
"git.fetch",
127129
"wait",
128130
{ "git.log", check = true },
@@ -137,7 +139,10 @@ end
137139
function M.log(opts)
138140
opts = M.opts(opts, { mode = "log" })
139141
return M.run({
140-
pipeline = { "git.log" },
142+
pipeline = {
143+
{ "git.origin", check = true },
144+
"git.log",
145+
},
141146
plugins = function(plugin)
142147
return plugin.url and plugin._.installed
143148
end,

lua/lazy/manage/task/git.lua

+24
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ M.branch = {
113113
end,
114114
}
115115

116+
-- check and switch origin
117+
M.origin = {
118+
skip = function(plugin)
119+
if not plugin._.installed or plugin._.is_local then
120+
return true
121+
end
122+
local origin = Git.get_origin(plugin.dir)
123+
return origin == plugin.url
124+
end,
125+
---@param opts {check?:boolean}
126+
run = function(self, opts)
127+
if opts.check then
128+
local origin = Git.get_origin(self.plugin.dir)
129+
self.error = "Origin has changed:\n"
130+
self.error = self.error .. " * old: " .. origin .. "\n"
131+
self.error = self.error .. " * new: " .. self.plugin.url .. "\n"
132+
self.error = self.error .. "Please run update to fix"
133+
return
134+
end
135+
require("lazy.manage.task.fs").clean.run(self, opts)
136+
M.clone.run(self, opts)
137+
end,
138+
}
139+
116140
-- fetches all needed origin branches
117141
M.fetch = {
118142
skip = function(plugin)

0 commit comments

Comments
 (0)