fix(pack): recover from broken HEAD repos during startup

This commit is contained in:
2026-05-30 15:27:33 +08:00
parent 94699474c7
commit f3176eb294
3 changed files with 50 additions and 3 deletions
+40 -1
View File
@@ -1,5 +1,28 @@
local M = {} 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) local function to_src(value)
if value:match("^[%w+.-]+://") or value:match("^git@") then if value:match("^[%w+.-]+://") or value:match("^git@") then
return value return value
@@ -21,7 +44,23 @@ function M.add(specs)
end end
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 end
return M return M
+5 -1
View File
@@ -28,7 +28,7 @@ local function setup_flash(keys)
end end
function M.setup() function M.setup()
require("plugins.pack").add({ local plugins_ready = require("plugins.pack").add({
"folke/flash.nvim", "folke/flash.nvim",
"folke/which-key.nvim", "folke/which-key.nvim",
"kylechui/nvim-surround", "kylechui/nvim-surround",
@@ -61,6 +61,10 @@ function M.setup()
vim.opt.foldmethod = "indent" vim.opt.foldmethod = "indent"
vim.opt.foldlevel = 99 vim.opt.foldlevel = 99
if not plugins_ready then
return
end
setup_flash({ setup_flash({
jump_key = "<leader>s", jump_key = "<leader>s",
treesitter_key = "<leader>S", treesitter_key = "<leader>S",
+5 -1
View File
@@ -28,11 +28,15 @@ local function setup_flash(keys)
end end
function M.setup() function M.setup()
require("plugins.pack").add({ local plugins_ready = require("plugins.pack").add({
"folke/flash.nvim", "folke/flash.nvim",
{ src = "kylechui/nvim-surround", version = vim.version.range("3") }, { src = "kylechui/nvim-surround", version = vim.version.range("3") },
}) })
if not plugins_ready then
return
end
local vscode = require("vscode") local vscode = require("vscode")
vim.notify = vscode.notify vim.notify = vscode.notify