fix(git): use nul char as separator for git status

This commit is contained in:
Folke Lemaitre 2025-02-04 19:08:37 +01:00
parent e3c5944421
commit 8e0dfd2856
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 7 additions and 2 deletions

View file

@ -56,6 +56,7 @@ function M.update(cwd, opts)
"-uall", "-uall",
"--porcelain=v1", "--porcelain=v1",
"--ignored=matching", "--ignored=matching",
"-z",
}, },
}, function() }, function()
stdout:close() stdout:close()
@ -71,7 +72,7 @@ function M.update(cwd, opts)
return return
end end
local ret = {} ---@type snacks.explorer.git.Status[] local ret = {} ---@type snacks.explorer.git.Status[]
for _, line in ipairs(vim.split(output, "\r?\n")) do for _, line in ipairs(vim.split(output, "\0")) do
if line ~= "" then if line ~= "" then
local status, file = line:sub(1, 2), line:sub(4) local status, file = line:sub(1, 2), line:sub(4)
ret[#ret + 1] = { ret[#ret + 1] = {

View file

@ -106,6 +106,7 @@ function M.status(opts, ctx)
"status", "status",
"-uall", "-uall",
"--porcelain=v1", "--porcelain=v1",
"-z",
} }
if opts.ignored then if opts.ignored then
table.insert(args, "--ignored=matching") table.insert(args, "--ignored=matching")
@ -116,6 +117,7 @@ function M.status(opts, ctx)
return require("snacks.picker.source.proc").proc({ return require("snacks.picker.source.proc").proc({
opts, opts,
{ {
sep = "\0",
cwd = cwd, cwd = cwd,
cmd = "git", cmd = "git",
args = args, args = args,

View file

@ -9,6 +9,7 @@ local islist = vim.islist or vim.tbl_islist
---@class snacks.picker.proc.Config: snacks.picker.Config ---@class snacks.picker.proc.Config: snacks.picker.Config
---@field cmd string ---@field cmd string
---@field sep? string
---@field args? string[] ---@field args? string[]
---@field env? table<string, string> ---@field env? table<string, string>
---@field cwd? string ---@field cwd? string
@ -37,6 +38,7 @@ function M.proc(opts, ctx)
end end
end end
local sep = opts.sep or "\n"
local aborted = false local aborted = false
local stdout = assert(uv.new_pipe()) local stdout = assert(uv.new_pipe())
@ -93,7 +95,7 @@ function M.proc(opts, ctx)
end end
local from = 1 local from = 1
while from <= #data do while from <= #data do
local nl = data:find("\n", from, true) local nl = data:find(sep, from, true)
if nl then if nl then
local cr = data:byte(nl - 1, nl - 1) == 13 -- \r local cr = data:byte(nl - 1, nl - 1) == 13 -- \r
local line = data:sub(from, nl - (cr and 2 or 1)) local line = data:sub(from, nl - (cr and 2 or 1))