refactor(git): route every git invocation through util.git

This commit is contained in:
2026-05-08 03:44:23 +02:00
parent ebfcaef240
commit 01a543c12f
6 changed files with 115 additions and 132 deletions
+33 -38
View File
@@ -22,17 +22,12 @@ local function git_cmds()
if cached_cmds then
return cached_cmds
end
local result = vim.system(
{ "git", "--list-cmds=main,others,alias" },
{ text = true }
)
:wait()
if result.code ~= 0 then
util.error("git --list-cmds failed: %s", vim.trim(result.stderr or ""))
local out = util.git({ "--list-cmds=main,others,alias" })
if not out then
return {}
end
cached_cmds = {}
for line in (result.stdout or ""):gmatch("[^\r\n]+") do
for line in out:gmatch("[^\r\n]+") do
if line ~= "" then
table.insert(cached_cmds, line)
end
@@ -191,10 +186,16 @@ end
local function run_in_split(r, args, conf)
util.git(args, {
cwd = r.worktree,
on_done = function(stdout)
if not stdout then
on_exit = function(result)
if result.code ~= 0 then
util.error(
"git %s failed: %s",
args[1] or "?",
vim.trim(result.stderr or "")
)
return
end
local stdout = result.stdout or ""
local buf = place_split("[Git " .. table.concat(args, " ") .. "]")
repo.bind(buf, r)
object.attach_dispatch(buf)
@@ -216,14 +217,11 @@ end
---@param r ow.Git.Repo
---@param args string[]
local function run_to_messages(r, args)
local cmd = { "git" }
vim.list_extend(cmd, args)
vim.system(
cmd,
{ cwd = r.worktree, text = true },
vim.schedule_wrap(function(obj)
local out = vim.trim(obj.stdout or "")
local err = vim.trim(obj.stderr or "")
util.git(args, {
cwd = r.worktree,
on_exit = function(result)
local out = vim.trim(result.stdout or "")
local err = vim.trim(result.stderr or "")
local chunks = {}
if out ~= "" then
table.insert(chunks, { out })
@@ -234,17 +232,17 @@ local function run_to_messages(r, args)
end
table.insert(chunks, { err, "ErrorMsg" })
end
if #chunks == 0 and obj.code ~= 0 then
if #chunks == 0 and result.code ~= 0 then
table.insert(
chunks,
{ "git exited " .. tostring(obj.code), "ErrorMsg" }
{ "git exited " .. tostring(result.code), "ErrorMsg" }
)
end
if #chunks > 0 then
vim.api.nvim_echo(chunks, true, {})
end
end)
)
end,
})
end
---@param args string[]
@@ -337,11 +335,11 @@ end
---@return string[]
local function list_files(r, dir)
return r:get_cached("files:" .. dir, function(self)
local cmd = { "git", "ls-files" }
local args = { "ls-files" }
if dir ~= "" then
table.insert(cmd, dir)
table.insert(args, dir)
end
local out = util.exec(cmd, { cwd = self.worktree, silent = true })
local out = util.git(args, { cwd = self.worktree, silent = true })
return out and util.split_lines(out) or {}
end)
end
@@ -350,8 +348,8 @@ end
---@return string[]
local function list_remotes(r)
return r:get_cached("remotes", function(self)
local out = util.exec(
{ "git", "remote" },
local out = util.git(
{ "remote" },
{ cwd = self.worktree, silent = true }
)
return out and util.split_lines(out) or {}
@@ -382,13 +380,10 @@ local function fetch_completions(sub)
if cached_completions[sub] then
return cached_completions[sub]
end
local out = util.exec(
{ "git", sub, "--git-completion-helper-all" },
local out = util.git(
{ sub, "--git-completion-helper-all" },
{ silent = true }
) or util.exec(
{ "git", sub, "--git-completion-helper" },
{ silent = true }
)
) or util.git({ sub, "--git-completion-helper" }, { silent = true })
local items = {}
if out then
for tok in out:gmatch("%S+") do
@@ -469,8 +464,8 @@ function M.complete_rev(arg_lead)
local stage, stage_path_lead = arg_lead:match("^:([0-3]):(.*)$")
if stage then
local out = util.exec(
{ "git", "ls-files", "--stage" },
local out = util.git(
{ "ls-files", "--stage" },
{ cwd = r.worktree, silent = true }
)
if not out then
@@ -506,11 +501,11 @@ function M.complete_rev(arg_lead)
name_lead = name_lead or path_lead
if rev ~= "" then
local cmd = { "git", "ls-tree", rev }
local args = { "ls-tree", rev }
if dir ~= "" then
table.insert(cmd, dir)
table.insert(args, dir)
end
local out = util.exec(cmd, { cwd = r.worktree, silent = true })
local out = util.git(args, { cwd = r.worktree, silent = true })
if not out then
return {}
end