From f3176eb294c974565157907b4327e4332e73f394 Mon Sep 17 00:00:00 2001 From: gwbeip Date: Sat, 30 May 2026 15:27:33 +0800 Subject: [PATCH] fix(pack): recover from broken HEAD repos during startup --- lua/plugins/pack.lua | 41 ++++++++++++++++++++++++++++++++++++++++- lua/profiles/native.lua | 6 +++++- lua/profiles/vscode.lua | 6 +++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lua/plugins/pack.lua b/lua/plugins/pack.lua index b0af988..3a4bdbe 100644 --- a/lua/plugins/pack.lua +++ b/lua/plugins/pack.lua @@ -1,5 +1,28 @@ local M = {} +local function has_valid_head(path) + if vim.fn.isdirectory(path .. "/.git") == 0 then + return true + end + + vim.fn.system({ "git", "-C", path, "rev-parse", "--verify", "HEAD" }) + return vim.v.shell_error == 0 +end + +local function prune_broken_pack_repos() + local root = vim.fn.stdpath("data") .. "/site/pack/core/opt" + if vim.fn.isdirectory(root) == 0 then + return + end + + for _, dir in ipairs(vim.fn.readdir(root)) do + local path = root .. "/" .. dir + if vim.fn.isdirectory(path) == 1 and not has_valid_head(path) then + vim.fn.delete(path, "rf") + end + end +end + local function to_src(value) if value:match("^[%w+.-]+://") or value:match("^git@") then return value @@ -21,7 +44,23 @@ function M.add(specs) end end - vim.pack.add(resolved, { confirm = false, load = true }) + local ok, err = pcall(vim.pack.add, resolved, { confirm = false, load = true }) + + if not ok and tostring(err):find("ambiguous argument 'HEAD'", 1, true) then + prune_broken_pack_repos() + vim.fn.delete(vim.fn.stdpath("config") .. "/nvim-pack-lock.json") + ok, err = pcall(vim.pack.add, resolved, { confirm = false, load = true }) + end + + if not ok then + vim.schedule(function() + local msg = "vim.pack failed to install/load plugins. Check network and run :lua vim.pack.update()." + vim.notify(msg .. "\n" .. tostring(err), vim.log.levels.WARN) + end) + return false + end + + return true end return M \ No newline at end of file diff --git a/lua/profiles/native.lua b/lua/profiles/native.lua index 8728102..762e044 100644 --- a/lua/profiles/native.lua +++ b/lua/profiles/native.lua @@ -28,7 +28,7 @@ local function setup_flash(keys) end function M.setup() - require("plugins.pack").add({ + local plugins_ready = require("plugins.pack").add({ "folke/flash.nvim", "folke/which-key.nvim", "kylechui/nvim-surround", @@ -61,6 +61,10 @@ function M.setup() vim.opt.foldmethod = "indent" vim.opt.foldlevel = 99 + if not plugins_ready then + return + end + setup_flash({ jump_key = "s", treesitter_key = "S", diff --git a/lua/profiles/vscode.lua b/lua/profiles/vscode.lua index ab776c2..5ea1bcb 100644 --- a/lua/profiles/vscode.lua +++ b/lua/profiles/vscode.lua @@ -28,11 +28,15 @@ local function setup_flash(keys) end function M.setup() - require("plugins.pack").add({ + local plugins_ready = require("plugins.pack").add({ "folke/flash.nvim", { src = "kylechui/nvim-surround", version = vim.version.range("3") }, }) + if not plugins_ready then + return + end + local vscode = require("vscode") vim.notify = vscode.notify