feat(git): highlight only fatal/error lines in failure dump

This commit is contained in:
2026-05-09 00:25:10 +02:00
parent 295803779a
commit 9bbbd1b1c8
2 changed files with 51 additions and 17 deletions
+28 -3
View File
@@ -321,6 +321,32 @@ local function clean_progress_line(line)
return "" return ""
end end
---@param lines string[]
---@param fallback string
---@return [string, string?][]
local function format_error_dump(lines, fallback)
if #lines == 0 then
return { { fallback, "ErrorMsg" } }
end
local chunks = {}
local matched = false
for i, line in ipairs(lines) do
if i > 1 then
table.insert(chunks, { "\n" })
end
if line:match("^fatal:") or line:match("^error:") then
table.insert(chunks, { line, "ErrorMsg" })
matched = true
else
table.insert(chunks, { line })
end
end
if not matched then
return { { table.concat(lines, "\n"), "ErrorMsg" } }
end
return chunks
end
---@param r ow.Git.Repo ---@param r ow.Git.Repo
---@param args string[] ---@param args string[]
local function run_streaming(r, args) local function run_streaming(r, args)
@@ -387,9 +413,8 @@ local function run_streaming(r, args)
) )
else else
emit_progress(("exit %d"):format(code), "failed") emit_progress(("exit %d"):format(code), "failed")
local body = #accum > 0 and table.concat(accum, "\n") local fallback = ("%s failed: exit %d"):format(title, code)
or ("%s failed: exit %d"):format(title, code) vim.api.nvim_echo(format_error_dump(accum, fallback), true, {})
vim.api.nvim_echo({ { body, "ErrorMsg" } }, true, {})
end end
end end
+23 -14
View File
@@ -550,7 +550,7 @@ t.test(
) )
t.test( t.test(
"streaming :G fetch (no bang) on failure dumps output to :messages with ErrorMsg", "streaming :G fetch (no bang) on failure highlights only fatal/error lines",
function() function()
make_repo({ a = "x" }) make_repo({ a = "x" })
with_echo_stub(function(calls) with_echo_stub(function(calls)
@@ -558,24 +558,33 @@ t.test(
t.wait_for(function() t.wait_for(function()
return has_status(calls, "failed") return has_status(calls, "failed")
end, "failed progress notification", 5000) end, "failed progress notification", 5000)
local body = "" ---@type table?
local dump
for _, c in ipairs(calls) do for _, c in ipairs(calls) do
if if c.history == true then
c.history == true dump = c.chunks
and c.chunks[1]
and c.chunks[1][2] == "ErrorMsg"
then
body = c.chunks[1][1]
break break
end end
end end
t.truthy(body ~= "", "expected ErrorMsg history dump") t.truthy(dump, "expected history dump")
local lower = string.lower(body) ---@cast dump -nil
local fatal_chunks_red, plain_continuation = 0, false
for _, chunk in ipairs(dump) do
local text, hl = chunk[1], chunk[2]
if text:match("^fatal:") and hl == "ErrorMsg" then
fatal_chunks_red = fatal_chunks_red + 1
end
if text:match("Please make sure") and hl ~= "ErrorMsg" then
plain_continuation = true
end
end
t.truthy( t.truthy(
lower:match("repository") fatal_chunks_red >= 1,
or lower:match("remote") "expected at least one fatal: line highlighted as ErrorMsg"
or lower:match("fatal"), )
"expected error mention in dump, got: " .. body t.truthy(
plain_continuation,
"expected continuation line to be plain"
) )
end) end)
end end