local interval_ms = 30 * 60 * 1000 local interval_s = interval_ms / 1000 local state_file = vim.fn.stdpath("state") .. "/config-update-last-check" local config_dir = vim.uv.fs_realpath(vim.fn.stdpath("config")) or vim.fn.stdpath("config") local running = false local function last_check() local f = io.open(state_file, "r") if not f then return 0 end local n = tonumber(f:read("*l")) f:close() return n or 0 end local function mark_checked() vim.fn.mkdir(vim.fn.fnamemodify(state_file, ":h"), "p") local f = io.open(state_file, "w") if f then f:write(tostring(os.time())) f:close() end end local function notify() local msg = "---------- notice ----------\nconfig update! restart now\n--------------" pcall(function() require("lz.n").trigger_load("fidget.nvim") end) local ok = pcall(function() require("fidget").notify(msg, vim.log.levels.ERROR, { annote = "", ttl = 600 }) end) if not ok then vim.notify(msg, vim.log.levels.ERROR) end end local function git(args, cb) local cmd = { "git", "-C", config_dir } vim.list_extend(cmd, args) local env = { GIT_TERMINAL_PROMPT = "0" } if not vim.env.GIT_SSH_COMMAND then env.GIT_SSH_COMMAND = "ssh -o BatchMode=yes -o ConnectTimeout=10" end local ok = pcall(vim.system, cmd, { text = true, env = env, timeout = 60000 }, function(res) vim.schedule(function() cb(res) end) end) if not ok then running = false end end local function finish() running = false end local function check() if running or vim.fn.executable("git") ~= 1 then return end running = true mark_checked() git({ "fetch", "--quiet" }, function(fetched) if fetched.code ~= 0 then return finish() end git({ "rev-list", "--count", "HEAD..@{u}" }, function(counted) if counted.code ~= 0 or (tonumber(vim.trim(counted.stdout or "")) or 0) == 0 then return finish() end git({ "merge", "--ff-only", "--quiet", "@{u}" }, function(merged) finish() if merged.code == 0 then notify() end end) end) end) end vim.api.nvim_create_autocmd("UIEnter", { once = true, callback = function() vim.schedule(function() if os.time() - last_check() >= interval_s then check() end end) end, }) local timer = vim.uv.new_timer() if timer then timer:start(interval_ms, interval_ms, function() vim.schedule(check) end) end