chore(git): consistent error reporting on git failures

This commit is contained in:
2026-04-27 13:33:09 +02:00
parent 20dc6cc3c9
commit 6e0e05ae56
5 changed files with 81 additions and 14 deletions
+11 -2
View File
@@ -20,15 +20,17 @@ local cached_cmds
---@param result vim.SystemCompleted
local function populate_cached_cmds(result)
if result.code ~= 0 then
log.error("git --list-cmds failed: %s", vim.trim(result.stderr or ""))
return
end
cached_cmds = {}
if result.code == 0 then
for line in (result.stdout or ""):gmatch("[^\r\n]+") do
if line ~= "" then
table.insert(cached_cmds, line)
end
end
table.sort(cached_cmds)
end
end
---Prime `cached_cmds` asynchronously so the first `:G <Tab>` doesn't block.
@@ -111,6 +113,13 @@ local function run_in_split(worktree, args, conf)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, split_lines(content))
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
if obj.code ~= 0 then
log.error(
"git %s failed: %s",
args[1] or "",
vim.trim(obj.stderr or "")
)
end
end)
end)
end
+2
View File
@@ -25,6 +25,8 @@ function M.commit(opts)
):wait()
if result.code == 0 then
initial = (result.stdout or ""):gsub("\n+$", "")
else
log.warning("git log -1 failed: %s", vim.trim(result.stderr or ""))
end
end
+13 -2
View File
@@ -62,8 +62,19 @@ local function read_show(worktree, revspec)
{ cwd = worktree, text = true }
)
:wait()
local content = result.code == 0 and (result.stdout or "") or ""
local lines = vim.split(content, "\n", { plain = true, trimempty = false })
if result.code ~= 0 then
log.error(
"git show %s failed: %s",
revspec,
vim.trim(result.stderr or "")
)
return {}
end
local lines = vim.split(
result.stdout or "",
"\n",
{ plain = true, trimempty = false }
)
if #lines > 0 and lines[#lines] == "" then
table.remove(lines)
end
+4
View File
@@ -1,3 +1,4 @@
local log = require("log")
local util = require("util")
local UNMERGED = {
@@ -73,6 +74,7 @@ local function resolve(path)
f:close()
local gitdir = content:match("gitdir:%s*(%S+)")
if not gitdir then
log.warning(".git file at %s has no `gitdir:` line", found)
return nil
end
if not gitdir:match("^/") then
@@ -148,6 +150,8 @@ local function do_refresh(repo)
statuses[vim.fs.joinpath(repo.worktree, path_part)] =
format(code)
end
else
log.warning("git status failed: %s", vim.trim(obj.stderr or ""))
end
local dirty = false
for buf in pairs(repo.buffers) do
+45 -4
View File
@@ -145,6 +145,9 @@ local function fetch_status(worktree, callback)
Unpushed = {},
Unpulled = {},
}
if obj.code ~= 0 then
log.error("git status failed: %s", vim.trim(obj.stderr or ""))
end
if obj.code == 0 then
for line in (obj.stdout or ""):gmatch("[^\r\n]+") do
if line:sub(1, 2) == "##" then
@@ -237,6 +240,12 @@ local function fetch_status(worktree, callback)
})
end
end
else
log.error(
"git log %s failed: %s",
f.range,
vim.trim(log_obj.stderr or "")
)
end
pending = pending - 1
if pending == 0 then
@@ -643,7 +652,17 @@ local function action_stage()
if entry.section == "Staged" then
return
end
vim.system({ "git", "add", "--", entry.path }, { cwd = s.worktree })
vim.system(
{ "git", "add", "--", entry.path },
{ cwd = s.worktree },
function(obj)
if obj.code ~= 0 then
vim.schedule(function()
log.error("git add failed: %s", vim.trim(obj.stderr or ""))
end)
end
end
)
end
local function action_unstage()
@@ -660,7 +679,16 @@ local function action_unstage()
table.insert(cmd, entry.orig)
end
table.insert(cmd, entry.path)
vim.system(cmd, { cwd = s.worktree })
vim.system(cmd, { cwd = s.worktree }, function(obj)
if obj.code ~= 0 then
vim.schedule(function()
log.error(
"git restore --staged failed: %s",
vim.trim(obj.stderr or "")
)
end)
end
end)
end
local function action_discard()
@@ -678,7 +706,10 @@ local function action_discard()
if entry.section == "Untracked" then
prompt = string.format("Delete untracked file %s?", entry.path)
action = function()
os.remove(vim.fs.joinpath(s.worktree, entry.path))
local ok, err = os.remove(vim.fs.joinpath(s.worktree, entry.path))
if not ok then
log.error("failed to remove %s: %s", entry.path, err or "")
end
refresh(vim.api.nvim_get_current_buf())
end
elseif entry.section == "Unstaged" then
@@ -686,7 +717,17 @@ local function action_discard()
action = function()
vim.system(
{ "git", "checkout", "--", entry.path },
{ cwd = s.worktree }
{ cwd = s.worktree },
function(obj)
if obj.code ~= 0 then
vim.schedule(function()
log.error(
"git checkout failed: %s",
vim.trim(obj.stderr or "")
)
end)
end
end
)
end
else