From 615781aebfc0230669a2e5750cba3c65f0b8a90e Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 8 Jan 2023 08:32:03 +0100 Subject: [PATCH] feat(git): lazy now detects origin changes and will fix it on update. Fixes #346. Fixes #331 --- lua/lazy/manage/init.lua | 7 ++++++- lua/lazy/manage/task/git.lua | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lua/lazy/manage/init.lua b/lua/lazy/manage/init.lua index 375f445..d14a7b8 100644 --- a/lua/lazy/manage/init.lua +++ b/lua/lazy/manage/init.lua @@ -94,6 +94,7 @@ function M.update(opts) opts = M.opts(opts, { mode = "update" }) return M.run({ pipeline = { + "git.origin", "git.branch", "git.fetch", { "git.checkout", lockfile = opts.lockfile }, @@ -123,6 +124,7 @@ function M.check(opts) opts = opts or {} return M.run({ pipeline = { + { "git.origin", check = true }, "git.fetch", "wait", { "git.log", check = true }, @@ -137,7 +139,10 @@ end function M.log(opts) opts = M.opts(opts, { mode = "log" }) return M.run({ - pipeline = { "git.log" }, + pipeline = { + { "git.origin", check = true }, + "git.log", + }, plugins = function(plugin) return plugin.url and plugin._.installed end, diff --git a/lua/lazy/manage/task/git.lua b/lua/lazy/manage/task/git.lua index 57388a6..c314c90 100644 --- a/lua/lazy/manage/task/git.lua +++ b/lua/lazy/manage/task/git.lua @@ -113,6 +113,30 @@ M.branch = { end, } +-- check and switch origin +M.origin = { + skip = function(plugin) + if not plugin._.installed or plugin._.is_local then + return true + end + local origin = Git.get_origin(plugin.dir) + return origin == plugin.url + end, + ---@param opts {check?:boolean} + run = function(self, opts) + if opts.check then + local origin = Git.get_origin(self.plugin.dir) + self.error = "Origin has changed:\n" + self.error = self.error .. " * old: " .. origin .. "\n" + self.error = self.error .. " * new: " .. self.plugin.url .. "\n" + self.error = self.error .. "Please run update to fix" + return + end + require("lazy.manage.task.fs").clean.run(self, opts) + M.clone.run(self, opts) + end, +} + -- fetches all needed origin branches M.fetch = { skip = function(plugin)