refactor(git): consolidate sync subprocess pattern via util.system_sync

This commit is contained in:
2026-04-28 08:38:14 +02:00
parent d2633ae9c2
commit 4390b55dfe
5 changed files with 57 additions and 67 deletions
+24 -38
View File
@@ -15,24 +15,23 @@ local function attach_index_writer(buf, worktree, path)
vim.api.nvim_buf_get_lines(buf, 0, -1, false),
"\n"
) .. "\n"
local hash = vim.system(
local hash_stdout = util.system_sync(
{ "git", "hash-object", "-w", "--stdin" },
{ cwd = worktree, stdin = body, text = true }
):wait()
if hash.code ~= 0 then
log.error("git hash-object failed: %s", hash.stderr or "")
{ cwd = worktree, stdin = body }
)
if not hash_stdout then
return
end
local sha = vim.trim(hash.stdout or "")
local sha = vim.trim(hash_stdout)
local mode = vim.b[buf].git_index_mode
if not mode then
mode = "100644"
local ls = vim.system(
local ls = util.system_sync(
{ "git", "ls-files", "-s", "--", path },
{ cwd = worktree, text = true }
):wait()
if ls.code == 0 and ls.stdout then
local m = ls.stdout:match("^(%d+)")
{ cwd = worktree, silent = true }
)
if ls then
local m = ls:match("^(%d+)")
if m then
mode = m
end
@@ -42,16 +41,16 @@ local function attach_index_writer(buf, worktree, path)
-- Use the 3-arg form (mode sha path) instead of the comma form
-- (mode,sha,path), which doesn't survive paths containing a
-- comma.
local upd = vim.system({
"git",
"update-index",
"--cacheinfo",
mode,
sha,
path,
}, { cwd = worktree, text = true }):wait()
if upd.code ~= 0 then
log.error("git update-index failed: %s", upd.stderr or "")
if
not util.system_sync({
"git",
"update-index",
"--cacheinfo",
mode,
sha,
path,
}, { cwd = worktree })
then
return
end
vim.bo[buf].modified = false
@@ -105,25 +104,12 @@ function M.read_uri(buf)
end
local revspec = ref == "index" and (":" .. path) or (ref .. ":" .. path)
local result = vim.system(
local stdout = util.system_sync(
{ "git", "show", revspec },
{ cwd = worktree, text = true }
{ cwd = worktree }
)
:wait()
if result.code == 0 then
vim.api.nvim_buf_set_lines(
buf,
0,
-1,
false,
util.split_lines(result.stdout or "")
)
else
log.error(
"git show %s failed: %s",
revspec,
vim.trim(result.stderr or "")
)
if stdout then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, util.split_lines(stdout))
end
if ref == "index" then