From 72c0dc9462ab3bf1a68198afabc1eb4e2940d299 Mon Sep 17 00:00:00 2001 From: Andre Toerien Date: Sun, 7 Jul 2024 17:13:49 +0200 Subject: [PATCH] fix(git): local plugin fixes (#1624) ## Description As I described in https://github.com/folke/lazy.nvim/pull/1512#issuecomment-2212474372, this makes it so that local plugins will only show as needing updates if the local branch is behind the upstream branch. This is done by checking the output of the `git log` command, and only setting `plugin._.updates` if the output is not empty. This seems to solve my issue where local plugins with unpushed changes always show as needing updates, but if there's a easier/better way of doing it then please feel free to edit/close this. Or if you don't agree that the current behaviour is a bug, then that's also fine - it's not a big deal and I can easily just ignore the "updates available" notice. I also came across a minor issue where the plugin diff view (press `d`) compares the wrong commits for local plugins, because [lua/lazy/view/init.lua](https://github.com/folke/lazy.nvim/blob/c771cf4928d1a1428ac7461658ab2916ed48adf5/lua/lazy/view/init.lua#L268) always uses `get_target`. I fixed this by moving `get_local_target` into `get_target` - I think this is simpler and more straightforward than the alternative of adding a ternary everywhere `get_target` is called. This second bugfix is a very small change, so I've just included it here, but I'm happy to make a second PR if you'd like. ## Related Issue(s) Related PR: #1512 --- lua/lazy/manage/checker.lua | 4 +++- lua/lazy/manage/git.lua | 15 ++++++--------- lua/lazy/manage/task/git.lua | 30 +++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lua/lazy/manage/checker.lua b/lua/lazy/manage/checker.lua index 8e03d9e..6efc5a7 100644 --- a/lua/lazy/manage/checker.lua +++ b/lua/lazy/manage/checker.lua @@ -35,7 +35,9 @@ end function M.fast_check(opts) opts = opts or {} for _, plugin in pairs(Config.plugins) do - if not plugin.pin and not plugin.dev and plugin._.installed then + -- don't check local plugins here, since we mark them as needing updates + -- only if local is behind upstream (if the git log task gives no output) + if plugin._.installed and not (plugin.pin or plugin._.is_local) then plugin._.updates = nil local info = Git.info(plugin.dir) local ok, target = pcall(Git.get_target, plugin) diff --git a/lua/lazy/manage/git.lua b/lua/lazy/manage/git.lua index 201e4e7..ef68cc7 100644 --- a/lua/lazy/manage/git.lua +++ b/lua/lazy/manage/git.lua @@ -116,6 +116,12 @@ end ---@param plugin LazyPlugin ---@return GitInfo? function M.get_target(plugin) + if plugin._.is_local then + local info = M.info(plugin.dir) + local branch = assert(info and info.branch or M.get_branch(plugin)) + return { branch = branch, commit = M.get_commit(plugin.dir, branch, true) } + end + local branch = assert(M.get_branch(plugin)) if plugin.commit then @@ -144,15 +150,6 @@ function M.get_target(plugin) } end end - ---@diagnostic disable-next-line: return-type-mismatch - return { branch = branch, commit = M.get_commit(plugin.dir, branch, true) } -end - ----@param plugin LazyPlugin ----@return GitInfo? -function M.get_local_target(plugin) - local info = M.info(plugin.dir) - local branch = assert(info and info.branch or M.get_branch(plugin)) return { branch = branch, commit = M.get_commit(plugin.dir, branch, true) } end diff --git a/lua/lazy/manage/task/git.lua b/lua/lazy/manage/task/git.lua index 8e0ef48..9425fec 100644 --- a/lua/lazy/manage/task/git.lua +++ b/lua/lazy/manage/task/git.lua @@ -32,11 +32,13 @@ M.log = { "--no-show-signature", } + local info, target + if opts.updated then table.insert(args, self.plugin._.updated.from .. ".." .. (self.plugin._.updated.to or "HEAD")) elseif opts.check then - local info = assert(Git.info(self.plugin.dir)) - local target = assert(self.plugin._.is_local and Git.get_local_target(self.plugin) or Git.get_target(self.plugin)) + info = assert(Git.info(self.plugin.dir)) + target = assert(Git.get_target(self.plugin)) if not target.commit then for k, v in pairs(target) do error(k .. " '" .. v .. "' not found") @@ -44,15 +46,17 @@ M.log = { error("no target commit found") end assert(target.commit, self.plugin.name .. " " .. target.branch) - if Git.eq(info, target) then - if Config.options.checker.check_pinned then - local last_commit = Git.get_commit(self.plugin.dir, target.branch, true) - if not Git.eq(info, { commit = last_commit }) then - self.plugin._.outdated = true + if not self.plugin._.is_local then + if Git.eq(info, target) then + if Config.options.checker.check_pinned then + local last_commit = Git.get_commit(self.plugin.dir, target.branch, true) + if not Git.eq(info, { commit = last_commit }) then + self.plugin._.outdated = true + end end + else + self.plugin._.updates = { from = info, to = target } end - else - self.plugin._.updates = { from = info, to = target } end table.insert(args, info.commit .. ".." .. target.commit) else @@ -63,6 +67,14 @@ M.log = { args = args, cwd = self.plugin.dir, }) + + -- for local plugins, mark as needing updates only if local is + -- behind upstream, i.e. if git log gave no output + if opts.check and self.plugin._.is_local then + if not vim.tbl_isempty(self:get_log()) then + self.plugin._.updates = { from = info, to = target } + end + end end, }